ChatGPT解决这个技术问题 Extra ChatGPT

How to drop all user tables?

How can I drop all user tables in oracle?

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

How about dropping the constraints instead of disabling them?

R
Rafael Winterhalter
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;
/

it depends on what's you intended to do. you can also use drop user cascade but you need re-create the user.
This worked very well and it doesn't require that I have authority on the Oracle server to delete and re-add my user. Bravo. Great answer.
I get java.lang.IllegalArgumentException: No SQL selected for execution.. My idea is that cur_rec is not declared. How to declare it since this is some consisted out of bject_name, object_type?
I think the script could be dangerous. If you are connected as SYSDBA than it deletes ALL tables of ALL users and also ALL system tables. In practice you delete the whole DB. Be careful!
This is useful for the users have a permission to a certain schema but not the dba permission in development environment.
k
khylo

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')

This will fail with tables which have lower-case names (or have names starting with numbers or contain keywords). db<>fiddle. To fix this, the query should be select 'drop table "'||table_name||'" cascade constraints;' from user_tables;
k
kazanaki

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.


This works for me but I had to quote the table name by writing 'DROP TABLE "' || c.table_name || '" CASCADE CONSTRAINTS'. This is necessary, if the table names are lower case.
@ceving nice to know this! I only use upper case tables so I have never encountered this.
+1 @ceving also need in case your table name is a reserved word. Also, I'd add PURGE on the end of the DROP statement.
Fixed the quotes as ceving suggested.
ORA-24005: Inappropriate utilities used to perform DDL on AQ table
S
Stéphane Bruckert
begin
  for i in (select 'drop table '||table_name||' cascade constraints' tbl from user_tables) 
  loop
     execute immediate i.tbl;
  end loop;
end;

B
Brian

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

DROP USER username CASCADE

This isn't the right method. This is a method for deleting a user when the user has created objects, which requires using CASCADE to delete the user tables just before the user is deleted. Deleting the user is not the question that he asked.
It does accomplish the goal very effectively in particular if the schema is large. It seems like in your case avoiding any communication with your DBA is a high priority. I prefer solutions that foster a relationship with your DBA - in particular if you do not have DBA privileges.
@Brian you assume wrong. Sometimes there isn't a DBA at all, or he is in another company. Or the most usual case - he won't give you access to do what you need.
Being new to Oracle and I'm more familiar with MySQL; reseting a DB seems difficult. IN MySQL a USER is separate to a DATABASE. DROP USER username CASCADE worked for me. But in MySQL all I would have to do is DROP DATABASE and create a new one
This is effective to drop the entire database including the tables if you have 1) a 'CREATE USER' script handy with all the exact permissions to recreate the database; and 2) permission to run that script. If all you need to do is drop tables then other mentioned approaches (@kazanaki) are more appropriate.
w
wibeasley
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


don't forget the quotes, otherwise you cannot delete tables with lowercase letters
T
TJR

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')

А
Антон Ткаченко

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');

L
Lova Chittumuri

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;