database - MYSQL: Is there a way to CREATE a new table or DROP the existing table on trigger? -


is there way create or drop tables in trigger?

ex: if have 2 tables, table1 , table2.

i want create trigger on table2, such whenever table1 gets new inserted value or updates, drop current table2, , create table3 (a new version of table2 incorporates changes in table1)

can such thing in mysql? , syntax if there way?

no.

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

the trigger cannot use statements explicitly or implicitly begin or end transaction, such start transaction, commit, or rollback. (rollback savepoint permitted because not end transaction.).

all ddl statements cause implicit commit.

here's example:

mysql> create trigger t after insert on mytable each row     create table if not exists foo (i int);  error 1422 (hy000): explicit or implicit commit not allowed in stored function or trigger. 

your use case sounds bad design. insert , update should affect data only, not have side-effects of changing structure of table.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -