How to drop all user tables?

前端 未结 9 575
后悔当初
后悔当初 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 04:50

    To remove all objects in oracle :

    1) Dynamic

    DECLARE
    CURSOR IX IS
    SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE ='TABLE' 
    AND OWNER='SCHEMA_NAME';
     CURSOR IY IS
     SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE 
    IN ('SEQUENCE',
    'PROCEDURE',
    'PACKAGE',
    'FUNCTION',
    'VIEW') AND  OWNER='SCHEMA_NAME';
     CURSOR IZ IS
     SELECT * FROM ALL_OBJECTS WHERE OBJECT_TYPE IN ('TYPE') AND  OWNER='SCHEMA_NAME';
    BEGIN
     FOR X IN IX LOOP
       EXECUTE IMMEDIATE('DROP '||X.OBJECT_TYPE||' SCHEMA_NAME.'||X.OBJECT_NAME|| ' CASCADE CONSTRAINT');
     END LOOP;
     FOR Y IN IY LOOP
       EXECUTE IMMEDIATE('DROP '||Y.OBJECT_TYPE||' SCHEMA_NAME.'||Y.OBJECT_NAME);
     END LOOP;
     FOR Z IN IZ LOOP
       EXECUTE IMMEDIATE('DROP '||Z.OBJECT_TYPE||' SCHEMA_NAME.'||Z.OBJECT_NAME||' FORCE ');
     END LOOP;
    END;
    /
    

    2)Static

        SELECT 'DROP TABLE "' || TABLE_NAME || '" CASCADE CONSTRAINTS;' FROM user_tables
            union ALL
            select 'drop '||object_type||' '|| object_name || ';' from user_objects 
            where object_type in ('VIEW','PACKAGE','SEQUENCE', 'PROCEDURE', 'FUNCTION')
            union ALL
            SELECT 'drop '
            ||object_type
            ||' '
            || object_name
            || ' force;'
            FROM user_objects
            WHERE object_type IN ('TYPE');
    
    0 讨论(0)
  • 2020-12-04 05:00

    If you just want a really simple way to do this.. Heres a script I have used in the past

    select 'drop table '||table_name||' cascade constraints;' from user_tables;
    

    This will print out a series of drop commands for all tables in the schema. Spool the result of this query and execute it.

    Source: https://forums.oracle.com/forums/thread.jspa?threadID=614090

    Likewise if you want to clear more than tables you can edit the following to suit your needs

    select 'drop '||object_type||' '|| object_name || ';' from user_objects where object_type in ('VIEW','PACKAGE','SEQUENCE', 'PROCEDURE', 'FUNCTION', 'INDEX')
    
    0 讨论(0)
  • 2020-12-04 05:02

    The easiest way would be to drop the tablespace then build the tablespace back up. But I'd rather not have to do that. This is similar to Henry's except that I just do a copy/paste on the resultset in my gui.

    SELECT
      'DROP'
      ,object_type
      ,object_name
      ,CASE(object_type)
         WHEN 'TABLE' THEN 'CASCADE CONSTRAINTS;'
         ELSE ';'
       END
     FROM user_objects
     WHERE
       object_type IN ('TABLE','VIEW','PACKAGE','PROCEDURE','FUNCTION','SEQUENCE')
    
    0 讨论(0)
  • 2020-12-04 05:02

    Please follow the below steps.

    begin
      for i in (select 'drop table '||table_name||' cascade constraints' tb from user_tables) 
      loop
         execute immediate i.tb;
      end loop;
      commit;
    end;
    purge RECYCLEBIN;
    
    0 讨论(0)
  • 2020-12-04 05:06
    BEGIN
       FOR cur_rec IN (SELECT object_name, object_type
                       FROM user_objects
                       WHERE object_type IN
                                 ('TABLE',
                                  'VIEW',
                                  'MATERIALIZED VIEW',
                                  'PACKAGE',
                                  'PROCEDURE',
                                  'FUNCTION',
                                  'SEQUENCE',
                                  'SYNONYM',
                                  'PACKAGE BODY'
                                 ))
       LOOP
          BEGIN
             IF cur_rec.object_type = 'TABLE'
             THEN
                EXECUTE IMMEDIATE 'DROP '
                                  || cur_rec.object_type
                                  || ' "'
                                  || cur_rec.object_name
                                  || '" CASCADE CONSTRAINTS';
             ELSE
                EXECUTE IMMEDIATE 'DROP '
                                  || cur_rec.object_type
                                  || ' "'
                                  || cur_rec.object_name
                                  || '"';
             END IF;
          EXCEPTION
             WHEN OTHERS
             THEN
                DBMS_OUTPUT.put_line ('FAILED: DROP '
                                      || cur_rec.object_type
                                      || ' "'
                                      || cur_rec.object_name
                                      || '"'
                                     );
          END;
       END LOOP;
       FOR cur_rec IN (SELECT * 
                       FROM all_synonyms 
                       WHERE table_owner IN (SELECT USER FROM dual))
       LOOP
          BEGIN
             EXECUTE IMMEDIATE 'DROP PUBLIC SYNONYM ' || cur_rec.synonym_name;
          END;
       END LOOP;
    END;
    /
    
    0 讨论(0)
  • 2020-12-04 05:06

    The simplest way is to drop the user that owns the objects with the cascade command.

    DROP USER username CASCADE
    
    0 讨论(0)
提交回复
热议问题