How to drop all user tables?

前端 未结 9 576
后悔当初
后悔当初 2020-12-04 04:22

How can I drop all user tables in oracle?

I have problem with constraints. When I disable all it is still no possible.

相关标签:
9条回答
  • 2020-12-04 05:06
    SELECT 'DROP TABLE "' || TABLE_NAME || '" CASCADE CONSTRAINTS;' 
    FROM user_tables;
    

    user_tables is a system table which contains all the tables of the user the SELECT clause will generate a DROP statement for every table you can run the script

    0 讨论(0)
  • 2020-12-04 05:11

    Another answer that worked for me is (credit to http://snipt.net/Fotinakis/drop-all-tables-and-constraints-within-an-oracle-schema/)

    BEGIN
    
    FOR c IN (SELECT table_name FROM user_tables) LOOP
    EXECUTE IMMEDIATE ('DROP TABLE "' || c.table_name || '" CASCADE CONSTRAINTS');
    END LOOP;
    
    FOR s IN (SELECT sequence_name FROM user_sequences) LOOP
    EXECUTE IMMEDIATE ('DROP SEQUENCE ' || s.sequence_name);
    END LOOP;
    
    END;
    

    Note that this works immediately after you run it. It does NOT produce a script that you need to paste somewhere (like other answers here). It runs directly on the DB.

    0 讨论(0)
  • 2020-12-04 05:11
    begin
      for i in (select 'drop table '||table_name||' cascade constraints' tbl from user_tables) 
      loop
         execute immediate i.tbl;
      end loop;
    end;
    
    0 讨论(0)
提交回复
热议问题