Removing data from an Oracle database

I run some test to transfer data from one database to another, and for this I need to delete and recreate the same tables, views and other materials. So, what is an SQL query in Oracle to erase everything (delete tables, views, sequences, functions, procedures, etc.). I know that I can use "DROP", but sometimes it is not so convenient.

thanks

+3
source share
5 answers

The easiest way is to abandon the scheme with which the objects are connected:

DROP USER [schema name] CASCADE

Remove it from orbit - this is the only way to make sure;)

script, , , script, :

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;
+3

, , script:

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;
/
+6

script, 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
+1

Enterprise Edition, Flashback Database.

DROP USER... CASCADE , script, .

+1

, .

, .

0
source

Source: https://habr.com/ru/post/1722521/


All Articles