0

I am making a trigger to alter a table that adds a new column to it and inserts the value in it.

My code looks lyk dis:

 delimiter |

 CREATE TRIGGER addfield AFTER INSERT ON `entity_group_mapping`
 FOR EACH ROW BEGIN
     ALTER TABLE user_access ADD NEW.type_name INT(2) NOT NULL;
 END;

 |
 delimiter ;

It is giving me an error: #1103 - Incorrect table name 'NEW'

2
  • Dynamically changing a table structure is usually wrong. What are you trying to do?
    – Peter Lang
    Aug 3, 2012 at 6:15
  • no this my requirement.. I need to bulid a form in which the no. of fields displayed are dyanamic... So adding a new form element needs to add a new column in a table. Aug 3, 2012 at 6:18

3 Answers 3

2

From the documentation:

There is a hard limit of 4096 columns per table...Every table (regardless of storage engine) has a maximum row size of 65,535 bytes.

Can you reach these limitations? Even if you cannot, I'd suggest you to think about design, and add records instead of new fields. Then you could try to PIVOT table - translate rows into fields, there are many pivot examples in the internet and of course in stackoverflow.

1
  • ohk thanx.. fr the PIVOT example.. i aint knew dat. Aug 3, 2012 at 8:29
1

try this without NEW.

 ALTER TABLE user_access ADD type_name INT(2) NOT NULL;
2
  • It doesn't work in Mysql. Also you can not use prepare statements inside trigger to form and execute a query dynamically.
    – Omesh
    Aug 3, 2012 at 6:14
  • Dynamic SQL is also not allowed inside trigger in MySQL. In Oracle, yes you can do that.
    – hemu
    Aug 3, 2012 at 6:20
1

ALTER TABLE or CREATE TABLE is not allowed inside trigger in MySql. So In anyways it won't work.

5
  • so any other way to achieve dis? Aug 3, 2012 at 6:12
  • As far as I know, If you are using MySQL & you want to alter table on any insert in table 'entity_group_mapping', then you may e able to do that only from your server side code, like you can make one method and call it after insert is done successful in one transaction .
    – hemu
    Aug 3, 2012 at 6:17
  • OK. If you want to from trigger then you can do one thing. Prepare the query string to alter table in trigger & then insert that query string in another table, let's say 'alterQuery' table. Now in you application you can write down the code which will run everyday or at interval of 2-3 hours (whatever time period you want) & will fetch all the queries from 'alterQuery' table, create batch and run them. Thus deleting all successfully ran queries & making 'alterQuery' empty
    – hemu
    Aug 3, 2012 at 6:26
  • But my requirement is bit different. See i have two tables entity_field, entity_data, and i have a form that inserts a new row in entity_field and at this point i want to add a new column in table entity_data. My requirement is to finally generate a dyanamic form whose form elements depends upon the values entered in entity_data.. !! Aug 3, 2012 at 6:29
  • Ok. Create a stored procedure to insert a new row in entity_field. In that stored procedure you also write down the code to alter table entity_data & add new column. Also in stored procedure you can write Dynamic SQL if needed.
    – hemu
    Aug 3, 2012 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.