mysql> insert into dage(name) values('铜锣湾');
Query OK, 1 row affected (0.01 sec)
mysql> select * from dage;
+----+--------+
| id | name |
+----+--------+
| 1 | 铜锣湾 |
+----+--------+
1 row in set (0.00 sec)
插入个小弟:
mysql> insert into xiaodi(dage_id,name) values(1,'铜锣湾_小弟A');
Query OK, 1 row affected (0.02 sec)
mysql> select * from xiaodi;
+----+---------+--------------+
| id | dage_id | name |
+----+---------+--------------+
| 1 | 1 | 铜锣湾_小弟A |
+----+---------+--------------+
把大哥删除:
mysql> delete from dage where id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`bstar/xiaodi`, CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`))
提示:不行呀,有约束的,大哥下面还有小弟,可不能扔下我们不管呀!
插入一个新的小弟:
mysql> insert into xiaodi(dage_id,name) values(2,'旺角_小弟A');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`bstar/xiaodi`, CONSTRAINT `xiaodi_ibfk_1` FOREIGN KEY (`dage_id`) REFERENCES `dage` (`id`))
mysql> delete from dage where id=1;
Query OK, 1 row affected (0.01 sec)
mysql> select * from dage;
Empty set (0.01 sec)
mysql> select * from xiaodi;
Empty set (0.00 sec)