Mysql:添加新字段时如何同步2个表结构?

时间:2018-06-06 10:40:13

标签: mysql

我有N个数据库:1个在开发中,N-1在生产中,有> 50个字段。

我在devlopment中添加了新字段。然后我手动将新字段添加到我的生产表中。

有没有办法同步结构:如果这些不存在则添加新字段? (一片增量更新?) 如果我放弃表并创建,我也将丢失数据。

如果我复制旧表然后尝试插入数据,我会得到一个sql错误告诉我旧表和新表没有相同数量的列

insert into newTable select * from oldTable; // error different number of fields in structure

2 个答案:

答案 0 :(得分:2)

使用命令行工具mysqldiff。

mysqldiff -d sql -changes-for=server2 \
--server1=username1:username1@host1 \
--server2=username2:password2@host2 \
your_database.your_table1:your_database.your_table2 

该程序将生成SQL命令以将 your_table2 的结构更改为与 your_table1 类似。

答案 1 :(得分:0)

足以指定要插入的列集以及所需的列:

insert into newTable (col1,col2,...,colN)
select toCol1, toCol2, ..., toColN from oldTable;