我们可以有一个外键不是其他表中的主键吗? [英] can we have a foreign key which is not a primary key in any other table?

查看:196
本文介绍了我们可以有一个外键不是其他表中的主键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

这篇关于我们可以有一个外键不是其他表中的主键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆