Create Table If Not Exists From Mysqldump
Answer :
Try to use this on your SQL file:
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
According to one source, mysqldump does not feature this option.
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.
Otherwise, you can run your dump file through a script that would replace all occurrences of CREATE TABLE
with CREATE TABLE IF NOT EXISTS
.
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:
- first dump containing table definitions (
--no-data --skip-add-drop-table
) - second dump with only data (
--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.
Comments
Post a Comment