SQL Server的外键必须引用的是主键或者唯一键(转载)
问:
In SQL Server , I got this error ->
"There are no primary or candidate keys in the referenced table 'BookTitle' that match the referencing column list in the foreign key 'FK_BookCopy_Title__2F10007B'."
I first created a relation called the BookTitle relation.
CREATE TABLE BookTitle ( ISBN CHAR(17) NOT NULL, Title VARCHAR(100) NOT NULL, Author_Name VARCHAR(30) NOT NULL, Publisher VARCHAR(30) NOT NULL, Genre VARCHAR(20) NOT NULL, Language CHAR(3) NOT NULL, PRIMARY KEY (ISBN, Title))
Then I created a relation called the BookCopy relation. This relation needs to reference to the BookTitle relation's primary key, Title.
CREATE TABLE BookCopy ( CopyNumber CHAR(10) NOT NULL, Title VARCHAR(100) NOT NULL, Date_Purchased DATE NOT NULL, Amount DECIMAL(5, 2) NOT NULL, PRIMARY KEY (CopyNumber), FOREIGN KEY (Title) REFERENCES BookTitle(Title))
But I can't create the BookCopy relation because the error stated above appeared.
I really appreciate some useful help.
答:
Foreign keys work by joining a column to a unique key in another table, and that unique key must be defined as some form of unique index, be it the primary key, or some other unique index.
At the moment, the only unique index you have is a compound one on ISBN, Title which is your primary key.
There are a number of options open to you, depending on exactly what BookTitle holds and the relationship of the data within it.
I would hazard a guess that the ISBN is unique for each row in BookTitle. ON the assumption this is the case, then change your primary key to be only on ISBN, and change BookCopy so that instead of Title you have ISBN and join on that.
If you need to keep your primary key as ISBN, Title then you either need to store the ISBN in BookCopy as well as the Title, and foreign key on both columns, OR you need to create a unique index on BookTitle(Title) as a distinct index.
More generally, you need to make sure that the column or columns you have in your REFERENCES clause match exactly a unique index in the parent table: in your case it fails because you do not have a single unique index on Title alone.
所以,SQL Server中,从表的外键必须引用的是主表的主键或者唯一键、唯一索引,否则会报错。
补充:外键的列顺序要和主键/唯一键的列顺序一致
Another thing is - if your keys are very complicated sometimes you need to replace the places of the fields and it helps :
if this dosent work:
foreign key (ISBN, Title) references BookTitle (ISBN, Title)
Then this might work (not for this specific example but in general) :
foreign key (Title,ISBN) references BookTitle (Title,ISBN)
所以如果数据库中从表的外键包含多个列,那么这多列在外键中的顺序,要和主表中引用的主键/唯一键的列顺序一致:
例如,如果从表BookCopy的外键列顺序如下:
BookCopy(ISBN, Title)
那么主表BookTitle的主键/唯一键要声明为同样的列顺序:
BookTitle(ISBN, Title)
而不能声明为:
BookTitle(Title, ISBN)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2010-07-29 Visual Studio中的引用项目和直接引用DLL文件