gpt4 book ai didi

mysql - 如何引用尚未创建的表?

转载 作者:行者123 更新时间:2023-11-28 23:32:03 26 4
gpt4 key购买 nike

假设我需要创建两个相互引用的表(它们需要约束)。是在没有约束的情况下创建表,然后将它们添加到单独的语句中的唯一方法吗?我记得有些 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 KEYSENABLE KEYS

关于mysql - 如何引用尚未创建的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37054474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com