Skip to content
Advertisement

Sync columns(without data) in multiple tables

I have multiple tables inside my database:

groups_name1
groups_name2
etc.

Is there a way to keep the columns of all the tables in sync with each other?

+----+------+-----------+
| id | name | last-edit |
+----+------+-----------+
+----+------+
| id | name | (copy column 'last-edit' here and all the other tables)
+----+------+

What I mean is that when I add a new column named ‘⁣last-edit‘ inside the first table, this column will duplicate to all the other tables. But the data inside the columns will only be unique to the original tables.

Advertisement

Answer

There’s no feature in MySQL itself to apply the same DDL to multiple tables.

ALTER TABLE can reference only one table per statement. There is no trigger support for DDL statements.

An ALTER TABLE must be run for each table you want to apply it to. There’s no way MySQL can infer which tables you want to include in that set of tables. It’s up to you to run each ALTER TABLE.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement