Backup MySQL schema with foreign key table constraints

 ̄綄美尐妖づ 提交于 2019-12-24 08:57:29

问题


I created my MySQL schema that consists of multiple tables and I decided that I would add the foreign key constraints afterwards for each table, using the command:

ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)

How can I get a backup of the schema (containing the foreign keys) so that I can duplicate it in another machine?

Note that SHOW CREATE TABLE and mysqldump do not work in my case because they only create a UNIQUE KEY constraint and not a FOREIGN KEY.


回答1:


mysqldump create the dump of foreign keys as well... it adds syntax like:

mysql> SET foreign_key_checks = 0;
mysql> SOURCE dump_file_name;
mysql> SET foreign_key_checks = 1;

You can read the manual at: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html for mysqldump of foreign keys




回答2:


I come across this often. This is how I do it so I can use 'pv' from the command line to get a progress bar on the sql dump while it restores:

{ echo "SET foreign_key_checks=0;";pv sql.gz; echo "SET foreign_key_checks=1;"; } | mysql dbname

(I put the several commands in { } so that its treated like a single command when being piped to mysql while retaining the progress bar from 'pv')



来源:https://stackoverflow.com/questions/23861645/backup-mysql-schema-with-foreign-key-table-constraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!