Script for dropping all the tables

Hello:
Can somebody share with me a script that will delete all the tables in a schema?
Thanks.

I think what Justin was getting at is this: we cannot drop tables that are referenced by foreign keys. So before we can drop all tables weed to drop all referencing constraints. Which makes this script even more dangerous - you may wish to abort if other schemas reference the "droppee".
Let's be careful out there!
Cheers, APC
declare
    cursor fks is
        select owner,table_name, constraint_name
        from all_constraints
        where r_owner='&&schema_to_delete'
        and constraint_type = 'R';
    cursor tables is
        select owner,table_name
        from all_tables
        where owner='&&schema_to_delete';
    begin
        for b in fks
        loop
            dbms_output.put_line( 'drop fk constraint '||b.owner||'.'||b.table_name||'.'||b.constraint_name);
            execute immediate 'alter table '||b.owner||'.'||b.table_name||' drop constraint '||b.constraint_name;
        end loop;
        for c in tables
        loop
            dbms_output.put_line( 'drop table '||c.owner||'.'||c.table_name);
            execute immediate 'drop table '||c.owner||'.'||c.table_name;
    end loop;
end;
/

Similar Messages

  • Script to generate all the tables and objects in a schema

    how to write a script to generate all the tables and objects in a schema.
    with toad the no of tables generated is not matching when i check from schema .

    Dear Sidhant,
    Try this script:
    set termout off
    set feedback off
    set serveroutput on size 100000
    spool ddl_schema.sql
    begin
    dbms_output.put_line('--');
    dbms_output.put_line('-- DROP TABLES --');
    dbms_output.put_line('--');
        for rt in (select tname from tab order by tname) loop
            dbms_output.put_line('DROP TABLE '||rt.tname||' CASCADE CONSTRAINTS;');
        end loop;
    end;
    declare
        v_tname  varchar2(30);
        v_cname  char(32);
        v_type     char(20);
        v_null   varchar2(10);
        v_maxcol number;
        v_virg     varchar2(1);
    begin
    dbms_output.put_line('--');
    dbms_output.put_line('-- CREATE TABLES --');
    dbms_output.put_line('--');
        for rt in (select table_name from user_tables order by 1) loop
            v_tname:=rt.table_name;
            v_virg:=',';
            dbms_output.put_line('CREATE TABLE '||v_tname||' (');
            for rc in (select table_name,column_name,data_type,data_length,
                                data_precision,data_scale,nullable,column_id
                    from user_tab_columns tc
                    where tc.table_name=rt.table_name
                    order by table_name,column_id) loop
                        v_cname:=rc.column_name;
                        if rc.data_type='VARCHAR2' then
                            v_type:='VARCHAR2('||rc.data_length||')';
                        elsif rc.data_type='NUMBER' and rc.data_precision is null and
                                             rc.data_scale=0 then
                            v_type:='INTEGER';
                        elsif rc.data_type='NUMBER' and rc.data_precision is null and
                                         rc.data_scale is null then
                            v_type:='NUMBER';
                        elsif rc.data_type='NUMBER' and rc.data_scale='0' then
                            v_type:='NUMBER('||rc.data_precision||')';
                        elsif rc.data_type='NUMBER' and rc.data_scale<>'0' then
                            v_type:='NUMBER('||rc.data_precision||','||rc.data_scale||')';
                        elsif rc.data_type='CHAR' then
                             v_type:='CHAR('||rc.data_length||')';
                        else v_type:=rc.data_type;
                        end if;
                        if rc.nullable='Y' then
                            v_null:='NULL';
                        else
                            v_null:='NOT NULL';
                        end if;
                        select max(column_id)
                            into v_maxcol
                            from user_tab_columns c
                            where c.table_name=rt.table_name;
                        if rc.column_id=v_maxcol then
                            v_virg:='';
                        end if;
                        dbms_output.put_line (v_cname||v_type||v_null||v_virg);
            end loop;
            dbms_output.put_line(');');
        end loop;
    end;
    declare
        v_virg        varchar2(1);
        v_maxcol    number;
    begin
    dbms_output.put_line('--');
    dbms_output.put_line('-- PRIMARY KEYS --');
    dbms_output.put_line('--');
        for rcn in (select table_name,constraint_name
                from user_constraints
                where constraint_type='P'
                order by table_name) loop
            dbms_output.put_line ('ALTER TABLE '||rcn.table_name||' ADD (');
            dbms_output.put_line ('CONSTRAINT '||rcn.constraint_name);
            dbms_output.put_line ('PRIMARY KEY (');
            v_virg:=',';
            for rcl in (select column_name,position
                    from user_cons_columns cl
                    where cl.constraint_name=rcn.constraint_name
                    order by position) loop
                select max(position)
                    into v_maxcol
                    from user_cons_columns c
                    where c.constraint_name=rcn.constraint_name;
                if rcl.position=v_maxcol then
                    v_virg:='';
                end if;
                dbms_output.put_line (rcl.column_name||v_virg);
            end loop;
            dbms_output.put_line(')');
            dbms_output.put_line('USING INDEX );');
        end loop;
    end;
    declare
        v_virg        varchar2(1);
        v_maxcol    number;
        v_tname        varchar2(30);
    begin
    dbms_output.put_line('--');
    dbms_output.put_line('-- FOREIGN KEYS --');
    dbms_output.put_line('--');
        for rcn in (select table_name,constraint_name,r_constraint_name
                from user_constraints
                where constraint_type='R'
                order by table_name) loop
            dbms_output.put_line ('ALTER TABLE '||rcn.table_name||' ADD (');
            dbms_output.put_line ('CONSTRAINT '||rcn.constraint_name);
            dbms_output.put_line ('FOREIGN KEY (');
            v_virg:=',';
            for rcl in (select column_name,position
                    from user_cons_columns cl
                    where cl.constraint_name=rcn.constraint_name
                    order by position) loop
                select max(position)
                    into v_maxcol
                    from user_cons_columns c
                    where c.constraint_name=rcn.constraint_name;
                if rcl.position=v_maxcol then
                    v_virg:='';
                end if;
                dbms_output.put_line (rcl.column_name||v_virg);
            end loop;
            select table_name
                into v_tname
                from user_constraints c
                where c.constraint_name=rcn.r_constraint_name;
            dbms_output.put_line(') REFERENCES '||v_tname||' (');
            select max(position)
                    into v_maxcol
                    from user_cons_columns c
                    where c.constraint_name=rcn.r_constraint_name;
            v_virg:=',';
            select max(position)
                into v_maxcol
                from user_cons_columns c
                where c.constraint_name=rcn.r_constraint_name;
            for rcr in (select column_name,position
                    from user_cons_columns cl
                    where rcn.r_constraint_name=cl.constraint_name
                    order by position) loop
                if rcr.position=v_maxcol then
                    v_virg:='';
                end if;
                dbms_output.put_line (rcr.column_name||v_virg);
            end loop;
            dbms_output.put_line(') );');
        end loop;
    end;
    begin
    dbms_output.put_line('--');
    dbms_output.put_line('-- DROP SEQUENCES --');
    dbms_output.put_line('--');
        for rs in (select sequence_name
                from user_sequences
                where sequence_name like 'SQ%'
                order by sequence_name) loop
            dbms_output.put_line('DROP SEQUENCE '||rs.sequence_name||';');
        end loop;
    dbms_output.put_line('--');
    dbms_output.put_line('-- CREATE SEQUENCES --');
    dbms_output.put_line('--');
        for rs in (select sequence_name
                from user_sequences
                where sequence_name like 'SQ%'
                order by sequence_name) loop
            dbms_output.put_line('CREATE SEQUENCE '||rs.sequence_name||' NOCYCLE;');
        end loop;
    end;
    declare
        v_virg        varchar2(1);
        v_maxcol    number;
    begin
    dbms_output.put_line('--');
    dbms_output.put_line('-- INDEXES --');
    dbms_output.put_line('--');
        for rid in (select index_name, table_name
                from user_indexes
                where index_name not in (select constraint_name from user_constraints)
                    and index_type<>'LOB'
                order by index_name) loop
            v_virg:=',';
            dbms_output.put_line('CREATE INDEX '||rid.index_name||' ON '||rid.table_name||' (');
            for rcl in (select column_name,column_position
                    from user_ind_columns cl
                    where cl.index_name=rid.index_name
                    order by column_position) loop
                select max(column_position)
                    into v_maxcol
                    from user_ind_columns c
                    where c.index_name=rid.index_name;
                if rcl.column_position=v_maxcol then
                    v_virg:='';
                end if;
                dbms_output.put_line (rcl.column_name||v_virg);
            end loop;
            dbms_output.put_line(');');
        end loop;
    end;
    spool off
    set feedback on
    set termout on Best Regards,
    Francisco Munoz Alvarez
    www.oraclenz.com

  • To drop all the tables in a database

    hi,
    I have 25000 tables in a database.I want ot delete all the tables and then i have to import the dump.For deleting all the tables what query has to be given.
    Thanks in advance,
    R.Ratheesh

    The code would be
    BEGIN
       FOR c IN (SELECT owner,
                        table_name
                   FROM all_tables
                  WHERE owner IN ('YOUR_OWNER1', 'YOUR_OWNER2_ETC'))
       LOOP
          EXECUTE IMMEDIATE    'drop table '
                            || c.owner
                            || '.'
                            || c.table_name
                            || ' cascade constraints';
       END LOOP;
    END;
    the user ,tablespaces ,datafiles has to be recreatedbut still dropping the user would be the simplest option
    you don't have to recreate tablespaces and datafiles for that.

  • How to delete/drop all the tables from SQL Server Database without using Enterprise Manager?

    How to delete/drop all the tables from SQL Server Database without using Enterprise Manager?
    I tried using DROP Tables, Truncate Database, Delete and many more but it is not working.  I want to delete all tables using Query Analyzer, i.e. through SQL Query.
    Please help me out in this concern.
    Nishith Shah

    Informative thread indeed. Wish I saw it early enough. Managed to come up with the code below before I saw this thread.
    declare @TTName Table
    (TableSchemaTableName
    varchar
    (500),
    [status] int
    default 0);
    with AvailableTables
    (TableSchemaTableName)
    as
    (select
    QUOTENAME(TABLE_SCHEMA)
    +
    +
    QUOTENAME(TABLE_NAME)
    from
    INFORMATION_SCHEMA.TABLES)
    insert into @TTName
    (TableSchemaTableName)
    select *
    from AvailableTables
    declare @TableSchemaTableName varchar
    (500)
    declare @sqlstatement nvarchar
    (1000)
    while 1=1
    begin
    set @sqlstatement
    =
    'DROP TABLE '
    + @TableSchemaTableName
    exec
    sp_executeSQL
    @sqlstatement
    print
    'Dropped Table : '
    + @TableSchemaTableName
    update @TTName
    set [status]
    = 1
    where TableSchemaTableName
    = @TableSchemaTableName
    if
    (select
    count([Status])
    from @TTName
    where [Status]
    = 0)
    = 0
    break
    end

  • How to move all the tables from one tablespace to other for a whole schema

    Hi,
    Is there any way to move all the tables in a schema from one tablespace to other?
    If so please help me out to do that.
    Thanks
    Regards
    Gatha

    hi,
    here is the steps to move SCOTT's objects from their current tablespace to a NEW_TABLESPACE
    would be:
    1) do an export of all of scott's objects. Make sure no one modifies them after you
    begin this process. You will lose these changes if they do.
    $ exp userid=scott/tiger owner=scott
    2) you would drop all of scotts tables. This will get the indexes as well. I don't
    suggest dropping the user SCOTT but rather dropping scott's objects. Dropping scott
    would cause any system priveleges SCOTT has to disappear and the import would not restore
    them. This script can be used to drop someones tables:
    set heading off
    set feedback off
    set verify off
    set echo off
    spool tmp.sql
    select 'drop table &1..' || table_name || ' cascade constraints;'
    from dba_tables
    where owner = upper('&1')
    spool off
    @tmp.sql
    3) You would modify the user to not have unlimited tablespace (else the IMP will just
    put the objects right back into the tablespace they came from) and then give them
    unlimited quota's on the new tablespace you want the objects to go into and on their
    temporary tablespace (for the sorts the index creates will do)
    alter user SCOTT default tablespace NEW_TABLESPACE
    revoke unlimited tablespace from SCOTT
    alter user SCOTT quota unlimited on NEW_TABLESPACE
    alter user SCOTT quota unlimited on SCOTTS_TEMPORARY_TABLESPACE
    4) you will IMP the data back in for that user. IMP will rewrite the create statements
    to use the users default tablespace when it discovers that it cannot create the objects
    in their original tablespace. Please make sure to review the file imp.log after you do
    this for any and all errors after you import.
    imp userid=scott/tiger full=y ignore=y log=imp.log
    5) you can optionally restore 'unlimited tablespace' to this user (or not). If you do
    not, this user can only create objects in this new tablespace and temp (which in itself
    is not a bad thing)...
    Regards,
    Mohd Mehraj Hussain
    http://mehrajdba.wordpress.com

  • What are all the tables used for this report:

    hi
    what are all the tables used for this report:
    report:
    •     <b>Stock Report, which will give opening balance, receipt, issue, and closing balance for any given Duration for any material.</b>
    thanks in advance

    Tables: MSEG, MKPF, MARD.
    FOR REFERENCE SEE TRANSACTION : MB5B.
    Message was edited by: Sharath kumar R

  • How to find all the tables associated for a particular transaction

    Hi-
    May I know how to find all the tables, related(foreign key) tables for a transaction within SAP GUI?
    Up to my technical knowledge, this can be achieved by looking database diagrams from DB level. But that would be for entire database as a whole. What I'm expecting is to see transaction level relative tables that too from SAP GUI. Please share the possibilities if any.
    Regards
    Sekhar

    Dear Micky Oestreich
    May be we possess expertise or high level experience, it should not show up in our way of communication. Every professional starts with the basic stuff to learn. When the question is raised in such minimum polite way, the same level of courtesy is expected in return. If you felt my question was basic, you might have refused it gently. If you are in good mood or bad mood it doesn't matters.
    Hi Vengal Rao
    Thanks for your response. It helped me.
    Regards
    Sekhar

  • How to generate test data for all the tables in oracle

    I am planning to use plsql to generate the test data in all the tables in schema, schema name is given as input parameters, min records in master table, min records in child table. data should be consistent in the columns which are used for constraints i.e. using same column value..
    planning to implement something like
    execute sp_schema_data_gen (schemaname, minrecinmstrtbl, minrecsforchildtable);
    schemaname = owner,
    minrecinmstrtbl= minimum records to insert into each parent table,
    minrecsforchildtable = minimum records to enter into each child table of a each master table;
    all_tables where owner= schemaname;
    all_tab_columns and all_constrains - where owner =schemaname;
    using dbms_random pkg.
    is anyone have better idea to do this.. is this functionality already there in oracle db?

    Ah, damorgan, data, test data, metadata and table-driven processes. Love the stuff!
    There are two approaches you can take with this. I'll mention both and then ask which
    one you think you would find most useful for your requirements.
    One approach I would call the generic bottom-up approach which is the one I think you
    are referring to.
    This system is a generic test data generator. It isn't designed to generate data for any
    particular existing table or application but is the general case solution.
    Building on damorgan's advice define the basic hierarchy: table collection, tables, data; so start at the data level.
    1. Identify/document the data types that you need to support. Start small (NUMBER, VARCHAR2, DATE) and add as you go along
    2. For each data type identify the functionality and attributes that you need. For instance for VARCHAR2
    a. min length - the minimum length to generate
    b. max length - the maximum length
    c. prefix - a prefix for the generated data; e.g. for an address field you might want a 'add1' prefix
    d. suffix - a suffix for the generated data; see prefix
    e. whether to generate NULLs
    3. For NUMBER you will probably want at least precision and scale but might want minimum and maximum values or even min/max precision,
    min/max scale.
    4. store the attribute combinations in Oracle tables
    5. build functionality for each data type that can create the range and type of data that you need. These functions should take parameters that can be used to control the attributes and the amount of data generated.
    6. At the table level you will need business rules that control how the different columns of the table relate to each other. For example, for ADDRESS information your business rule might be that ADDRESS1, CITY, STATE, ZIP are required and ADDRESS2 is optional.
    7. Add table-level processes, driven by the saved metadata, that can generate data at the record level by leveraging the data type functionality you have built previously.
    8. Then add the metadata, business rules and functionality to control the TABLE-TO-TABLE relationships; that is, the data model. You need the same DETPNO values in the SCOTT.EMP table that exist in the SCOTT.DEPT table.
    The second approach I have used more often. I would it call the top-down approach and I use
    it when test data is needed for an existing system. The main use case here is to avoid
    having to copy production data to QA, TEST or DEV environments.
    QA people want to test with data that they are familiar with: names, companies, code values.
    I've found they aren't often fond of random character strings for names of things.
    The second approach I use for mature systems where there is already plenty of data to choose from.
    It involves selecting subsets of data from each of the existing tables and saving that data in a
    set of test tables. This data can then be used for regression testing and for automated unit testing of
    existing functionality and functionality that is being developed.
    QA can use data they are already familiar with and can test the application (GUI?) interface on that
    data to see if they get the expected changes.
    For each table to be tested (e.g. DEPT) I create two test system tables. A BEFORE table and an EXPECTED table.
    1. DEPT_TEST_BEFORE
         This table has all EMP table columns and a TEST_CASE column.
         It holds EMP-image rows for each test case that show the row as it should look BEFORE the
         test for that test case is performed.
         CREATE TABLE DEPT_TEST_BEFORE
         TESTCASE NUMBER,
         DEPTNO NUMBER(2),
         DNAME VARCHAR2(14 BYTE),
         LOC VARCHAR2(13 BYTE)
    2. DEPT_TEST_EXPECTED
         This table also has all EMP table columns and a TEST_CASE column.
         It holds EMP-image rows for each test case that show the row as it should look AFTER the
         test for that test case is performed.
    Each of these tables are a mirror image of the actual application table with one new column
    added that contains a value representing the TESTCASE_NUMBER.
    To create test case #3 identify or create the DEPT records you want to use for test case #3.
    Insert these records into DEPT_TEST_BEFORE:
         INSERT INTO DEPT_TEST_BEFORE
         SELECT 3, D.* FROM DEPT D where DEPNO = 20
    Insert records for test case #3 into DEPT_TEST_EXPECTED that show the rows as they should
    look after test #3 is run. For example, if test #3 creates one new record add all the
    records fro the BEFORE data set and add a new one for the new record.
    When you want to run TESTCASE_ONE the process is basically (ignore for this illustration that
    there is a foreign key betwee DEPT and EMP):
    1. delete the records from SCOTT.DEPT that correspond to test case #3 DEPT records.
              DELETE FROM DEPT
              WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT_TEST_BEFORE WHERE TESTCASE = 3);
    2. insert the test data set records for SCOTT.DEPT for test case #3.
              INSERT INTO DEPT
              SELECT DEPTNO, DNAME, LOC FROM DEPT_TEST_BEFORE WHERE TESTCASE = 3;
    3 perform the test.
    4. compare the actual results with the expected results.
         This is done by a function that compares the records in DEPT with the records
         in DEPT_TEST_EXPECTED for test #3.
         I usually store these results in yet another table or just report them out.
    5. Report out the differences.
    This second approach uses data the users (QA) are already familiar with, is scaleable and
    is easy to add new data that meets business requirements.
    It is also easy to automatically generate the necessary tables and test setup/breakdown
    using a table-driven metadata approach. Adding a new test table is as easy as calling
    a stored procedure; the procedure can generate the DDL or create the actual tables needed
    for the BEFORE and AFTER snapshots.
    The main disadvantage is that existing data will almost never cover the corner cases.
    But you can add data for these. By corner cases I mean data that defines the limits
    for a data type: a VARCHAR2(30) name field should have at least one test record that
    has a name that is 30 characters long.
    Which of these approaches makes the most sense for you?

  • Recordcount for all the tables in my user

    How I will get recordcount for all the tables in my user
    with a single query??
    Plz help.
    Thanx in advance.

    Not possible. As there can be any number of tables with any names, this requires dynamic SQL.
    SQL given to the Oracle SQL Engine cannot be dynamic ito scope and references - it must be static. For example, one cannot do this:
    SELECT count(*) FROM :table
    For the SQL Engine to parse the SQL, determine if it is valid, determine the scope and security, determine an execution plan, it needs to know the actual object names. Objects like tables and columns and functions cannot be variable.
    You will therefore need to write a user function (in PL/SQL) that dynamically creates a [SELECT COUNT] SQL for a table, execute that SQL and return the row count - and then use SQL to iterate through USER_TABLES for example and sum the results of this function.
    Note that object tables are not listed in USER_TABLES - thus a more comprehensive list of all table objects in your schema can be found in USER_OBJECTS.

  • What r all the tables used for this report. please reply

    hai,
    what r all the tables used for this report.
    report :
    <b>
    •     Report to display all finished goods that go out-of-stock. Developed a drill down report for materials details (totals and subtotals for material stock values by material group and material type).</b>
    thanks in advance

    hi Ashok,
    Check this out
    http://www.allsaplinks.com/tables.html
    http://www.sapgenie.com/abap/tables.htm
    Regards,
    Santosh

  • All the tables used for Organisation details

    Hi All,
    Kindly request to specify all the tables ( or atleast possible list of tables ) that are updated when we change / insert / modify the organisation details for a transaction number in CRM.
    Thanks and regards.

    Tables updated when you change org in CRM transactions are CRMD_ORDER_INDEX , CRMD_ORGMAN

  • What r all the tables  used for the following report

    hai
    what r all the tables  used for the following report
    report:
    <b>
    •     Report that displays all the late shipments in a particular period of time</b>
    send important fields for this tables also
    thanks in advance.

    Hi,
       Imp table are vttk,vttp,vtts.
    Regards
    Amole

  • Creating SQL-Loader script for more than one table at a time

    Hi,
    I am using OMWB 2.0.2.0.0 with Oracle 8.1.7 and Sybase 11.9.
    It looks like I can create SQL-Loader scripts for all the tables
    or for one table at a time. If I want to create SQL-Loader
    scripts for 5-6 tables, I have to either create script for all
    the tables and then delete the unwanted tables or create the
    scripts for one table at a time and then merge them.
    Is there a simple way to create migration scripts for more than
    one but not all tables at a time?
    Thanks,
    Prashant Rane

    No there is no multi-select for creating SQL-Loader scripts.
    You can either create them separately or create them all and
    then discard the one you do not need.

  • Selecting string from all the tables in databse

    Hi All,
    I need a help in finding the data from all the database tables at one time using single query.
    For e.g. I wanna search a string 'ABC' in all the database tables for a schema.
    I want to find out,which all tables (in either of its columns) contain this string strored inside themselves.
    For brand name changing,I need to find out all the tables and in turn all the columns containing that string where databse consisting of 1000 tables.
    Could anyone suggest me some option for this?Is it possible to avoid this tedious task to search individual table?

    Why is it necessary to search every column? Does your data model permit the brand name to be stored in any column?
    Whatever.
    I suggest starting with dba_tab_columns (where owner = 'WHATEVER') and look for those tables/columns that could possibly hold the brand name. You can eliminate NUMBER and DATE columns and VARCHAR columns that are too short.
    Spool that out to another script where each select statement is like this:
    SELECT 'TABLE_NAME.COLUMN_NAME', column_name
    FROM table_name
    WHERE UPPER(COLUMN_NAME) LIKE '%BRAND NAME%'
    AND rownum = 1;

  • Dropping all deployed tables in OWB 10.2.0.3.0

    I am running OWB 10.2.0.3.0 and have multiple schemas containing deployed tables. This is a test instance and I would like to drop all the old tables and recapture some of the space. Is WB_RT_VERSION_FLAG the only table I need to keep intact to keep OWB functioning? Should I also leave CREATE$JAVA$LOB$TABLE and TRACE if it exists? Thanks.

    Hi,
    you must keep WB_RT_VERSION_FLAG, all the synonyms (created by owb) and the roles of a target user. All other objects may be dropped.
    You should consider dropping the objects using control center manager (use context menu on the module an set the action to drop for all objects at once) in order to have the deployment status correctly.
    Regards,
    Carsten.

Maybe you are looking for