不少人在遇到这个情况下会找到这么样的一个sql脚本:查询外键
查找表的外键(包括名称,引用表的表名和对应的键名,下面是分成多步查询): select * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查询的表 查询外键约束的列名: select * from user_cons_columns cl where cl.constraint_name = 外键名称 查询引用表的键的列名: select * from user_cons_columns cl where cl.constraint_name = 外键引用表的键名
那么这时候解决的办法:
1,不要用truncate table 改用delete
2,试试这个:禁用外键关系
①执行脚本,禁用外键
BEGIN for c in (select 'ALTER TABLE '||TABLE_NAME||' DISABLE CONSTRAINT '||constraint_name||' ' as v_sql from user_constraints where CONSTRAINT_TYPE='R') loop EXECUTE IMMEDIATE c.v_sql; end loop; end;
begin for ec in (select table_name from user_tables) loop execute immediate 'delete '|| ec.table_name; end loop; end;
BEGIN for c in (select 'ALTER TABLE '||TABLE_NAME||' ENABLE CONSTRAINT '||constraint_name||' ' as v_sql from user_constraints where CONSTRAINT_TYPE='R') loop EXECUTE IMMEDIATE c.v_sql; end loop; end;
------------------------------------------------------------------------------------------------------------------------------------------------------------
补充:启用外键的时候,如果用步骤③无效的话,那么我们需要手动启用外键!
select 'alter table '||table_name||' enable constraint '||constraint_name||';' from user_constraints where constraint_type='R'
alter table T_DATAMODELEXTENDFIELD enable constraint FK_DATAMODELEXTENDWIDGETID; alter table T_POWER_MAPPER enable constraint FK7865B8A6C505DDD; alter table T_ADMINUSER_MAPPER enable constraint FKCC6502F11FB6F528; alter table T_SYSTEM_TREENODE enable constraint TREENODE_PID; alter table T_DATA_TREE_MAPPER enable constraint T_DATA_T_REFERENCE_T_SYSTEM; alter table T_SYSTEM_TREENODE enable constraint T_SYSTEM_REFERENCE_T_SYSTEM; alter table T_ANSWER enable constraint FK5F4291E99020211D; alter table T_POWER_POINT enable constraint FK1CD8FA8B2EB2BE67; alter table T_OPERATE_LOG enable constraint FKF5C6CABE16D04B87; alter table T_POWER_MAPPER enable constraint FK7865B8A6531B82E2; alter table T_FEEDMODULE_TYPE_MAPPER enable constraint FK4575CFDC303E350E; alter table T_FEEDBACK_ANSWER enable constraint FKE54C2FED7FFDB5CE; alter table T_FAVORITE enable constraint FKF54E3D078EBBFDB5; alter table T_FAVORITE_TYPE enable constraint FK54731772C1EDF96B; alter table T_EMAIL_USER_MAPPER enable constraint FK178A5A079AA6A005; alter table T_D_MANUALS enable constraint FK_TD_MANUALS_ZHONGD; alter table T_D_STRATEGY enable constraint FK_STRATEGY_RAIDERTYPE; alter table T_D_CZSTEP enable constraint FK_TD_CZSTEP_T_PROCESS; alter table T_DOC_POWER_MAPPER enable constraint FK8CECEACD8D4BBF79; alter table T_CMS_CLASS enable constraint FK1B67ACF760A36168; alter table T_DATAMODELEXTENDFIELD enable constraint FK_EXTENDWIDGETDATAMODELID; alter table T_COORDINATE enable constraint T_COORDI_REFERENCE_T_COORDI; alter table T_CMS_USER enable constraint FK_CMS_USER_LEVEL; alter table T_CMS_USER enable constraint FK_CMS_USER_USERGROUPID; alter table T_CMS_USER_GROUP enable constraint FK_USERGROUP_PID; alter table T_DOC_POWER_MAPPER enable constraint FK8CECEACD76E86716; alter table T_COORDINATE_MAPPER enable constraint FK_T_COORDI_REFERENCE_T_CMS_US; alter table T_FAVORITE enable constraint FK_FAVORITE_USERID; alter table T_CMS_USER_JIFEN_LOG enable constraint FK74A9BC62835201B2; alter table T_ANSWER enable constraint FK5F4291E92E8E5B29; alter table T_QUESTION enable constraint FK712EAF512E8E5B29; alter table T_DYNAMICS enable constraint FKCB31B7F2E8E5B29; alter table T_BLOG enable constraint FKCB5B108D2E8E5B29; alter table T_DISCUSS_CONTENT enable constraint FK47DE3D4F2E8E5B29; alter table T_DOCS enable constraint FK_TDOCS_USERID; alter table T_LETTER enable constraint FK718976511AF48B53; alter table T_USER_FRIEND enable constraint FKCF0AA247B91113C; alter table KM_DOCS enable constraint FKDBA4DAF82E8E5B29; alter table T_EMAIL_USER_MAPPER enable constraint FK178A5A072E8E5B29; alter table T_DOC_POWER_MAPPER enable constraint FK8CECEACD14EC0B48; alter table T_CMS_CLASS enable constraint FK_CMSCLASSSITEID; alter table T_DATAMODEL enable constraint FK9FC3FB42A9D5321; alter table T_CMS_CLASS enable constraint FK_CMSCLASSPID; alter table T_COORDINATE enable constraint FKC938BA234F395E2E; alter table T_ZHUI_WEN enable constraint FK831F874E1944206; alter table T_QUESTION enable constraint FK712EAF519BEA7A31; alter table T_D_STRATEGY enable constraint FK_STRATEGY_ADMINUSER; alter table KM_CONTENT_VERSIONS enable constraint FK2D4AF8DE318963AE; alter table KM_CONTENT_VERSIONS enable constraint FK2D4AF8DEA4DF1815; alter table KM_CONTENT_VERSIONS enable constraint FK2D4AF8DEA72909F0; alter table KM_CONTENT enable constraint FK11C669C318963AE; alter table KM_CONTENT enable constraint FK11C669CA4DF1815; alter table KM_TREE enable constraint FKDBAC2C5B394AD71B; alter table KM_TREE_COORDINATE enable constraint FK_KM_TREE__REFERENCE_KM_TREE; alter table KM_TOOLBAR enable constraint FK8470BD9EEA9BA8F8; alter table KM_NOTICE enable constraint FK96F2145555561879; alter table KM_GUZHANG enable constraint FKDF95FDE155561879; alter table KM_NOTICE enable constraint FK96F2145560DD35CD; alter table KM_GUZHANG enable constraint FKDF95FDE160DD35CD; alter table KM_DOC_MAPPER enable constraint FK7BB1DE253EF6F880; alter table KM_CONTENT enable constraint FK11C669C664239CA;
Tips:删除外键或者禁用外键的sql脚本
删除外键: 关键字 drop constraiint
select 'alter table '||table_name||' drop constraint '||constraint_name||';' from user_constraints where constraint_type='R'
禁用外键:关键字disable constraint
select 'alter table '||table_name||' disable constraint '||constraint_name||';' from user_constraints where constraint_type='R'