我无法弄清楚为什么这不是编译它与外键有某种关系:
Drop Table Employee;
Drop Table Department;
Create Table Employee(
EmpNr int not null primary key,
EmpName Varchar(35) not null,
Dept Varchar (2) not null,
Gender char not null
);
Create Table Department(
DeptCode Varchar (2) not null primary key,
DeptName Varchar (35) not null,
Foreign Key (DeptCode) references Employee (Dept)
);
insert into Employee values (001, 'HagarT','DV','M'),
(002, 'WongS','DV','F'),
(003, 'Jones','MK','F'),
(004, 'MifuneK','SL','M');
insert into Department values ('DV', 'Development'),
('MK', 'Marketing'),
('RS', 'Research'),
('SL', 'Sales');
答案 0 :(得分:0)
您需要Dept
PRIMARY KEY
或UNIQUE
作为FK
Create Table Employee(
EmpNr int not null primary key,
EmpName Varchar(35) not null,
Dept Varchar (2) unique not null,
Gender char not null
);
正如您所看到的,Employee
有两行'DV'
,因此对于Department
来说是陌生的?
我想你想要的是
Create Table Employee(
EmpNr int not null primary key,
EmpName Varchar(35) not null,
Dept Varchar (2) not null,
Gender char not null,
Foreign Key (Dept) references Department (DeptCode)
);