Trigger Does not works - Alter table to add a new column before inserting in it

≡放荡痞女 提交于 2020-01-07 02:26:48

问题


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'


回答1:


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.




回答2:


try this without NEW.

 ALTER TABLE user_access ADD type_name INT(2) NOT NULL;



回答3:


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



来源:https://stackoverflow.com/questions/11790112/trigger-does-not-works-alter-table-to-add-a-new-column-before-inserting-in-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!