Fork me on GitHub

oracle清空用户下的所有数据及表

!!! 注意这种方式会删除用户下面的所有表和数据,所以只能在新建的普通用户(只授权了指定的表空间)下面执行。

1、先禁用数据库中所有的约束

constraint_type: P:主键 R:外键 U:唯一键 C:检查

select 'alter table ' || table_name || ' disable constraint ' ||
constraint_name || ';'
from user_constraints
where constraint_type = 'R';

执行所有约束禁用命令。

2、清空所有表中的数据

select 'truncate table '||table_name||';' from user_tables;

执行所有的清表命令。

3、删除所有表

select 'drop table '||table_name||';' as sqlscript from user_tables;

执行所有表的删除命令。

4、启用数据库中所有表的约束

select 'alter table ' || table_name || ' enable constraint ' ||
constraint_name || ';'
from user_constraints
where constraint_type = 'R';

执行所有约束启用命令。

posted @ 2022-11-29 15:10  秋夜雨巷  阅读(3367)  评论(0编辑  收藏  举报