简体   繁体   English

MYSQL:有没有一种方法可以在触发时创建新表或删除现有表?

[英]MYSQL: Is there a way to CREATE a new table or DROP the existing table on trigger?

Is there a way to create or drop tables in a trigger? 有没有一种方法可以在触发器中创建或删除表?

ex: If I have two tables, table1 and table2. 例如:如果我有两个表,table1和table2。

I want to create a trigger on table2, such that whenever table1 gets new inserted value or updates, drop current table2, and then create table3 (a new version of table2 that incorporates the changes in table1) 我想在table2上创建一个触发器,以便每当table1获得新的插入值或更新时,删除当前的table2,然后创建table3(包含对table1所做更改的table2的新版本)

can I do such thing in Mysql? 我可以在Mysql中做这样的事情吗? and what would be the syntax like if there is a way? 如果有办法的话,语法会是什么样?

No. 没有。

https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html says: https://dev.mysql.com/doc/refman/5.7/zh-CN/trigger-syntax.html说:

The trigger cannot use statements that explicitly or implicitly begin or end a transaction, such as START TRANSACTION, COMMIT, or ROLLBACK. 触发器不能使用显式或隐式开始或结束事务的语句,例如START TRANSACTION,COMMIT或ROLLBACK。 (ROLLBACK to SAVEPOINT is permitted because it does not end a transaction.). (允许ROLLBACK到SAVEPOINT,因为它不结束事务。)

All DDL statements cause an implicit COMMIT. 所有DDL语句都会导致隐式COMMIT。

Here's an example: 这是一个例子:

mysql> create trigger t after insert on mytable for each row
    create table if not exists foo (i int);

ERROR 1422 (HY000): Explicit or implicit commit is not allowed in stored function or trigger.

Your use case sounds like a bad design. 您的用例听起来很糟糕。 INSERT and UPDATE should affect data only, not have side-effects of changing the structure of your table. INSERT和UPDATE应该只影响数据,而没有改变表结构的副作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM