how to add a new column to multiple mysql tables a

2019-09-09 10:18发布

问题:

I have a MySQL Database containing 100 tables. I want to add 2 fields in all those 100 tables at once.

fields are 'created_by' and 'modified_by'.

any suggestions...

回答1:

There is no automatic way to do this. Instead, construct the SQL queries and then run them. Something like this:

select concat('alter table ', t.table_name,
              ' add created_by varchar(255), add modified_by varchar(255)'
             )
from information_schema.tables t;

Then copy the code into the appropriate tool and execute it (use use prepare).

I would also recommend that you at created_at and modified_at, if these are not already present.