MySQL 如果从 mysqldump 不存在,则创建表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2220469/
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
Create table if not exists from mysqldump
提问by khelll
I'm wondering if there is any way in mysqldump to add the appropriate create table option [IF NOT EXISTS]. Any ideas?
我想知道 mysqldump 中是否有任何方法可以添加适当的创建表选项 [IF NOT EXISTS]。有任何想法吗?
采纳答案by Daniel Vassallo
According to one source, mysqldump does not feature this option.
据一位消息人士称,mysqldump 没有此选项。
You could use the --force
option when importing the dump file back, where MySQL will ignore the errors generated from attempts to create duplicate tables. However note that with this method, other errors would be ignored as well.
您可以--force
在导入转储文件时使用该选项,其中 MySQL 将忽略因尝试创建重复表而产生的错误。但是请注意,使用此方法,其他错误也将被忽略。
Otherwise, you can run your dump file through a script that would replace all occurrences of CREATE TABLE
with CREATE TABLE IF NOT EXISTS
.
否则,您可以通过一个脚本运行您的转储文件,该脚本将替换所有出现的CREATE TABLE
with CREATE TABLE IF NOT EXISTS
。
回答by Pawel
Try to use this on your SQL file:
尝试在您的 SQL 文件中使用它:
sed 's/CREATE TABLE/CREATE TABLE IF NOT EXISTS/g' <file-path>
or to save
或保存
sed -i 's/CREATE TABLE/CREATE TABLE IF NOT EXISTS/g' <file-path>
it's not ideal but it works :P
这并不理想,但它有效:P
回答by sfussenegger
Using sed
as described by @Pawel works well. Nevertheless you might not like the idea of piping your data through more potential error sources than absolutely necessary. In this case one may use two separate dumps:
sed
按照@Pawel 的描述使用效果很好。然而,您可能不喜欢通过比绝对必要的更多潜在错误源来传输数据的想法。在这种情况下,可以使用两个单独的转储:
- first dump containing table definitions (
--no-data --skip-add-drop-table
) - second dump with only data (
--no-create-info --skip-add-drop-table
)
- 第一个包含表定义的转储 (
--no-data --skip-add-drop-table
) - 仅包含数据的第二个转储 (
--no-create-info --skip-add-drop-table
)
There are some other things to take care of though (e.g. triggers). Check the manual for details.
还有一些其他的事情需要处理(例如触发器)。查看手册了解详细信息。
回答by Boldewyn
Not what you might want, but with --add-drop-table
every CREATE is prefixed with the according DROP TABLE statement.
不是您可能想要的,但是--add-drop-table
每个 CREATE 都以相应的 DROP TABLE 语句为前缀。
Otherwise, I'd go for a simple search/replace (e.g., with sed
).
否则,我会进行简单的搜索/替换(例如,使用sed
)。
回答by Artistan
create a bash script with this... make sure you make it executable. (chmod 0777 dump.sh)
用这个创建一个 bash 脚本...确保你让它可执行。(chmod 0777 dump.sh)
dump.sh
转储文件
#!/bin/bash
name=$HOSTNAME
name+="-"
name+=$(date +"%Y-%m-%d.%H:%M")
name+=".sql"
echo $name;
mysqldump --replace --skip-add-drop-table --skip-comments izon -p > "$name"
sed -i 's/CREATE TABLE/CREATE TABLE IF NOT EXISTS/g' "$name"
回答by Celepine
The sed will be muchfaster without the 'g' (global) at its end:
战略经济对话将是多快,而不在其结束的“G”(全球):
eg:
例如:
mysqldump -e <database> | sed 's/^CREATE TABLE /CREATE TABLE IF NOT EXISTS /' > <database>.sql
回答by Mostafa
The dump output is the combination of DROP and CREATE, so you must remove DROP statement and change the CREATE statement to form a valid (logical) output:
转储输出是 DROP 和 CREATE 的组合,因此您必须删除 DROP 语句并更改 CREATE 语句以形成有效(逻辑)输出:
mysqldump --no-data -u root <schema> | sed 's/^CREATE TABLE /CREATE TABLE IF NOT EXISTS /'| sed 's/^DROP TABLE IF EXISTS /-- DROP TABLE IF EXISTS /' > <schema>.sql
回答by Franc
To Find and replace the text On Windows 7 Using PowerShell
使用 PowerShell 在 Windows 7 上查找和替换文本
Open command prompt and use the command below
打开命令提示符并使用下面的命令
powershell -Command "(gc E:\map\map_2017.sql) -replace 'CREATE TABLE', 'CREATE TABLE IF NOT EXISTS' | Out-File E:\map\map_replaced.sql"
First param is the filepath
Second param is 'find string'
Third param is 'replace string'
第一个参数是文件路径
第二个参数是“查找字符串”
第三个参数是“替换字符串”
This command will create a new file with the replaced text. Remove the Command starting from '|' (pipe) if you want to replace and save contents on the same file.
此命令将创建一个带有替换文本的新文件。删除以“|”开头的命令 (pipe) 如果要替换和保存同一文件中的内容。