mysql参照表和被参照表是本身问题:Cannot add or update a child row: a foreign key constraint fails
建表:CourseCREATETABLECourse(CnoChar(4)PRIMARYKEY,CnameChar(40),CpnoChar(4),CcreditInt,...
建表:
Course
CREATE TABLE Course (
Cno Char(4) PRIMARY KEY,
Cname Char(40),
Cpno Char(4),
Ccredit Int,
FOREIGN KEY (Cpno) REFERENCES Course(Cno)
);
插入数据:
INSERT INTO Course VALUES('2', ' 数学', '',2);
INSERT INTO Course VALUES('1','数据库','5',4);
INSERT INTO Course VALUES('3', 信息系统', '1',4);
INSERT INTO Course VALUES('4', ' 操作系统', '6',3);
INSERT INTO Course VALUES('5', ' 数据结构', '7',4);
INSERT INTO Course VALUES('6', '数据处理', '',2);
INSERT INTO Course VALUES('7', ' PASCAL语言', '6',4);
就会因为因外键Cpno和主键Cno顺序问题出错,怎么解决~
谢谢……………… 展开
Course
CREATE TABLE Course (
Cno Char(4) PRIMARY KEY,
Cname Char(40),
Cpno Char(4),
Ccredit Int,
FOREIGN KEY (Cpno) REFERENCES Course(Cno)
);
插入数据:
INSERT INTO Course VALUES('2', ' 数学', '',2);
INSERT INTO Course VALUES('1','数据库','5',4);
INSERT INTO Course VALUES('3', 信息系统', '1',4);
INSERT INTO Course VALUES('4', ' 操作系统', '6',3);
INSERT INTO Course VALUES('5', ' 数据结构', '7',4);
INSERT INTO Course VALUES('6', '数据处理', '',2);
INSERT INTO Course VALUES('7', ' PASCAL语言', '6',4);
就会因为因外键Cpno和主键Cno顺序问题出错,怎么解决~
谢谢……………… 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询