我们可以有一个不是任何其他表中的主键的外键吗?

每本书都写外键实际上是其他表中的主键,但我们可以有一个不是任何其他表中的主键的外键

it is written in every book that foreign keys are actually primary key in some other table but can we have a foreign key which is not primary key in any other table

推荐答案

是的 - 您可以使用外键引用另一个表中的唯一索引.

Yes - you can have a foreign key that references a unique index in another table.

CREATE UNIQUE INDEX UX01_YourTable ON dbo.YourTable(SomeUniqueColumn)

ALTER TABLE dbo.YourChildTable
   ADD CONSTRAINT FK_ChildTable_Table
   FOREIGN KEY(YourFKColumn) REFERENCES dbo.YourTable(SomeUniqueColumn)

相关文章