truncate有外键约束的表,报ORA-02266处理。

问题描述:当父表有子表的外键约束时,无法直接truncate父表。报ORA-02266: unique/primary keys in table referenced by enabled foreign keys。
这里使用另一种办法来解决这个问题:先把外键失效,truncate表之后再把外键生效。
SQL> truncate table machine_room;
truncate table machine_room
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys

1.查询外键约束

SQL> select TABLE_NAME,CONSTRAINT_NAME,CONSTRAINT_TYPE,R_CONSTRAINT_NAME from ALL_CONSTRAINTS WHERE constraint_type='R' and owner='UML';

TABLE_NAME CONSTRAINT_NAME R_CONSTRAINT_NAME
------------------------------ ------------------------------ ------------------------------
TEAM FK_T_SCHOOL01 PK_T_SCHOOL
TEAM_MESSAGE FK_T_TEAM04 PK_T_TEAM
TEAM_MESSAGE FK_T_SCHOOL0 PK_T_SCHOOL
RACE_SEAT FK_T_MACHINE_ROOM02 PK_T_MACHINE
RACE_SEAT FK_T_TEAM02 PK_T_TEAM
RACE_MARK FK_T_TEAM03 PK_T_TEAM

6 rows selected.

2. 失效子表的外键约束 (所有外键)
SQL> ALTER TABLE race_seat DISABLE CONSTRAINT fk_t_machine_room02 ;

Table altered.

3. 再次执行truncate父表数据的操作
SQL> truncate table machine_room;

Table truncated.

4. 生效外键约束 (刚才所有失效外键)
SQL> ALTER TABLE race_seat enable CONSTRAINT fk_t_machine_room02;

Table altered.

ok,转载请标明出处。

posted on 2016-12-25 13:59  张冲andy  阅读(605)  评论(0编辑  收藏  举报

导航