且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用触发器将MySQL列添加到表中

更新时间:2023-11-30 20:01:10

简短答案:不可能。

ALTER TABLE是所谓的 DDL语句(数据定义语言),与 DML语句(数据操作语言)不同。

ALTER TABLE is a so-called "DDL-statement" (data definition language), which differs from "DML-statements" (data manipulation language).

您将需要动态SQL来执行此操作,因为表名在DDL中不是变量。

You would need dynamic SQL to do this, because table names are not variable in DDL.

不幸的是,触发器中不允许使用动态SQL。尝试在触发器中执行prepare语句时,mysql会引发错误:

Unfortunately, dynamic SQL is not allowed in triggers. When trying to execute a prepare statement in a trigger, mysql throws an error:

Dynamic SQL is not allowed in stored function or trigger

更多mysql文档: https://dev.mysql.com/doc/refman/5.7/en/stored-program-restrictions.html

More in the mysql documentation: https://dev.mysql.com/doc/refman/5.7/en/stored-program-restrictions.html