刪除或清空具有外來鍵約束的表資料包-ERROR 1701 (42000)
OS: centos 6.3
DB:5.5.14
mysql> select database();
+------------+
| database() |
+------------+
| sakila |
+------------+
1 row in set (0.00 sec)
mysql> delete from actor;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`sakila`.`film_actor`, CONSTRAINT `fk_film_actor_actor` FOREIGN KEY (`actor_id`) REFERENCES `actor` (`actor_id`) ON UPDATE CASCADE)
mysql> truncate table actor;
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`sakila`.`film_actor`, CONSTRAINT `fk_film_actor_actor` FOREIGN KEY (`actor_id`) REFERENCES `sakila`.`actor` (`actor_id`))
mysql> show index from actor;
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| actor | 0 | PRIMARY | 1 | actor_id | A | 200 | NULL | NULL | | BTREE | | |
| actor | 1 | idx_actor_last_name | 1 | last_name | A | 200 | NULL | NULL | | BTREE | | |
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
mysql> set foreign_key_checks=0;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from actor;
Query OK, 200 rows affected (0.01 sec)
mysql> set foreign_key_checks=1;
Query OK, 0 rows affected (0.00 sec)
DB:5.5.14
mysql> select database();
+------------+
| database() |
+------------+
| sakila |
+------------+
1 row in set (0.00 sec)
mysql> delete from actor;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`sakila`.`film_actor`, CONSTRAINT `fk_film_actor_actor` FOREIGN KEY (`actor_id`) REFERENCES `actor` (`actor_id`) ON UPDATE CASCADE)
mysql> truncate table actor;
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`sakila`.`film_actor`, CONSTRAINT `fk_film_actor_actor` FOREIGN KEY (`actor_id`) REFERENCES `sakila`.`actor` (`actor_id`))
mysql> show index from actor;
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| actor | 0 | PRIMARY | 1 | actor_id | A | 200 | NULL | NULL | | BTREE | | |
| actor | 1 | idx_actor_last_name | 1 | last_name | A | 200 | NULL | NULL | | BTREE | | |
+-------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
mysql> set foreign_key_checks=0;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from actor;
Query OK, 200 rows affected (0.01 sec)
mysql> set foreign_key_checks=1;
Query OK, 0 rows affected (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28939273/viewspace-1249694/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql 刪除老是報外來鍵約束MySql
- 資料完整性約束:主鍵、外來鍵、各種約束的建立刪除語句
- 解決無法刪除表,提示被外來鍵約束引用
- 生成指令碼,得到所有表的外來鍵約束,然後刪除並重建這些約束指令碼
- 批量刪除MSSQL 中主外來鍵約束SQL
- 解決資料庫的索引亂碼問題,先刪除外來鍵約束,再刪除主鍵約束及其索引資料庫索引
- Javaweb-約束-外來鍵約束JavaWeb
- Mysql-基本練習(06-唯一約束、外來鍵約束、新增、刪除單列)MySql
- Oracle定義約束 外來鍵約束Oracle
- 【Foreign Key】Oracle外來鍵約束三種刪除行為Oracle
- mysql中外來鍵約束級聯更新與刪除MySql
- SQL的主鍵和外來鍵約束SQL
- [求助][資料庫]表間約束的刪除完整性?資料庫
- mysql資料庫匯入外來鍵約束問題MySql資料庫
- mysql~資料完整性考慮~外來鍵約束MySql
- oracle外來鍵約束的總結Oracle
- SQL刪除資料庫裡所有表的外來鍵,同時刪除所有使用者表SQL資料庫
- 修改外來鍵為validate時需要驗證資料是否符合外來鍵約束
- 資料庫約束 主鍵-唯一性-Check-外來鍵資料庫
- 快速刪除有外來鍵關聯的資料庫的資料資料庫
- 教你mysql如何增加外來鍵約束MySql
- MySQL禁用恢復外來鍵約束MySql
- 利用虛擬列實現虛擬刪除的主外來鍵約束
- 分散式資料庫環境中,外來鍵約束的問題??分散式資料庫
- SQL外來鍵約束的含義及建立SQL
- Oracle外來鍵約束中NULL的處理OracleNull
- 查詢(看)表的主鍵、外來鍵、唯一性約束和索引索引
- 新增/刪除約束(Oracle)Oracle
- (轉)oracle資料庫中所有外來鍵約束失效SQL語句Oracle資料庫SQL
- [資料庫]資料庫中為什麼不推薦使用外來鍵約束資料庫
- ORACLE: 查詢(看)表的主鍵、外來鍵、唯一性約束和索引Oracle索引
- SQL SERVER中找出拙劣的約束,索引,外來鍵SQLServer索引
- 【實驗】【外來鍵】小議外來鍵約束對應用程式的影響
- MySQL——表的約束,資料型別,增刪查改MySql資料型別
- AppBoxFuture(七): 分散式外來鍵約束APP分散式
- mysql不能新增外來鍵約束怎麼辦MySql
- mysql啟動和關閉外來鍵約束MySql
- 詳解外來鍵約束(foreign key)