2016-04-24 63 views
2

我想問一下,是否有某種方法可以將具有相同模式的表添加到同一服務器中的多個數據庫中,並且如果可以添加例如再次存在於同一服務器中的許多數據庫中的列到特定表。有沒有一些疑問,或者是不可能的?在同一臺服務器上的許多數據庫中批量創建表

+0

您可以使用內部'information_schema'數據庫中的表並創建批量聲明。 –

回答

2

對於創建表:

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'); 

創建結果:

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

複製的結果和執行。

同樣可以創建批量ALTER查詢。

+0

非常感謝您的快速響應。我有一些問題:schema_name是我需要創建表的數據庫的名稱('test',test_db),.X是表的名稱,a,b是列? –

+0

正確。你可以使用另一個條件,比如'where db.schema_name like'project _%''。但我認爲白名單是這個關鍵任務的更好方式。 –

相關問題