Dimitrios Markopoulos Dimitrios Markopoulos - 2 years ago 89
PHP Question

bulk create table in many databases in same server

I would like to ask whether there is some way to add a table with same schema to multiple databases in the same server and if it is possible to add e.g. a column to a specific table again in many databases that exist in same server. Is there some query for that or is it impossible?

Answer Source

For table creation:

select group_concat(
  concat('create table `', db.schema_name, '`.X (a int, b int);') separator '\n'
) as qry
from information_schema.schemata db
where db.schema_name in ('test', 'test_db');

Created result:

create table `test`.X (a int, b int);
create table `test_db`.X (a int, b int);

Copy the result and execute.

Same way you can create a bulk ALTER query.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download