使用外键创建 table 到下面在 sql 文件中创建的另一个 table
Create table with foreign key to an other table created below in sql file
我的问题是我有两个 table,每个 table 都有一个到另一个 table 的外键。
每次,我执行包含两个 table 的创建的 SQL 文件,它给我一个错误,他没有找到另一个 table。我正在使用 sqlplus 来执行 sql 文件。
这是我尝试使用的 SQL 文件的示例:
create table A(
Age number(3),
name number(3) constraint A_FK references B(name))
/
create table B(
Age number(3) constraint B_FK references A(Age),
name number(3))
即使我颠倒顺序,它也会给出同样的错误。
感谢您的帮助。
失败,因为引用 table 尚不存在。
先创建没有密钥的 table。然后删除一个并使用参考重新创建它。然后删除第二个并使用参考重新创建它。
先创建table,然后添加约束
ALTER TABLE A
ADD FOREIGN KEY (name) REFERENCES B(name);
ALTER TABLE B
ADD FOREIGN KEY (age) REFERENCES A(age);
外键引用的 table 列在创建约束时必须存在。由于在 table 之间有某种循环引用,因此需要分三步执行此操作:
先创建一个没有外键的table
创建第二个 table(带有外键)
最后用alter table
语句将外键添加到第一个table
您还需要为引用的列设置唯一或主键约束,否则会出现错误 ORA-02270: no matching unique or primary key for this column-list
。
create table A(
age number(3) primary key,
name number(3)
);
create table B(
age number(3) constraint B_FK references A(Age),
name number(3) primary key
);
alter table A add constraint A_FK foreign key (name) references B(name);
旁注:我对您的示例结构非常怀疑,但这可能是因为您在问题中过度简化了它。
这是外键循环的问题。一种方法是在 table 创建之后添加所有外键(正如我认为其他答案所建议的那样)。
您也可以在第一个 table:
create table A (
Age number(3) primary key,
name number(3)
);
create table B (
name number(3) primary key,
Age number(3),
constraint B_FK foreign key (age) references A(Age)
);
alter table B add constraint A_FK foreign key (name) references B(name);
Here 是一个 db<>fiddle.
备注:
- 外键应引用主键,因此我也添加了该声明。
- 我建议将主键设为 table 中的第一列。
- 您还可以为 table 之一(即
age number(3) constraint b_fk references a(age)
)定义内联约束。
我的问题是我有两个 table,每个 table 都有一个到另一个 table 的外键。 每次,我执行包含两个 table 的创建的 SQL 文件,它给我一个错误,他没有找到另一个 table。我正在使用 sqlplus 来执行 sql 文件。 这是我尝试使用的 SQL 文件的示例:
create table A(
Age number(3),
name number(3) constraint A_FK references B(name))
/
create table B(
Age number(3) constraint B_FK references A(Age),
name number(3))
即使我颠倒顺序,它也会给出同样的错误。 感谢您的帮助。
失败,因为引用 table 尚不存在。
先创建没有密钥的 table。然后删除一个并使用参考重新创建它。然后删除第二个并使用参考重新创建它。
先创建table,然后添加约束
ALTER TABLE A
ADD FOREIGN KEY (name) REFERENCES B(name);
ALTER TABLE B
ADD FOREIGN KEY (age) REFERENCES A(age);
外键引用的 table 列在创建约束时必须存在。由于在 table 之间有某种循环引用,因此需要分三步执行此操作:
先创建一个没有外键的table
创建第二个 table(带有外键)
最后用
alter table
语句将外键添加到第一个table
您还需要为引用的列设置唯一或主键约束,否则会出现错误 ORA-02270: no matching unique or primary key for this column-list
。
create table A(
age number(3) primary key,
name number(3)
);
create table B(
age number(3) constraint B_FK references A(Age),
name number(3) primary key
);
alter table A add constraint A_FK foreign key (name) references B(name);
旁注:我对您的示例结构非常怀疑,但这可能是因为您在问题中过度简化了它。
这是外键循环的问题。一种方法是在 table 创建之后添加所有外键(正如我认为其他答案所建议的那样)。
您也可以在第一个 table:
create table A (
Age number(3) primary key,
name number(3)
);
create table B (
name number(3) primary key,
Age number(3),
constraint B_FK foreign key (age) references A(Age)
);
alter table B add constraint A_FK foreign key (name) references B(name);
Here 是一个 db<>fiddle.
备注:
- 外键应引用主键,因此我也添加了该声明。
- 我建议将主键设为 table 中的第一列。
- 您还可以为 table 之一(即
age number(3) constraint b_fk references a(age)
)定义内联约束。