[英]“cannot add foreign key constraint”
First i created this table name DEPARTMENT and afterwards i created Dept_Locations which has the foreign key reference in this table at Dnumber. 首先,我创建了该表名称DEPARTMENT,然后创建了Dept_Locations,该表在Dnumber的表中具有外键引用。
create table DEPARTMENT(
Dname varchar(20) NOT NULL,
Dnumber int NOT NULL,
Mgr_ssn char(9) NOT NULL,
Mgr_start_date date NOT NULL,
Primary key(Dnumber),
Unique(Dname)
);
Code for Dept_Locations table: Dept_Locations表的代码:
create table Dept_Locations(
Dnum char(4),
Dlocation varchar(16),
primary key(Dnum,Dlocation),
foreign key(Dnum) references DEPARTMENT(Dnumber));
Now when i execute this code for creating Dept_Locations I get an error: 现在,当我执行此代码以创建Dept_Locations时,出现错误:
Cannot add foreign key constraint. 无法添加外键约束。
The foreign key must be the same data type as the column it's referring to. 外键必须与其引用的列具有相同的数据类型。
The column Dnumber
in DEPARTMENT is int
, and the column Dnum
is defined as char(4)
- but it must be defined as int
. DEPARTMENT中的Dnumber
int
,并且Dnum
列定义为char(4)
-但必须将其定义为int
。
Think the datatypes differ. 认为数据类型不同。
Dnum char(4)
Dnumber int
You primary key and the column with the foreign key constraint are different sizes - try: 您的主键和具有外键约束的列的大小不同-尝试:
create table DEPARTMENT(
Dname varchar(20) NOT NULL,
Dnumber char(4) NOT NULL, # <-- Note the size change
Mgr_ssn char(9) NOT NULL,
Mgr_start_date date NOT NULL,
Primary key(Dnumber),
Unique(Dname)
);
(or update the other to match) (或更新另一个以匹配)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.