MySQL 导入 - 如果存在行,如何忽略删除表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20573778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL import - How to ignore Drop table if exists line?
提问by zenCoder
I exported 2 identical databases(identical in terms of names and structures of tables) into two .sql files using mysqldump. I want to merge them into one file. However, both the databases have a 'Drop table' line before every table. What that means is if I import db1 and then db2, db1 tables are dropped before db2 tables are imported.
我使用 mysqldump 将 2 个相同的数据库(在表的名称和结构方面相同)导出到两个 .sql 文件中。我想将它们合并为一个文件。但是,两个数据库在每个表之前都有一个“删除表”行。这意味着如果我先导入 db1,然后导入 db2,那么在导入 db2 表之前会删除 db1 表。
The files are huge and I am not able to open them in the editor. Also, there are 50 tables in each databases.
这些文件很大,我无法在编辑器中打开它们。此外,每个数据库中有 50 个表。
How can I ignore the Drop table command during mysql import?
如何在 mysql 导入期间忽略 Drop table 命令?
回答by GoingMyWay
All you need is add --skip-add-drop-table
option when using mysqldump
.
您只需要--skip-add-drop-table
在使用mysqldump
.
$ mysqldump --databases --skip-add-drop-table -u root db1 > /tmp/qqq.2
So, there would not DROP TABLE IF EXISTS
in sql
files.
所以,就没有DROP TABLE IF EXISTS
在sql
文件中。
回答by ravnur
If you do not want to make dump once again and you are using Linux you can go with:
如果您不想再次进行转储并且您使用的是 Linux,则可以使用:
awk '!/^DROP TABLE IF EXISTS/{print}' <dump.file> | mysql <db_name>
If you want to dump data once again you should pass --skip-add-drop-table
to mysqldump utility.
如果你想再次转储数据,你应该传递--skip-add-drop-table
给 mysqldump 实用程序。
回答by Mike Brant
I guess I don't see why a DROP TABLE statement should be problematic or why you need to merge dumps for two IDENTICAL databases.
我想我不明白为什么 DROP TABLE 语句应该有问题,或者为什么需要合并两个相同数据库的转储。
That being said, you should probably just not add DROP TABLE in the initial dump. This would be controlled via flag use in your mysqldump command as noted in the documention at http://dev.mysql.com/doc/refman/5.5/en/mysqldump.html
话虽如此,您可能不应该在初始转储中添加 DROP TABLE。这将通过在您的 mysqldump 命令中使用标志来控制,如http://dev.mysql.com/doc/refman/5.5/en/mysqldump.html的文档中所述
This probably means you need to use --skip-opt
flag if you were using default options (default is to run as if --opt
flag is passed). You will then need to specify all flags within --opt
that you still want to use.
这可能意味着--skip-opt
如果您使用默认选项,则需要使用标志(默认是像--opt
传递标志一样运行)。然后--opt
,您需要指定仍要使用的所有标志。