mysql插入的查询有外部约束错误(表ok)
我在数据库注册中创建了以下表:mysql插入的查询有外部约束错误(表ok),mysql,constraints,Mysql,Constraints,我在数据库注册中创建了以下表: CREATE TABLE COLLEGE (Cnumber INT(3) not null, Cname VARCHAR(30) not null, Primary key (Cnumber) ); CREATE TABLE DEPARTMENT (Dnumber INT(5) not null,
CREATE TABLE COLLEGE
(Cnumber INT(3) not null,
Cname VARCHAR(30) not null,
Primary key (Cnumber)
);
CREATE TABLE DEPARTMENT
(Dnumber INT(5) not null,
Dname varchar(30) not null,
Cnumber INT(3),
Cprefix varchar(4) not null,
Primary key (Dnumber),
Unique key (Dname),
Unique key (Cprefix),
Foreign key (Cnumber) references COLLEGE (Cnumber)
ON DELETE SET NULL ON UPDATE CASCADE);
CREATE TABLE STUDENT
(Snumber INT(9) not null,
Fname VARCHAR(20) not null,
Mname VARCHAR(20),
Lname VARCHAR(20) not null,
Major varchar(4),
Pwd INT(6) not null,
Stype VARCHAR(6) check (Stype in (‘UGRAD’, ‘GRAD’, ‘PHD’)),
Primary key (Snumber),
Foreign key (Major) references DEPARTMENT (Cprefix)
ON DELETE SET NULL ON UPDATE CASCADE);
And load data into MySQL no problem for DEPARTMENT:
00001 Accounting 012 ACC
00002 Microbiology 001 MCB
00003 Animal Sciences 001 AMS
00004 Plant Sciences 001 PLS
00005 Landscape Ecology 002 LSE
00006 Heritage Conservation 002 HTC
00007 Early Childhood Education 003 ELC
00008 Special Education 003 SPE
00009 Electrical and Computer Engineering 004 ECE
00010 Civil Engineering 004 CVE
00011 Chemical Engineering 004 CME
00012 Dance 005 DAN
00013 Music 005 MUS
00014 Spanish 006 SPA
00015 German Studies 006 GER
00016 Nursing 008 NUR
00017 Nurse Practitioner 008 NSP
00018 Math 009 MAT
00019 Physics 009 PHY
00020 Chemistry 009 CHE
00021 Pharmaceutical Sciences 010 PMS
00022 Pharmacology 010 PMC
00023 Communication 011 COM
00024 History 011 HIS
00025 Linguistics 011 LIN
00026 Philosophy 011 PHI
00027 Finance 012 FIN
但是,当我将学生数据加载到STUDENT表中时,如果MAJOR不是null或CHE,则会给出错误消息:
"ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails ('registration'.'student', CONSTRAINT 'student_ibfk_1' FOREIGN KEY ('Major') references 'DEPARTMENT' ('Cprefix') ON DELETE SET NULL ON UPDATE CASCADE)
111111111 John Sam Davison CHE 123456 UGRAD
222222222 Jacob Andy Oram CHE 112233 GRAD
333333333 Ashish Fu Bagai CHE 123123 UGRAD
444444444 Joe Happy Harris CHE 321321 UGRAD
555555555 Andy Cyplex Blignau CHE 654321 PHD
666666666 Pommie Meggie Mbangwa CHE 135246 GARD
777777777 Ian Pei Healy CHE 333444 UGRAD
888888888 Dougie Max Marilli CHE 444555 PHD
999999999 Diana Daisy Cedeno CHE 321321 GRAD
问题:
为什么我的学生。专业不能是系里的任何其他价值观。Cprefix除了CHE?怎么了?谢谢 如评论中所述,错误是告诉您试图向学生添加/更新一行,但该行没有该系的有效值
"select Cprefix from department where Dnumber=12" is empty.
你希望我们能读到吗?我想连你都看不懂。请格式化问题!对不起,这是我第一次使用它。我想知道如何格式化……对不起……哈利,你现在能读了吗?我不知道我在编辑方面做了些什么,但现在它看起来格式化了,可读性好。谢谢。错误告诉您,您正在尝试向学生添加/更新一行,该行对于部门无效。此问题可能重复