【MySQL】解决MySQL删除外键(foreign key)时报错问题
一、现象
(1) 查看主表class_info和从表student。
MariaDB [class_info]> desc class_info;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| class_id | int(2) | NO | PRI | 0 | |
| class_name | varchar(10) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MariaDB [class_info]> desc student;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| stu_id | int(2) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| class_id | int(2) | YES | | NULL | |
| score | float(4,2) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
(2) 在从表student上创建外键
MariaDB [class_info]> alter table student add foreign key (class_id) references class_info(class_id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [