我在教程中的练习任务上使用了MySQL,但是我在创建一个错误“不正确的约束”表时遇到了问题。MySQL - errno 150:“外键约束不正确”
请看看,并建议我如何解决它。谢谢!
* A当然,产品是在特定的学期 提供的课程*学生将在课程祭,不是课程 *注册讲师被分配到课程祭,不是课程
CREATE TABLE `students` (
`student_id` char(8) NOT NULL,
`max_load` tinyint(4) NOT NULL,
PRIMARY KEY (`student_id`)
)
CREATE TABLE `courses` (
`course_code` char(8) NOT NULL,
`course_title` varchar(100) NOT NULL,
PRIMARY KEY (`course_code`)
)
CREATE TABLE `lecturers` (
`lecturer_id` char(8) NOT NULL,
`lecturer_name` varchar(50) NOT NULL,
PRIMARY KEY (`lecturer_id`)
)
CREATE TABLE `course_offerings` (
`course_code` char(8) NOT NULL,
`semester` tinyint(4) NOT NULL,
`year` year(4) NOT NULL,
`lecturer_id` char(8) NOT NULL,
PRIMARY KEY (`course_code`,`semester`,`year`),
FOREIGN KEY (`course_code`) REFERENCES `courses` (`course_code`),
FOREIGN KEY (`lecturer_id`) REFERENCES `lecturers` (`lecturer_id`)
)
CREATE TABLE `enrolment` (
`student_id` char(8) NOT NULL,
`course_code` char(8) NOT NULL,
`semester` tinyint(4) NOT NULL,
`year` year(4) NOT NULL,
PRIMARY KEY (`student_id`,`course_code`,`semester`,`year`),
FOREIGN KEY (`student_id`) REFERENCES students(`student_id`),
FOREIGN KEY (`course_code`) REFERENCES course_offerings(`course_code`),
FOREIGN KEY (`semester`) REFERENCES course_offerings(`semester`),
FOREIGN KEY (`year`) REFERENCES course_offerings(`year`)
)
非常感谢,我会按照你的建议。 –