具有外键表约束的备份MySQL模式 [英] Backup MySQL schema with foreign key table constraints
问题描述
我创建了一个由多个表组成的MySQL模式,然后决定使用以下命令为每个表添加外键约束:
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?
请注意,在我的情况下,SHOW CREATE TABLE
和mysqldump不起作用,因为它们仅创建UNIQUE KEY约束,而不创建FOREIGN KEY.
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.
推荐答案
mysqldump也会创建外键转储... 它添加了如下语法:
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;
您可以在以下位置阅读该手册: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html 用于外键的mysqldump
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
这篇关于具有外键表约束的备份MySQL模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!