当我尝试在表格中插入日期时遇到此错误。这是我的完整错误:
错误1452(23000):无法添加或更新子行:外键约束失败(
university
。course
,CONSTRAINTcourse_ibfk_2
FOREIGN KEY(prerequisite
)参考course
(course_id
))
以下是我的表格:
CREATE TABLE department (
dept_id INT(1) UNIQUE,
dept_name VARCHAR(50) NOT NULL,
budget INT(10) NOT NULL,
PRIMARY KEY (dept_id))ENGINE=INNODB;
CREATE TABLE term (
term_id VARCHAR(50) UNIQUE,
term_desc VARCHAR(50) NOT NULL,
start_date DATE NOT NULL,
end_date DATE NOT NULL,
PRIMARY KEY (term_id))ENGINE=INNODB;
CREATE TABLE major (
major_id VARCHAR(3) UNIQUE,
major_desc VARCHAR(50) NOT NULL,
PRIMARY KEY (major_id))ENGINE=INNODB;
CREATE TABLE location (
room_id INT(2) UNIQUE,
building VARCHAR(50) NOT NULL,
room_no INT(3) NOT NULL,
capacity INT(2) NOT NULL,
room_type VARCHAR(1) NOT NULL,
room_description VARCHAR(50) NOT NULL,
PRIMARY KEY (room_id))ENGINE=INNODB;
CREATE TABLE course
(
course_id VARCHAR(6),
title VARCHAR(30) NOT NULL UNIQUE,
credits INT(1) NOT NULL,
dept_id INT(1) NOT NULL AUTO_INCREMENT,
prerequisite VARCHAR(6),
PRIMARY KEY (course_id),
FOREIGN KEY (dept_id) REFERENCES department (dept_id),
FOREIGN KEY (prerequisite) REFERENCES course (course_id)
)
ENGINE=innodb;
CREATE TABLE faculty
(
faculty_id INT(5),
name VARCHAR(30) NOT NULL,
room_id INT(2),
dept_id INT(1) NOT NULL,
salary DECIMAL(8,2),
PRIMARY KEY (faculty_id),
FOREIGN KEY (room_id) REFERENCES location (room_id),
FOREIGN KEY (dept_id) REFERENCES department (dept_id)
)
ENGINE=innodb;
CREATE TABLE student
(
student_id INT(5),
last_name VARCHAR(30) NOT NULL,
first_name VARCHAR(30) NOT NULL,
street VARCHAR(60),
city VARCHAR(40),
state CHAR(2),
zip VARCHAR(5),
birth_date DATE,
major_id VARCHAR(3),
phone VARCHAR(10),
student_type ENUM('ugrad','grad'),
PRIMARY KEY (student_id),
FOREIGN KEY (major_id) REFERENCES major (major_id)
)
ENGINE=innodb;
CREATE TABLE section
(
section_id INT(4),
course_id VARCHAR(6) NOT NULL,
section_number VARCHAR(2) NOT NULL,
term_id VARCHAR(4) NOT NULL,
faculty_id INT(5),
day VARCHAR(8),
max_count INT(2),
start_time TIME,
end_time TIME,
room_id INT(2),
PRIMARY KEY (section_id),
FOREIGN KEY (course_id) REFERENCES course (course_id),
FOREIGN KEY (term_id) REFERENCES term (term_id),
FOREIGN KEY (faculty_id) REFERENCES faculty (faculty_id),
FOREIGN KEY (room_id) REFERENCES location (room_id)
);
CREATE TABLE registration
(
student_id INT(5),
section_id INT(4),
midterm_grade ENUM('A','B','C','D','F','W'),
final_grade ENUM('A','B','C','D','F','W'),
PRIMARY KEY (student_id, section_id),
FOREIGN KEY (student_id) REFERENCES student (student_id),
FOREIGN KEY (section_id) REFERENCES section (section_id)
)
ENGINE=innodb;
当我尝试将我的值输入到表格课程,部分和注册中时,我收到错误,而我似乎无法弄清楚如何更正输入值。以下是我尝试插入课程,部分和注册表中的几个输入值:
INSERT INTO course (course_id, title, credits, dept_id, prerequisite)
VALUES ('AC101', 'Accounting', '3', '3', 'EN100')
INSERT INTO section (section_id, course_id, section, term_id, faculty_id, day, max_count, start_time, end_time, room_id)
VALUES ('1101', 'CIS265', '01', 'WN13', '63395', 'MW', '30', '09:00', '10:30', '13')
INSERT INTO registration (student_id, section_id, midterm_grade, final_grade)
VALUES ('24746', '1102', 'B', 'B')
无论何时我尝试在这些表中输入值,我都会得到ERROR 1452(23000)。有人可以帮我理解我错在哪里或者我应该如何输入我的价值观?
答案 0 :(得分:1)
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`university`.`course`, CONSTRAINT `course_ibfk_2`
FOREIGN KEY (`prerequisite`) REFERENCES `course` (`course_id`))
此错误表示放入prerequisite
表的course
列的值与course
表中的行不匹配。
在添加course_id='EN100'
行之前,表格中必须包含prerequisite='EN100'
的行。
插入以下行:
INSERT INTO department (dept_id, dept_name, budget) VALUES
('1', 'Language Arts', NULL);
INSERT INTO course (course_id, title, credits, dept_id, prerequisite) VALUES
('EN100', 'English Composition', '3', '1', NULL) ;
INSERT INTO department (dept_id, dept_name, budget) VALUES
('3', 'Business', NULL);
在我们插入此行之前:
INSERT INTO course (course_id, title, credits, dept_id, prerequisite) VALUES
('AC101', 'Accounting', '3', '3', 'EN100') ;