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;

目录
相关文章
|
9月前
|
关系型数据库 数据库 PostgreSQL
PG备份恢复:multiple primary keys for table "t1" are not allowed
PG备份恢复:multiple primary keys for table "t1" are not allowed
191 0
|
6月前
|
关系型数据库 MySQL 数据库
解决出现的SQLIntegrityConstraintViolationExceptionw:Duplicate entry ‘10‘ for for key ‘user.PRIMARY‘问题
解决出现的SQLIntegrityConstraintViolationExceptionw:Duplicate entry ‘10‘ for for key ‘user.PRIMARY‘问题
|
数据库
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
121 0
Incorrect table definition; there can be only one auto column and it must be defined as a key
|
SQL 数据库
ON DUPLICATE KEY UPDATE
ON DUPLICATE KEY UPDATE
96 0
ON DUPLICATE KEY UPDATE
|
存储 关系型数据库 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 原因及解决方法
1451 0
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
|
存储 关系型数据库 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
|
关系型数据库 MySQL
MySQL - Duplicate entry for key PRIMARY
MySQL - Duplicate entry for key PRIMARY
261 0
1041 Be Unique (20)
#include #include #include #include #include using namespace std; int main() { int n; cin >> n; ...
744 0