我可以在不丢失当前内容的情况下将更新的结构导入 MySQL 表吗? [英] Can I Import an updated structure into a MySQL table without losing its current content?

查看:30
本文介绍了我可以在不丢失当前内容的情况下将更新的结构导入 MySQL 表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 MySQL 表,随着产品的发展,我们会不时向其中添加新字段.我正在寻找一种方法将表的结构从数据库的一个副本导出到另一个副本,而不会删除我要导入的表的内容.

We use MySQL tables to which we add new fields from time to time as our product evolves. I'm looking for a way to export the structure of the table from one copy of the db, to another, without erasing the contents of the table I'm importing to.

例如说我有一个表的副本 A 和 B,我将字段 X、Y、Z 添加到表 A.有没有办法将更改后的结构(字段 X、Y、Z)复制到表 B,同时保持其内容完整?

For example say I have copies A and B of a table, and I add fields X,Y,Z to table A. Is there a way to copy the changed structure (fields X,Y,Z) to table B while keeping its content intact?

我尝试使用 mysqldump,但似乎我只能复制整个表及其内容,覆盖旧表,或者我可以使用-d"标志来避免复制数据(仅转储结构),但这导入时将创建一个空表,再次覆盖旧数据.

I tried to use mysqldump, but it seems I can only copy the whole table with its content, overriding the old one, or I can use the "-d" flag to avoid copying data (dumping structure only), but this will create an empty table when imported, again overriding old data.

有什么办法可以用 mysqldump 或其他工具做我需要的事情吗?

Is there any way to do what I need with mysqldump, or some other tool?

推荐答案

我通常做的是存储在开发表上运行的每一个 ALTER TABLE 语句,并将它们应用到如有必要,目标表.

What I usually do is store each and every ALTER TABLE statement run on the development table(s), and apply them to the target table(s) whenever necessary.

有更复杂的方法可以做到这一点(如结构比较工具等),但我发现这种做法效果很好.手动逐步执行此操作还有助于防止因更改字段类型或最大长度的结构更改而意外更改或破坏数据.

There are more sophisticated ways to do this (like structure comparison tools and such), but I find this practice works well. Doing this on a manual step by step basis also helps prevent accidental alteration or destruction of data by structural changes that change a field's type or maximum length.

这篇关于我可以在不丢失当前内容的情况下将更新的结构导入 MySQL 表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆