我在测试一些SQL命令时创建了很多表和用户,现在我不记得所有的表和用户的名字。
但现在我想为我的大项目删除所有内容。那么是否可以在SQL命令行中删除它?
答案 0 :(得分:0)
执行此命令:
BEGIN
FOR table_ IN (SELECT * FROM dba_tables where owner like 'YOUR_SCHEMA') LOOP
execute immediate 'truncate table ' || table_.owner || '.' || table_.table_name ||' cascade';
END LOOP;
END;
/
答案 1 :(得分:0)
您有两种方式:
你可以使用函数delete
而不是truncate
这个函数你不应该禁用约束,但它的工作速度会慢一些,因为如果你这样做就可以回滚这个操作:
BEGIN
FOR table_ IN (SELECT * FROM dba_tables where owner like 'YOUR_SCHEMA') LOOP
execute immediate 'delete from table ' || table_.owner || '.' || table_.table_name;
END LOOP;
END;
使用truncate
它会更快,但您必须在表格上禁用约束:
begin
for disable_constraint_ in
(select * from dba_constraints where owner= 'YOUR_SCHEMA'
)
loop
execute immediate 'alter table ' || disable_constraint_.owner || '.' || disable_constraint_.table_name ||' disable constraint '|| disable_constraint_.constraint_name;
end loop;
for table_ in (select * from dba_tables where owner = 'YOUR_SCHEMA')
loop
execute immediate 'truncate table ' || table_.owner || '.' ||table_.table_name ||' cascade';
end loop;
for enable_constaint_ in (select * from dba_constraints where owner= 'YOUR_SCHEMA')
loop
execute immediate 'alter table ' || enable_constaint_.owner || '.' || enable_constaint_.table_name ||' enable constraint '|| enable_constaint_.constraint_name;
end loop;
end;
答案 2 :(得分:0)
BEGIN
FOR T in (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' DISABLE ALL CONSTRAINTS';
--This will disable all the constraint
END LOOP;
FOR T in (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE 'TRUNCATE TABLE '||T.table_name;
END LOOP;
FOR T in (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' ENABLE ALL CONSTRAINTS';
END LOOP;
END;
更改表...禁用所有约束抛出ORA-01735:如果没有为表定义约束,则抛出ALTER TABLE选项无效,如果至少有一个表没有,则会导致脚本无法截断每个表约束。您可能希望将execute immediate放在begin -exception块
中答案 3 :(得分:0)
此PL / SQL块将对删除oracle数据库中的所有数据很有用
BEGIN
FOR cur_rec IN (SELECT object_name, object_type
FROM user_objects
WHERE object_type IN
('TABLE',
'VIEW',
'PACKAGE',
'PROCEDURE',
'FUNCTION',
'SEQUENCE',
'SYNONYM',
'PACKAGE BODY'
))
LOOP
BEGIN
IF cur_rec.object_type = 'TABLE'
THEN
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '" CASCADE CONSTRAINTS';
ELSE
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"';
END IF;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ( 'FAILED: DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"'
);
END;
END LOOP;
END;
/