简体   繁体   English

无法将外键添加到mysql数据库

[英]Unable to add foreign keys to mysql database

I am trying to build the below schema: 我正在尝试构建以下架构:

! 架构

When I try to add these foreign keys mentioned below after inserting data into the tables, I get an error saying "ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails" : 当我在将数据插入表中后尝试添加以下提到的这些外键时,出现错误消息:“错误1452(23000):无法添加或更新子行:外键约束失败”:

alter table takes add foreign key (course_id,sec_id,semester,year) references section (course_id,sec_id,semester,year) on update cascade on delete cascade;

alter table prereq add foreign key (prereq_id) references course (course_id) on update cascade on delete cascade;

alter table course add foreign key (dept_name) references department (dept_name) on update cascade on delete cascade;

I dont understand why I could do it for the other fields but not for these. 我不明白为什么我可以在其他领域做到这一点,但对于这些却不行。 engine=innodb is enabled for all tables. 为所有表启用了engine = innodb。

Any suggestions? 有什么建议么?

The tables have the following values: 这些表具有以下值:

    mysql> select * from prereq;                          
+-----------+-----------+
| course_id | prereq_id |
+-----------+-----------+
| BIO667    | BIO304    |
| CIS621    | CIS220    |
| CIS637    | CIS108    |
| CIS637    | CIS220    |
| MAT647    | MAT235    |
+-----------+-----------+

mysql> select * from takes;                           
+-----+-----------+--------+----------+------+-------+
| ID  | course_id | sec_id | semester | year | grade |
+-----+-----------+--------+----------+------+-------+
| 100 | CIS621    | 010    | Fall     | 2012 | B+    |
| 100 | CIS637    | 010    | Fall     | 2011 | A     |
| 104 | CIS621    | 010    | Fall     | 2012 | B+    |
| 104 | CIS637    | 010    | Fall     | 2012 | B-    |
| 206 | BIO667    | 1      | Spring   | 2012 | A-    |
| 476 | MAT647    | 010    | Spring   | 2011 | B     |
+-----+-----------+--------+----------+------+-------+

mysql> select * from course;
+-----------+--------------+-----------+---------+
| course_id | title        | dept_name | credits |
+-----------+--------------+-----------+---------+
| BIO667    | Gene Theory  | BIOTECH   |       4 |
| CIS621    | Algorithms   | CIS       |       3 |
| CIS637    | Database     | CIS       |       3 |
| MAT647    | Calculus - I | MATH      |       3 |
+-----------+--------------+-----------+---------+


mysql> select * from section;
+-----------+--------+----------+------+----------+---------+--------------+
| course_id | sec_id | semester | year | building | room_no | time_slot_id |
+-----------+--------+----------+------+----------+---------+--------------+
| BIO667    | 1      | Spring   | 2012 | Brown    |     116 |            2 |
| CIS621    | 010    | Fall     | 2012 | Gore     |     114 |            1 |
| CIS637    | 010    | Fall     | 2011 | Smith    |     102 |            3 |
| MAT647    | 010    | Spring   | 2011 | Memorial |     126 |            4 |
+-----------+--------+----------+------+----------+---------+--------------+

In table takes this row takes这一行

| 104 | CIS637    | 010    | Fall     | 2012 | B-    |

has no match for all its keys in section (2012 is missing) section中的所有键均不匹配(缺少2012年)

Please check all your keys carefully. 请仔细检查所有按键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM