某些情况,需要清空某些大量数据的表,比如要重新部署一个新的系统,数据量在百万以上的表如果用delete清空会非常慢,这时候需要truncate table语句。
1. 用SQL语句查询出整个数据库中所有表的数据行数。select t.table_name,t.num_rows
from all_tables t
where t.num_rows is not null
order by num_rows desc;
结果大概是这样:
2. 执行truncate table。truncate table T_MEDIAUSERLOG;
这是有可能会报错:SQL 错误: ORA-02266: 表中的唯一/主键被启用的外键引用
02266. 00000 - "unique/primary keys in table referenced by enabled foreign keys"
3. 可以用SQL查询出指定表的主键被哪些表当成了外键。select a.constraint_type,a.status, 'alter table ',a.table_name, ' disable constraint ' , b.constraint_name ,';'
from user_constraints a
inner join user_cons_columns b on a.constraint_name = b.constraint_name
where a.r_constraint_name in
(select constraint_name from user_constraints e where e.table_name='T_COMMODITIES');
查询结果类似这样:
4. 复制出选中的这些生成好的SQL语句执行,可以禁用约束,然后再次执行truncate table即可。table T_COMMODITIES已截断。
5. 如果需要,这些约束稍后再次启用