views:

197

answers:

3

How can I drop all user tables in oracle?

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

+3  A: 
BEGIN
   FOR cur_rec IN (SELECT object_name, object_type
                     FROM user_objects
                    WHERE object_type IN
                             ('TABLE',
                              'VIEW',
                              'PACKAGE',
                              'PROCEDURE',
                              'FUNCTION',
                              'SEQUENCE'
                             ))
   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;
END;
/
Henry Gao
thanks a lot :)
szaman
it depends on what's you intended to do. you can also use drop user cascade but you need re-create the user.
Henry Gao
+1  A: 

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

DROP USER username CASCADE
Brian
A: 

I have another problem. How can I execute truncate table on all user tables? There is the same problem with constraints.

szaman
Please ask this as a new question.
APC