创建两个外键相互引用的表
Create two tables whose foreign keys reference each other
在 Oracle SQL 中,SQL 开发人员:我正在尝试创建两个 table,每个都有一个引用另一个 table 的外键首要的关键。
按照我的逻辑,我无法设置外键引用,因为另一个 table 还不存在。
以下是我构建它的大致思路:
CREATE TABLE table1
(
column1 datatype PRIMARY KEY NOT NULL,
column2 datatype NOT NULL,
column3 datatype NOT NULL,
CONSTRAINT fk_keyname
FOREIGN KEY (colmn3)
REFERENCES otherTable (column3)
);
CREATE TABLE table2
(
column1 datatype PRIMARY KEY NOT NULL,
column2 datatype NOT NULL,
column3 datatype NOT NULL,
CONSTRAINT fk_keyname2
FOREIGN KEY (colmn3)
REFERENCES otherTable2 (column3)
);
我收到一个错误
ORA-00942: table or view does not exist
我之前通过先创建父 table 解决了这个问题,但是由于它们都相互引用,所以我不知道我需要在这里做什么,因为在这个特定的地方必须相互引用案例.
从技术角度来看,可以创建第一个 table 没有外键约束,然后创建第二个 table (有外键),最后添加外键第一个 table.
的键约束
但是您遇到的问题确实表明 设计问题。它还预示了当您尝试填充 tables 时将要处理的问题:由于交叉外键,您将无法在 tables 中添加 INSERT
记录,除非你做了一些复杂的事情,比如暂时禁用一个约束,插入两个 tables,更新第一个 table,然后启用约束。
你的问题没有提供足够的上下文让我提供替代设计的建议,但根据经验,肯定存在更好的解决方案。它可能涉及拥有一个 table 而不是两个(如果您正在处理 1-1 关系),或者拥有第三个 table 充当桥梁 table 如果这是一个N-M关系。
您可以先创建表,然后再创建 FK。例如:
create table table1 (
column1 int primary key not null,
column2 int not null,
column3 int not null
);
create table table2 (
column1 int primary key not null,
column2 int not null,
column3 int not null,
constraint fk2
foreign key (column3)
references table1 (column1)
);
alter table table1 add constraint fk1
foreign key (column3)
references table1 (column1);
即使将创建表,您也无法在其中插入数据,因为约束会阻止您创建不指向其他 [不存在] 行的行。为了插入数据,您需要将约束创建为 "deferrable"。这是 改进的 SQL 脚本:
create table table1 (
column1 int primary key not null,
column2 int not null,
column3 int not null
);
create table table2 (
column1 int primary key not null,
column2 int not null,
column3 int not null,
constraint fk2
foreign key (column3)
references table1 (column1) deferrable initially deferred
);
alter table table1 add constraint fk1
foreign key (column3)
references table1 (column1) deferrable initially deferred;
现在,确保在事务边界之间插入所有涉及的表的行。现在将仅在事务结束时检查约束,而不是在每个 inserted/modified/deleted 行上检查。
在 Oracle SQL 中,SQL 开发人员:我正在尝试创建两个 table,每个都有一个引用另一个 table 的外键首要的关键。
按照我的逻辑,我无法设置外键引用,因为另一个 table 还不存在。
以下是我构建它的大致思路:
CREATE TABLE table1
(
column1 datatype PRIMARY KEY NOT NULL,
column2 datatype NOT NULL,
column3 datatype NOT NULL,
CONSTRAINT fk_keyname
FOREIGN KEY (colmn3)
REFERENCES otherTable (column3)
);
CREATE TABLE table2
(
column1 datatype PRIMARY KEY NOT NULL,
column2 datatype NOT NULL,
column3 datatype NOT NULL,
CONSTRAINT fk_keyname2
FOREIGN KEY (colmn3)
REFERENCES otherTable2 (column3)
);
我收到一个错误
ORA-00942: table or view does not exist
我之前通过先创建父 table 解决了这个问题,但是由于它们都相互引用,所以我不知道我需要在这里做什么,因为在这个特定的地方必须相互引用案例.
从技术角度来看,可以创建第一个 table 没有外键约束,然后创建第二个 table (有外键),最后添加外键第一个 table.
的键约束但是您遇到的问题确实表明 设计问题。它还预示了当您尝试填充 tables 时将要处理的问题:由于交叉外键,您将无法在 tables 中添加 INSERT
记录,除非你做了一些复杂的事情,比如暂时禁用一个约束,插入两个 tables,更新第一个 table,然后启用约束。
你的问题没有提供足够的上下文让我提供替代设计的建议,但根据经验,肯定存在更好的解决方案。它可能涉及拥有一个 table 而不是两个(如果您正在处理 1-1 关系),或者拥有第三个 table 充当桥梁 table 如果这是一个N-M关系。
您可以先创建表,然后再创建 FK。例如:
create table table1 (
column1 int primary key not null,
column2 int not null,
column3 int not null
);
create table table2 (
column1 int primary key not null,
column2 int not null,
column3 int not null,
constraint fk2
foreign key (column3)
references table1 (column1)
);
alter table table1 add constraint fk1
foreign key (column3)
references table1 (column1);
即使将创建表,您也无法在其中插入数据,因为约束会阻止您创建不指向其他 [不存在] 行的行。为了插入数据,您需要将约束创建为 "deferrable"。这是 改进的 SQL 脚本:
create table table1 (
column1 int primary key not null,
column2 int not null,
column3 int not null
);
create table table2 (
column1 int primary key not null,
column2 int not null,
column3 int not null,
constraint fk2
foreign key (column3)
references table1 (column1) deferrable initially deferred
);
alter table table1 add constraint fk1
foreign key (column3)
references table1 (column1) deferrable initially deferred;
现在,确保在事务边界之间插入所有涉及的表的行。现在将仅在事务结束时检查约束,而不是在每个 inserted/modified/deleted 行上检查。