0
CREATE TABLE `class` (
`class_id` int(11) NOT NULL AUTO_INCREMENT,
`section_name` varchar(50) NOT NULL,
`class_alias` varchar(200) NOT NULL,
`grading_scheme` int(11) NOT NULL DEFAULT '0',
`year` year(4) NOT NULL,
`grade_calc_method_id` varchar(20) DEFAULT NULL,
PRIMARY KEY (`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=48819 DEFAULT CHARSET=latin1;
CREATE TABLE `teachers` (
`teacher_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`teacher_subject` varchar(20) NOT NULL DEFAULT 'None',
PRIMARY KEY (`teacher_id`),
KEY `user_id` (`user_id`,`school_id`)
) ENGINE=InnoDB AUTO_INCREMENT=48606 DEFAULT CHARSET=latin1;
CREATE TABLE `teacher_classes` (
`teacher_class_id` int(11) NOT NULL AUTO_INCREMENT,
`teacher_id` int(11) NOT NULL,
`class_id` int(11) NOT NULL,
PRIMARY KEY (`teacher_class_id`),
UNIQUE KEY `teacher_id_class_id` (`teacher_id`,`class_id`),
KEY `teacher_id` (`teacher_id`,`class_id`)
) ENGINE=InnoDB AUTO_INCREMENT=46707 DEFAULT CHARSET=latin1;
嘗試使用外鍵,以確保表之間數據的一致性,使得數據庫管理系統可以檢查errors.I有另一個結表teacher_classes
這裏是我的查詢添加外鍵約束不能添加或更新子行,外鍵約束失敗的MySQL
ALTER TABLE teacher_classes
ADD CONSTRAINT `tc_fk_class_id` FOREIGN KEY (`class_id`)
REFERENCES class (`class_id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
ADD CONSTRAINT `tc_fk_teacher_id` FOREIGN KEY (`teacher_id`)
REFERENCES teachers (`teacher_id`) ON UPDATE NO ACTION ON DELETE NO ACTION;
已經看到了關於這個主題的其他帖子,但沒有運氣,得到以下錯誤。
不能添加或更新子行,外鍵約束失敗 (
DB_NAME
#sql-403_12
,約束tc_fk_teacher_id
外鍵(teacher_id
)參考文獻teachers
(teacher_id
)ON DELETE NO ACTION ON UPDATE NO ACTION)
MySQL是告訴你的是,數據不匹配。表'teachers'中的'teacher_id'與表'teacher_classes'中的id不匹配。 'teacher_classes'中可能有一個id不存在於表'teachers'中 – Sablefoste
我編輯了我的問題 – deadman