现在的位置: 首页 > 综合 > 正文

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

2013年08月16日 ⁄ 综合 ⁄ 共 1128字 ⁄ 字号 评论关闭

在删除有父子表关系的表的数据时,我们都知道要先删除子表 再删除父表数据;或者先取消外键 然后再删除。

昨天采用先删子表 ,再删父表,删除时使用truncate,结果在删除父表的时候,给出了这个提示:

在行 1 上开始执行命令时出错:
truncate table  ep_point
错误报告:
SQL 错误: ORA-02266: 表中的唯一/主键被启用的外键引用
02266. 00000 -  "unique/primary keys in table referenced by enabled foreign keys"
*Cause:    An attempt was made to truncate a table with unique or
           primary keys referenced by foreign keys enabled in another table.
           Other operations not allowed are dropping/truncating a partition of a
           partitioned table or an ALTER TABLE EXCHANGE PARTITION.
*Action:   Before performing the above operations the table, disable the
           foreign key constraints in other tables. You can see what
           constraints are referencing a table by issuing the following
           command:
           SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = "tabnam";

”。

而改成delete则删除成功。

网上给出的解释是ddl与dml 的区别(because truncate isn't going to verify the constraint, truncate is ddl.)。

 

解决办法:

SQL> alter table ep_pointdisable primary key cascade;          
表已更改。

SQL> truncate table ep_point;
表已截掉。

SQL> alter table ep_point enable primary key;
表已更改。

SQL>ALTER TABLE '子表' ENABLE CONSTRAINT '外键约束名';

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

 

由于此解决方法会disable掉关联子表的外键,所以慎用。

 

抱歉!评论已关闭.