简体   繁体   English

具有外键表约束的备份MySQL模式

[英]Backup MySQL schema with foreign key table constraints

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: 我创建了由多个表组成的MySQL模式,然后决定使用以下命令为每个表添加外键约束:

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. 注意, SHOW CREATE TABLE和mysqldump在我的情况下不起作用,因为它们仅创建UNIQUE KEY约束而不创建FOREIGN KEY。

mysqldump create the dump of foreign keys as well... it adds syntax like: mysqldump也会创建外键转储...它添加了如下语法:

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 您可以在以下网址阅读该手册: http : //dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html以获得mysqldump的外键

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: 这是我的操作方式,因此我可以从命令行使用'pv'在还原时恢复sql dump上的进度条:

{ 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' ) (我将几个命令放在{ }以便在将其通过管道传输到mysql同时将其视为单个命令,同时保留了'pv'的进度条)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM