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)
SQL Server的外键必须引用的是主键或者唯一键(转载)的更多相关文章
- SQL Server学习之路(二):主键和外键
0.目录 1.定义 1.1 什么是主键和外键 1.2 主键和外键的作用 1.3 主键.外键和索引的区别 2.主键(primary key) 2.1 通过SSMS设置主键 2.2 通过SQL语句设置主键 ...
- Sql Server中的游标最好只用于有主键或唯一键的表
游标cursor,我想大多数人都在sql server里面用过.当一个表数据量不太大的时候,游标还是可以用的,毕竟游标是循环一个表中每一行数据的最简便办法.但是如果你用一个游标去循环一个没有主键或唯一 ...
- 图解SQL Server:聚集索引、唯一索引、主键
http://www.cnblogs.com/chenxizhang/archive/2010/01/14/1648042.html
- 何查询SQL Server数据库没有主键的表并增加主键
SQL Server数据库中,如果一个表没有主键,我们该如何查询呢?本文我们主要就介绍了如何查询数据库中没有主键的表名并为其增加主键的方法,希望能够对您有所帮助. 该功能的实现代码如下: declar ...
- sql server 2008 外键关联的设置和取消
直接上图片 选中表右击-设计 找到需要设置外键的字段.右击-关系,在弹出的对话框中点击添加 选择右边的小按钮点击.选择主键表和关联的主键ID,以及外建表的关联字段. 建立外键完成. 删除的话选中某个外 ...
- SQL Server 查看对象之间的引用关系
前期准备: use studioA; go create table T(X int,Y int); insert into T(X,Y) values(1,1),(2,2); ...
- Hibernate 表映射 主键生成策略与复合主键
主要分析三点: 一.数据表和Java类的映射 : 二.单一主键映射和主键的生成策略 : 三.复合主键的表映射 : 一.数据表和Java类的映射 Hibernate封装了数据库DDL语句,只需要将数据 ...
- hibernate 联合主键生成机制(组合主键XML配置方式)
hibernate 联合主键生成机制(组合主键XML配置方式) 如果数据库中用多个字段而不仅仅是一个字段作为主键,也就是联合主键,这个时候就可以使用hibernate提供的联合主键生成策略. 具体 ...
- 根据oracle的主键列生成SQLserver的主键
根据oracle的主键列生成MsSQLServer的主键列 select 'alter table ' || cu.table_name ||' add constraint '||' PK_' ...
随机推荐
- CENTOS安装xwindow
CentOS6安装图形界面 [root@centos6~]# yum -y install xorg* [root@centos6 ~]# yum -y groupinstall "X Wi ...
- XGBoost 引入 - 提升树
认识提升树 这个boosting 跟 Adaboost 不同. Adaboost 是通过上一轮的误差率来动态给定一下轮样本不同的权重来学习不同的模型. 现在的方式, 更多是基于残差 的方式来训练. 一 ...
- Linux shell 中断循环语句
无限循环: 循环有限的生命,他们跳出来,一旦条件是 false 还是 false 取决于循环. 由于所需的条件是不符合一个循环可能永远持续下去.永远不会终止执行一个循环执行无限次数.出于这个原因,这样 ...
- Linux shell if条件判断2
前面介绍linux shell的if判断的语法,现在再补充一点. Linux shell if条件判断1 分支判断结构 if , case 下面两个结构语法,已经在前面有过示例. 结构1: ...
- PHP技术栈
本文旨在给要学习 PHP 的新手一个大概的认知轮廓,在心里有个学习的结构,有的放矢,避免走太多弯路.大神请忽略. 入门阶段 预备知识 1.掌握基本HTML.JS.CSS语法:熟悉 Bootstrap. ...
- 性能测试基础---jmeter参数化、关联、事物、检查的等
·Jmeter脚本增强·性能测试的脚本增强技术:参数化.关联.事务.检查点.思考时间和集合点. ·参数化:在Jmeter中,实现参数化的方式很多.本质上来说,参数化的实现方式有两种:·文件方式:一般建 ...
- HTML JAVASCRIPT CSS 大小写敏感问题
html: 大小写不敏感 css: 大小写不敏感 javascript: 大小写敏感 但是 但是 但是 这三者是相互联系的, 所以合在一起使用的时候就产生了变化 ---- TagName, Clas ...
- 【luoguP1382】楼房
题目描述 离散化,线段树维护区间修改,发现询问都是单点的\(max\),不妨把标记留在点上,不用下传,查询时取个\(max\)就可以了 #include<algorithm> #inclu ...
- 编码格式检测chardet模块
chardet模块: -->检测编码格式 未知编码的bytes,要把它转换成str,就需要知道该bytes的编码方式 #1.直接检测bytes >>> chardet.dete ...
- 安卓设备连接Mac的简单方法
mac设备是苹果出品的桌面系统,以高冷而闻名,不同于我们平常使用的windows系统,mac系统对软件硬件的兼容性很差,将iOS 设备(iPhone.iPad和iPod)连接至Mac是一件很简单的事, ...