ORA-02266: 表中的唯一/主键被启用的外键引用

来源:互联网 发布:微信商城源码使用 编辑:程序博客网 时间:2024/06/10 01:17

如果一个表,它的主键是其他表的外键关联的,那么这个表在truncate的时候会报错


SQL> create table gw1(id int primary key);



Table created.


SQL> create table gw2(sid int ,foreign key (sid) references gw1(id));


Table created.


SQL> truncate table gw1;
truncate table gw1
               *
ERROR at line 1:
ORA-02266: 表中的唯一/主键被启用的外键引用


但是可以delete,如果非要truncate,可以先取消外键。


SQL> delete from gw1;


0 rows deleted.


Elapsed: 00:00:00.00


SQL> select constraint_name,constraint_type from user_constraints where table_name='GW2';


CONSTRAINT_NAME                C
------------------------------ -
SYS_C0036183                   R


Elapsed: 00:00:00.05


SQL> alter table gw2 drop constraint SYS_C0036183;


Table altered.


Elapsed: 00:00:00.01


SQL> truncate table gw1;


Table truncated.
0 0