ORA-02266: unique/primary keys in table referenced by enabled foreign keys

简介: 在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误SQL >TRUNCATE TABLE p;ORA-02266: unique/primary keys in table referenced by enabled foreign keys有时候对应的中文错误提示为:ORA-02266: 表中的唯一/主键被启用的外部关键字引用,一般出现这个错误,是因为表中的主键被其它表的外键所引用,导致删除数据时出错。

在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误

SQL >TRUNCATE TABLE p;

ORA-02266: unique/primary keys in table referenced by enabled foreign keys

有时候对应的中文错误提示为:ORA-02266: 表中的唯一/主键被启用的外部关键字引用,一般出现这个错误,是因为表中的主键被其它表的外键所引用,导致删除数据时出错。


此时,你可以通过下面脚本查看一下涉及该表主键的外键约束信息。

-查询主外键关系
SQL> col 主键表名 for a20
SQL> col 主键列名 for a20
SQL> col 外键表名 for a20
SQL> col 外键列名  for a20
SQL> select b.table_name  主键表名,
        b.column_name 主键列名,
        a.table_name  外键表名,
        a.column_name 外键列名
   from ( select a.constraint_name,
                b.table_name,
                b.column_name,
                a.r_constraint_name
           from dba_constraints a, dba_cons_columns b
          WHERE a.constraint_type = 'R'
            and a.constraint_name = b.constraint_name) a,
        ( select distinct a.r_constraint_name, b.table_name, b.column_name
           from dba_constraints a, dba_cons_columns b
          WHERE a.constraint_type = 'R'
            and a.r_constraint_name = b.constraint_name) b
  where a.r_constraint_name = b.r_constraint_name
and b.table_name= 'P' ;
 
--处理方法一
SQL> alter table p disable primary key cascade ;
 
Table altered.
 
SQL> truncate table p;
 
Table truncated.
 
SQL> alter table p enable primary key ;
 
Table altered.
 
--处理方法二
SQL> delete from t;
 
1 row deleted.
 
 
SQL> commit ;
 
Commit complete.

注意事项在ENABLE主键后不会自动恢复外键(没有cascade选项),因此需要手工对引用该键的约束进行ENABLE。

ALTER TABLE P ENABLE VALIDATE CONSTRAINT FK_P;

目录
相关文章
|
关系型数据库 数据库 PostgreSQL
PG备份恢复:multiple primary keys for table "t1" are not allowed
PG备份恢复:multiple primary keys for table "t1" are not allowed
475 0
|
存储 关系型数据库 MySQL
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
4041 0
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
|
数据库
Incorrect table definition; there can be only one auto column and it must be defined as a key
Incorrect table definition; there can be only one auto column and it must be defined as a key
185 0
Incorrect table definition; there can be only one auto column and it must be defined as a key
|
存储 关系型数据库 MySQL
ERROR 1215 (HY000): Cannot add foreign key constraint
ERROR 1215 (HY000): Cannot add foreign key constraint
ERROR 1215 (HY000): Cannot add foreign key constraint
|
数据安全/隐私保护 索引
报错:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin'
报错:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin'
507 0
|
关系型数据库 MySQL 数据库
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
1507 0
|
关系型数据库 MySQL
Mysql - 删除表时出现: Cannot delete or update a parent row: a foreign key constraint fails
Mysql - 删除表时出现: Cannot delete or update a parent row: a foreign key constraint fails
275 0