Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Phil.

Asked: October 29, 2007 - 11:43 am UTC

Last updated: October 30, 2007 - 11:06 am UTC

Version: 10.2

Viewed 10K+ times! This question is

You Asked

Hi Tom

Many years ago, I had in my armoury of scripts an SQL script that would knock out a drop script for a schema. It would use the dependency information within the database to drop tables in order & then views sequences etc. Life without it has been OK but I've not come across it since losing it and despite the fact I can run a script that firstly drops all constraints it seemed quite elegant to just do it in the right order.

Do you know of such an approach?

Cheers

Phil

and Tom said...

being rather lazy sometimes, I would

a) drop user cascade
b) after running
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:494205100346718343
if I needed the user to exist
c) run the script from b to recreate them.

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

OR use TOAD or Oracle EM

Reader, October 30, 2007 - 11:22 am UTC

that will give you create user script with all the grants, object and system privs. These days GUI tools are good to extract scripts.

Approach

Phil Winfield, October 30, 2007 - 1:10 pm UTC

Guess so but my reason is for a deinstall option on an APEX app. The script needs to drop all tables and I thought it would be easier to ressurrect this script. I've managed to get around by just doing a for loop with execute immediate in the middle for all tables (with cascade constraints) and sequences and then a series of drop statements for the remaining objects. APEX v3 should really permit multiple de-install scripts in the same way we have multiple install scripts.
Phil

cleanschema.sql

Balaji, October 30, 2007 - 1:59 pm UTC

Guys,
I use the following script to cleanup the schema.
Since I use "CASCADE CONSTRAINTS" for table it doesn't throw me an error.
--------------------------------------------------------------------

set feedback off
set heading off
set termout off
set linesize 1000
set trimspool on
set verify off
spool c:\temp\drop_objects.lst
SELECT 'DROP ' || OBJECT_TYPE || ' ' || OBJECT_NAME ||';' FROM USER_OBJECTS
WHERE OBJECT_TYPE <> 'TABLE' AND OBJECT_TYPE <> 'INDEX' AND OBJECT_TYPE<>'PACKAGE BODY' AND OBJECT_TYPE<>'TRIGGER' AND OBJECT_TYPE<>'LOB'
UNION ALL
SELECT 'DROP ' || OBJECT_TYPE || ' ' || OBJECT_NAME ||' CASCADE CONSTRAINTS;' FROM USER_OBJECTS
WHERE OBJECT_TYPE = 'TABLE';
spool off;
@c:\temp\drop_objects.lst
prompt 'Purging the recycle bin...'
purge recyclebin;
host del c:\temp\drop_objects.lst
exit;

may help

T. Charlton, February 07, 2008 - 1:13 pm UTC

SET PAGES 0 TRIMS ON ECHO OFF VERIFY OFF FEEDBACK OFF


PROMPT
PROMPT ***** DROP all tables *****
PROMPT

SPOOL drop_tables.sql

SELECT 'SET ECHO ON TIME ON' FROM dual;
SELECT 'SPOOL drop_tables.log' FROM dual;
SELECT '' FROM dual;

SELECT 'DROP TABLE '||owner||'.'||table_name||';'
FROM dba_tables
WHERE owner='&1'
/

SELECT '' FROM dual;
SELECT 'SPOOL OFF' FROM dual;
SELECT 'SET ECHO OFF TIME OFF' FROM dual;

SPOOL OFF
SET PAGES 999 TRIMS ON ECHO OFF VERIFY ON FEEDBACK ON

PROMPT
PROMPT *******************************
PROMPT
PROMPT Output saved at drop_tables.sql
PROMPT