I created the following tables in database REGISTRATION:
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
However, when I load STUDENT data into table STUDENT, if the MAJOR is not null or CHE, then it gives the error message:
"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
Question
Why my STUDENT.Major
cannot be any other values in DEPARTMENT.Cprefix
except CHE
? What is wrong? Thanks!