![](/img/trans.png)
[英]Foreign Key SQL: How can I reference two attributes in one table to a composite primary key that contains 3 columns in the other table?
[英]How can I reference multiple foreign key in one table to a single primary key in ms sql
我试过这个查询,但它不起作用。 说我必须使用 ON DELETE NO ACTION,但我需要为已删除的引用设置 null
CREATE TABLE airports
(
id integer primary key identity(1,1),
country varchar(50) not null,
city varchar(50) not null,
airport_name varchar(50) not null,
airport_class varchar(50) not null
);
CREATE TABLE flights
(
id integer primary key identity(1,1),
code integer unique not null,
departure_point integer,
arrival_point integer,
departure_time datetime not null,
arrival_time datetime not null,
foreign key(departure_point) references airports(id) on delete set null,
foreign key(arrival_point) references airports(id) on delete set null
);
错误(它是俄语,抱歉)
Введение ограничения внешнего ключа (FOREIGN KEY) "FK__flights__arrival__286302EC" для таблицы "flights" может привести к появлению циклов или множественных каскадных путей. Укажите ON DELETE NO ACTION или ON UPDATE NO ACTION либо измените другие ограничения внешнего ключа (FOREIGN KEY).
这是一个有点长的评论。
我认为这在 SQL 服务器中是不可能的。 不幸的是,SQL 服务器只允许每个外部表引用一set null
。
也就是说,您可能还有其他选择:
NULL
机场的flights
是否真的有用。 您可能想要删除整行。NULL
值或无效的外键引用。如果我创建具有两列的单独表,它会起作用:一个是主表,一个外键引用一个航班,另一个是常规外键引用一个机场。
这是我的数据库图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.