一个外键可以引用另一个外键吗

Can a foreign key reference another foreign key

是否可以有一个外键引用不同 table 中的另一个外键,或者它是否可以只引用主键和唯一键?

外键可以引用定义为唯一的任何字段。如果该唯一字段本身被定义为外键,则没有区别。外键只是为了执行参照完整性。使字段成为外键不会以任何方式更改字段本身。如果是唯一字段,也可以是另一个FK的目标。

例如:

create table Table1(
     PK int identity primary key,
     ...
);
create table Table2( -- 1-1 relationship with Table1
     PKFK int primary key,
     ...,
     constraint FK_Table2_1 foreign key( PKFK ) references Table1( PK )
);
create table Table3( -- relates to Table2
    PKFKFK int primary key,
    ...,
     constraint FK_Table3_2 foreign key( PKFKFK ) references Table2( PKFK )
);

据我所知,没有 DBMS 不是这种情况。并且我同意Horse的做法,没有错。

Is it possible to have a foreign key that references another foreign key in a different table

是的。事实上,与公认的答案相反,引用的 FK 列甚至不必是唯一的! - 至少在 MySQL。请参阅 https://www.db-fiddle.com/f/6RUEP43vYVkyK2sxQQpBfj/0 以获取相同的演示。

这提出了一个问题,如果 FK 在父行中不是唯一的 table,那么谁是父行? FKs的目的是建立亲子关系