Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wipe data from Oracle DB

Tags:

sql

oracle

I'm running some test to migrate data from one database to another and for that i need to delete and recreate same tables, views and other stuff. So what is the SQL statement(s) in Oracle to wipe everything (delete Tables, Views, Sequences, Functions, Procedures, etc.). I know that I can use "DROP" but sometimes that's not convenient enough.

Thanks

like image 339
Maksim Avatar asked Nov 10 '09 23:11

Maksim


1 Answers

The easiest way would be to drop the schema the objects are associated to:

DROP USER [schema name] CASCADE

Nuke it from orbit - it's the only way to be sure ;)

For the script you provided, you could instead run those queries without having to generate the intermediate script using the following anonymous procedure:

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;
like image 68
OMG Ponies Avatar answered Sep 20 '22 02:09

OMG Ponies