Home » SQL & PL/SQL » SQL & PL/SQL » drop all tables belong to a user
drop all tables belong to a user [message #241196] Mon, 28 May 2007 14:47 Go to next message
t7wang
Messages: 2
Registered: May 2007
Junior Member
Hi,

I was wondering if there is a way to drop all tables belong to a specific user in Oracle. Thanks.
Re: drop all tables belong to a user [message #241200 is a reply to message #241196] Mon, 28 May 2007 15:22 Go to previous messageGo to next message
michael_bialik
Messages: 611
Registered: July 2006
Senior Member
Cann't you just drop the user using

DROP USER xxx CACADE;


and re-create it later?
Re: drop all tables belong to a user [message #241201 is a reply to message #241200] Mon, 28 May 2007 15:26 Go to previous messageGo to next message
t7wang
Messages: 2
Registered: May 2007
Junior Member
Actually, I would like to drop all tables belong to the user without dropping the user. Is that possible? thanks
Re: drop all tables belong to a user [message #241203 is a reply to message #241196] Mon, 28 May 2007 16:57 Go to previous messageGo to next message
DreamzZ
Messages: 1666
Registered: May 2007
Location: Dreamzland
Senior Member
set heading off
spool /export/home/oracle/drop.sql

select 'drop table '||table_name||' cascade constraints ;'
from dba_tables
where owner='ABC';

spool off;

@/export/home/oracle/drop.sql
Re: drop all tables belong to a user [message #241207 is a reply to message #241196] Mon, 28 May 2007 18:39 Go to previous messageGo to next message
saibal
Messages: 111
Registered: March 2007
Location: India
Senior Member
You could try something like this:

begin
for x in ( select *from all_objects where owner = 'some_user' )
 loop
    execute immediate 'drop some_user.' || x.object_type;
  end loop;
end;

[Updated on: Mon, 28 May 2007 18:41]

Report message to a moderator

Re: drop all tables belong to a user [message #241208 is a reply to message #241196] Mon, 28 May 2007 19:01 Go to previous messageGo to next message
BlackSwan
Messages: 25036
Registered: January 2009
Location: SoCal
Senior Member
Saibal,
did you test posted code?
SQL> connect Id10t/untested
Connected.
SQL> create table lazy (id number);

Table created.

SQL> set term on echo on
  1  begin
  2  for x in ( select * from all_objects where owner = 'Id10t' )
  3   loop
  4      execute immediate 'drop Id10t.' || x.object_type;
  5    end loop;
  6* end;
SQL> /

PL/SQL procedure successfully completed.

SQL> desc lazy
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                                 NUMBER

[Updated on: Mon, 28 May 2007 21:47] by Moderator

Report message to a moderator

Re: drop all tables belong to a user [message #241220 is a reply to message #241208] Mon, 28 May 2007 22:36 Go to previous messageGo to next message
saibal
Messages: 111
Registered: March 2007
Location: India
Senior Member
@anacedent,
True,I posted in haste and I apologize for not testing before posting. Actually I was thinking more about the algo, rather than the actual code. Here is the revised version:

system@DBASE-SQL>>create user g00gle identified by isg00glebroken;

User created.
system@DBASE-SQL>>grant connect, resource to g00gle;

Grant succeeded.

system@DBASE-SQL>>grant all on scott.emp to g00gle;

Grant succeeded.

system@DBASE-SQL>>conn g00gle/isg00glebroken
Connected.
g00gle@DBASE-SQL>>create table nitpicking as select * from scott.emp;

Table created.

g00gle@DBASE-SQL>>select * from nitpicking;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.

g00gle@DBASE-SQL>>begin
  2  for x in (select * from all_objects where owner='G00GLE') loop
  3  execute immediate 'drop'||'  '||x.object_type||'  '||x.object_name;
  4  end loop;
  5  end;
  6  /

PL/SQL procedure successfully completed.

g00gle@DBASE-SQL>>select * from nitpicking;
select * from nitpicking
              *
ERROR at line 1:
ORA-00942: table or view does not exist


Hopefully, this ought to work.

Regards

[Updated on: Mon, 28 May 2007 22:37]

Report message to a moderator

Re: drop all tables belong to a user [message #241221 is a reply to message #241220] Mon, 28 May 2007 22:45 Go to previous messageGo to next message
rleishman
Messages: 3724
Registered: October 2005
Location: Melbourne, Australia
Senior Member
You may need some additional cleverness to handle cases when you cannot drop an object because of dependencies.

You could add conditional keywords like "FORCE" and "INCLUDING TABLES" and "CASCADE CONSTRAINTS", or perhaps order the objects using ALL_DEPENDENCIES and CONNECT BY.

You may also need error handling, like when you try to drop a Package Body, but its already gone because you dropped the package spec.

Ross Leishman
Re: drop all tables belong to a user [message #241222 is a reply to message #241221] Mon, 28 May 2007 22:51 Go to previous messageGo to next message
saibal
Messages: 111
Registered: March 2007
Location: India
Senior Member
Absolutely true. And perhaps, you missed telling me about purging the tables(if the OP is on 10g).
Re: drop all tables belong to a user [message #241258 is a reply to message #241222] Tue, 29 May 2007 00:38 Go to previous messageGo to next message
Frank
Messages: 7880
Registered: March 2000
Senior Member
Look, saibal, you came up with a solution after Dreamzz provided a good and working solution.
Don't get upset if people point you to the fact that your solution simply is not working.
Re: drop all tables belong to a user [message #241271 is a reply to message #241258] Tue, 29 May 2007 01:19 Go to previous messageGo to next message
saibal
Messages: 111
Registered: March 2007
Location: India
Senior Member
Frank,
You will see that I apologized for posting in haste, and tried to make amends, by revising the code, which actually worked. I am not at all upset if somebody points out that a solution doesn't work, but the manner of pointing that out by one of the respondents in this case wasn't perhaps the greatest act of diplomacy in the world--look at the username and table name. I was wrong in following suit with er...unconventional username/tablename combo, and I will certainly keep things in check in future. But having said that, maybe you will accept that I do liven up things now and then in the forum!
Regards
Re: drop all tables belong to a user [message #274695 is a reply to message #241271] Tue, 16 October 2007 19:14 Go to previous messageGo to next message
nobodyman
Messages: 1
Registered: October 2007
Location: Arizona
Junior Member
I'm with saibal -- replying to snarky posts in kind is no great offense. In that vein, I'll add my own:

select  * from  personality_traits where (userid = 'anacedent' and trait = 'tact')or (userid = 'frank' and trait 'sense of humor');

USERID       TRAIT
------------ ---------------

0 ROWS SELECTED
Re: drop all tables belong to a user [message #274734 is a reply to message #274695] Wed, 17 October 2007 00:59 Go to previous messageGo to next message
Michel Cadot
Messages: 64103
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
You query is not valid, there is a syntax error.

Regards
Michel
Re: drop all tables belong to a user [message #274739 is a reply to message #274695] Wed, 17 October 2007 01:06 Go to previous message
Maaher
Messages: 7062
Registered: December 2001
Senior Member
This old thread can be closed, I think. We're drifting off topic and bringing it up after 5 months does not add anything to the original question. If the OP hasn't succeeded in performing his task by now, I suggest that he changes his career.

http://www.orafaq.com/forum/fa/448/0/

nobodyman, we all had a good laugh but here it ends. In the community hangout forum you can fool around if you feel the need.

MHE

[Updated on: Wed, 17 October 2007 01:07]

Report message to a moderator

Previous Topic: Threading in Pl/SQL
Next Topic: Integer number type
Goto Forum:
  


Current Time: Sun Dec 04 20:58:27 CST 2016

Total time taken to generate the page: 0.10235 seconds