linux truncate 命令,truncate 命令使用

今天使用truncate清空表时报错了:

SQL> truncate table ref_set;

truncate table ref_set

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

通过dba_constraints查看,该表的主键被其他表所引用,因此无法truncate。即使从表中的数据被清空或者从来没有过数据,也无法直接对被引用的表执行truncate操作。

解决方法:alter table tab_name disable constraint constraint_name;

说明:以前曾经听过关于truncate的限制,但实际工作中由于用的比较少,渐渐忘了。

除了存在外键约束而无法执行truncate外,还应该注意:truncate是ddl操作,如果在执行truncate过程中由其他用户的dml操作没有提交,truncate会触发其提交操作。因此在并发比较高的系统中慎重使用truncate命令。

根据表名查询有哪些外键调用了该表中的列:

select a.table_name "被引用表",a.constraint_name "约束名",

b.constraint_name "外键名",b.table_name "引用表" from dba_constraints a,dba_constraints b

where a.constraint_type='P' and a.owner=:COLUMN_NAME

and a.constraint_name=b.r_constraint_name;

以下直接产生禁用约束的语句:

select 'alter table '||b.table_name||'disable constraint '||b.constraint_name

from dba_constraints a,dba_constraints b

where a.constraint_type='P' and a.owner=:COLUMN_NAME

and a.constraint_name=b.r_constraint_name and a.table_name=:table_name;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值