假设我需要创建两个相互引用的表(它们需要约束)。是在没有约束的情况下创建表,然后将它们添加到单独的语句中的唯一方法吗?我记得有些 orms 可以自己解决这个问题,但是是否可以仅使用 sql 并在两个语句中执行此操作?
最佳答案
您应该必须首先创建没有外键的表,并在创建两个表后附加它们:
create table t1 (id int not null primary key, id2 int not null);
create table t2 (id int not null primary key, id1 int not null);
alter table t1 add foreign key (id2) references t2(id);
alter table t2 add foreign key (id1) references t1(id);
好消息:模式转储工作完美(so
是我的数据库):
mysqldump -u root so t1 t2 | mysql -u root so
这不会出错,因为 mysqldump
在正确的位置插入了 DISABLE KEYS
和 ENABLE KEYS
。
关于mysql - 如何引用尚未创建的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37054474/