我正在运行一些测试来将数据从一个数据库迁移到另一个数据库,为此我需要删除并重新创建相同的表,视图和其他内容。那么Oracle中用于擦除所有内容的SQL语句是什么(删除表,视图,序列,函数,过程等)。我知道我可以使用“DROP”但有时候这不够方便。
由于
答案 0 :(得分:6)
在我不能简单地删除架构的情况下,我使用以下脚本:
DECLARE
CURSOR c_get_objects IS
SELECT object_type, '"' || object_name || '"' || DECODE(object_type, 'TABLE', ' cascade constraints', NULL) obj_name
FROM user_objects
WHERE object_type IN ('TABLE', 'VIEW', 'PACKAGE', 'SEQUENCE', 'PROCEDURE', 'FUNCTION', 'SYNONYM', 'MATERIALIZED VIEW')
ORDER BY object_type;
CURSOR c_get_objects_type IS
SELECT object_type, '"' || object_name || '"' obj_name FROM user_objects WHERE object_type IN ('TYPE');
BEGIN
FOR object_rec IN c_get_objects
LOOP
EXECUTE IMMEDIATE ('drop ' || object_rec.object_type || ' ' || object_rec.obj_name);
END LOOP;
FOR object_rec IN c_get_objects_type
LOOP
EXECUTE IMMEDIATE ('drop ' || object_rec.object_type || ' ' || object_rec.obj_name || ' force');
END LOOP;
END;
/
答案 1 :(得分:4)
最简单的方法是删除与之关联的架构:
DROP USER [schema name] CASCADE
从轨道上取下它 - 这是确保唯一的方法;)
对于您提供的脚本,您可以改为运行这些查询,而无需使用以下匿名过程生成中间脚本:
BEGIN
--Bye Views!
FOR i IN (SELECT uv.view_name
FROM USER_VIEWS uv) LOOP
EXECUTE IMMEDIATE 'drop view '|| i.view_name ||'';
END LOOP;
--Bye Sequences!
FOR i IN (SELECT us.sequence_name
FROM USER_SEQUENCES us) LOOP
EXECUTE IMMEDIATE 'drop sequence '|| i.sequence_name ||'';
END LOOP;
--Bye Tables!
FOR i IN (SELECT ut.table_name
FROM USER_TABLES ut) LOOP
EXECUTE IMMEDIATE 'drop table '|| i.table_name ||' CASCADE CONSTRAINTS ';
END LOOP;
--Bye Procedures/Functions/Packages!
FOR i IN (SELECT us.name,
us.type
FROM USER_SOURCE us
WHERE us.type IN ('PROCEDURE', 'FUNCTION', 'PACKAGE')
GROUP BY us.name, us.type) LOOP
EXECUTE IMMEDIATE 'drop '|| i.type ||' '|| i.name ||'';
END LOOP;
--Bye Synonyms!
FOR i IN (SELECT ut.synonym_name
FROM USER_SYNONYMS us
WHERE us.synonym_name NOT LIKE 'sta%'
AND us.synonym_name LIKE 's_%') LOOP
EXECUTE IMMEDIATE 'drop synonym '|| i.synonym_name ||'';
END LOOP;
END;
答案 2 :(得分:1)
我found this script你可以在哪里生成脚本来删除所有东西,但是如果我使用通用的东西那就太棒了。
set feedback off
set pagesize 0
spool AllObjectsDrop.sql
select 'drop view '||view_name||';' from user_views;
select distinct 'drop sequence '||sequence_name|| ';'from user_sequences;
select distinct 'drop table '||table_name|| ';'from user_tables;
select distinct 'drop procedure '||name|| ';'from user_source where type = 'procedure';
select distinct 'drop function '||name|| ';'from user_source where type = 'function';
select distinct 'drop package '||name|| ';'from user_source where type = 'package';
select 'drop synonym '||synonym_name||';' from user_synonyms where synonym_name not like 'sta%' and synonym_name like 's_%'
spool off
答案 3 :(得分:1)
如果您有Enterprise Edition,则应查看restore points and Flashback Database。
如果你有权限和快速脚本来重新创建用户,那么DROP USER ... CASCADE是好的。
答案 4 :(得分:0)
之前我一直在那个场景中,我所做的是恢复备份并覆盖当前架构。
这样我就把所有东西都放回原点。