问题描述:当父表有子表的外键约束时,无法直接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 keys1.查询外键约束
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_SCHOOLTEAM_MESSAGE FK_T_TEAM04 PK_T_TEAMTEAM_MESSAGE FK_T_SCHOOL0 PK_T_SCHOOLRACE_SEAT FK_T_MACHINE_ROOM02 PK_T_MACHINERACE_SEAT FK_T_TEAM02 PK_T_TEAMRACE_MARK FK_T_TEAM03 PK_T_TEAM6 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,转载请标明出处。