将 .sql 文件导入 MySQL 是否会覆盖现有数据库或附加到它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12619871/
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
Does import of a .sql file to MySQL overwrite the existing db or append to it?
提问by Edward
In some cases, I've had to do a mysqldump to produce a .sql file. After making some changes to the MySQL database for testing and development I want to restore it to the way it was before. So I import the .sql file. In the past I have delete the db and re-created it, and then did te import. Is that necessary? Does import from a .sql file overwrite and totally re-create the database and it's tables, or does it append to it? Thanks!
在某些情况下,我不得不执行 mysqldump 来生成 .sql 文件。在对 MySQL 数据库进行一些更改以进行测试和开发之后,我想将其恢复到以前的状态。所以我导入了 .sql 文件。过去我删除了数据库并重新创建了它,然后进行了导入。那有必要吗?从 .sql 文件导入会覆盖并完全重新创建数据库及其表,还是附加到它?谢谢!
采纳答案by Jon
It depends on what commands the SQL file contains. Commands of interest are:
这取决于 SQL 文件包含哪些命令。感兴趣的命令是:
DROP DATABASE xxx; # will delete the whole database
DROP TABLE xxx; # unconditionally deletes a table
CREATE TABLE [IF NOT EXISTS] # if IF NOT EXISTS adds the table, does nothing if exists
# otherwise, it adds the table, gives an error if it exists
回答by L0j1k
Appends to it, unless the import file specifically treats tables differently.
附加到它,除非导入文件专门以不同的方式处理表。