dbTalk Databases Forums  

drop and re-add schema

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss drop and re-add schema in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
ddog
 
Posts: n/a

Default drop and re-add schema - 11-12-2007 , 08:25 PM






What are the commands to drop a schema you are logged in to (or is
this even possible?) and recreate it? All of the tables need to be
dropped and recreated. Rather than drop 100+ tables one at a time, I
was hoping there would be a reasonable way to accomplish this.


Reply With Quote
  #2  
Old   
Geoff Muldoon
 
Posts: n/a

Default Re: drop and re-add schema - 11-12-2007 , 08:44 PM






In article <1194920706.612932.209790 (AT) 22g2000hsm (DOT) googlegroups.com>, ddog
says...
Quote:
What are the commands to drop a schema you are logged in to (or is
this even possible?) and recreate it? All of the tables need to be
dropped and recreated. Rather than drop 100+ tables one at a time, I
was hoping there would be a reasonable way to accomplish this.
Assuming that you can only operate within the schema (don't have DBA
rights where you could EXPort the schema and IMPort it with the
appropriate "with no data" flags), then I guess all you really want to do
is just to TRUNCATE the tables (completely empty the data in them all)
rather than drop and then recreate them ...

.... create a PL/SQL procedure which has a cursor returning all the table
names (select TABLE_NAME from USER_TABLES) and loop through this cursor
using EXECUTE IMMEDIATE dynamic SQL to truncate them.

GM


Reply With Quote
  #3  
Old   
fitzjarrell@cox.net
 
Posts: n/a

Default Re: drop and re-add schema - 11-13-2007 , 07:35 AM



On Nov 12, 8:44 pm, Geoff Muldoon <geoff.muld... (AT) trap (DOT) gmail.com>
wrote:
Quote:
In article <1194920706.612932.209... (AT) 22g2000hsm (DOT) googlegroups.com>, ddog
says...

What are the commands to drop a schema you are logged in to (or is
this even possible?) and recreate it? All of the tables need to be
dropped and recreated. Rather than drop 100+ tables one at a time, I
was hoping there would be a reasonable way to accomplish this.

Assuming that you can only operate within the schema (don't have DBA
rights where you could EXPort the schema and IMPort it with the
appropriate "with no data" flags), then I guess all you really want to do
is just to TRUNCATE the tables (completely empty the data in them all)
rather than drop and then recreate them ...

... create a PL/SQL procedure which has a cursor returning all the table
names (select TABLE_NAME from USER_TABLES) and loop through this cursor
using EXECUTE IMMEDIATE dynamic SQL to truncate them.

GM
A bit of overkill, really, as a simple SQL*Plus script and a spool
operation would do the same thing without context switching:

set head off pagesize 0 linesize 200 trimspool on verify off feedback
off termout off

select 'truncate table '||table_name||';'
from user_tables

spool trunc_usr_tbls.sql
/
spool off

set feedback on termout on head on pagesize 60

@trunc_usr_tbls



David Fitzjarrell



Reply With Quote
  #4  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: drop and re-add schema - 11-13-2007 , 03:10 PM



fitzjarrell (AT) cox (DOT) net wrote:
Quote:
On Nov 12, 8:44 pm, Geoff Muldoon <geoff.muld... (AT) trap (DOT) gmail.com
wrote:
In article <1194920706.612932.209... (AT) 22g2000hsm (DOT) googlegroups.com>, ddog
says...

What are the commands to drop a schema you are logged in to (or is
this even possible?) and recreate it? All of the tables need to be
dropped and recreated. Rather than drop 100+ tables one at a time, I
was hoping there would be a reasonable way to accomplish this.
Assuming that you can only operate within the schema (don't have DBA
rights where you could EXPort the schema and IMPort it with the
appropriate "with no data" flags), then I guess all you really want to do
is just to TRUNCATE the tables (completely empty the data in them all)
rather than drop and then recreate them ...

... create a PL/SQL procedure which has a cursor returning all the table
names (select TABLE_NAME from USER_TABLES) and loop through this cursor
using EXECUTE IMMEDIATE dynamic SQL to truncate them.

GM

A bit of overkill, really, as a simple SQL*Plus script and a spool
operation would do the same thing without context switching:

set head off pagesize 0 linesize 200 trimspool on verify off feedback
off termout off

select 'truncate table '||table_name||';'
from user_tables

spool trunc_usr_tbls.sql
/
spool off

set feedback on termout on head on pagesize 60

@trunc_usr_tbls



David Fitzjarrell

Hm, not really:

begin
for i in (select table_name from user_tables) loop
execute immediate 'truncate table '||i.table_name;
end loop;
end;
/


--
Regards,
Frank van Bortel

Top-posting is one way to shut me up...


Reply With Quote
  #5  
Old   
Terry Dykstra
 
Posts: n/a

Default Re: drop and re-add schema - 11-13-2007 , 04:09 PM



"Frank van Bortel" <frank.van.bortel (AT) gmail (DOT) com> wrote

Quote:
fitzjarrell (AT) cox (DOT) net wrote:
On Nov 12, 8:44 pm, Geoff Muldoon <geoff.muld... (AT) trap (DOT) gmail.com
wrote:
In article <1194920706.612932.209... (AT) 22g2000hsm (DOT) googlegroups.com>, ddog
says...

What are the commands to drop a schema you are logged in to (or is
this even possible?) and recreate it? All of the tables need to be
dropped and recreated. Rather than drop 100+ tables one at a time, I
was hoping there would be a reasonable way to accomplish this.
Assuming that you can only operate within the schema (don't have DBA
rights where you could EXPort the schema and IMPort it with the
appropriate "with no data" flags), then I guess all you really want to
do
is just to TRUNCATE the tables (completely empty the data in them all)
rather than drop and then recreate them ...

... create a PL/SQL procedure which has a cursor returning all the table
names (select TABLE_NAME from USER_TABLES) and loop through this cursor
using EXECUTE IMMEDIATE dynamic SQL to truncate them.

GM

A bit of overkill, really, as a simple SQL*Plus script and a spool
operation would do the same thing without context switching:

set head off pagesize 0 linesize 200 trimspool on verify off feedback
off termout off

select 'truncate table '||table_name||';'
from user_tables

spool trunc_usr_tbls.sql
/
spool off

set feedback on termout on head on pagesize 60

@trunc_usr_tbls



David Fitzjarrell

Hm, not really:

begin
for i in (select table_name from user_tables) loop
execute immediate 'truncate table '||i.table_name;
end loop;
end;
/


--
Regards,
Frank van Bortel

Top-posting is one way to shut me up...
The truncate will fail if you have referential constraints. You have to
disable those constraints first.

--
Terry Dykstra




Reply With Quote
  #6  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: drop and re-add schema - 11-14-2007 , 06:03 AM



On 13 nov, 23:09, "Terry Dykstra" <tddyks... (AT) forestoil (DOT) ca> wrote:
Quote:
"Frank van Bortel" <frank.van.bor... (AT) gmail (DOT) com> wrote in messagenews:fhd3s0$p7a$1 (AT) news1 (DOT) zwoll1.ov.home.nl...



fitzjarr... (AT) cox (DOT) net wrote:
On Nov 12, 8:44 pm, Geoff Muldoon <geoff.muld... (AT) trap (DOT) gmail.com
wrote:
In article <1194920706.612932.209... (AT) 22g2000hsm (DOT) googlegroups.com>, ddog
says...

What are the commands to drop a schema you are logged in to (or is
this even possible?) and recreate it? All of the tables need to be
dropped and recreated. Rather than drop 100+ tables one at a time, I
was hoping there would be a reasonable way to accomplish this.
Assuming that you can only operate within the schema (don't have DBA
rights where you could EXPort the schema and IMPort it with the
appropriate "with no data" flags), then I guess all you really want to
do
is just to TRUNCATE the tables (completely empty the data in them all)
rather than drop and then recreate them ...

... create a PL/SQL procedure which has a cursor returning all the table
names (select TABLE_NAME from USER_TABLES) and loop through this cursor
using EXECUTE IMMEDIATE dynamic SQL to truncate them.

GM

A bit of overkill, really, as a simple SQL*Plus script and a spool
operation would do the same thing without context switching:

set head off pagesize 0 linesize 200 trimspool on verify off feedback
off termout off

select 'truncate table '||table_name||';'
from user_tables

spool trunc_usr_tbls.sql
/
spool off

set feedback on termout on head on pagesize 60

@trunc_usr_tbls

David Fitzjarrell

Hm, not really:

begin
for i in (select table_name from user_tables) loop
execute immediate 'truncate table '||i.table_name;
end loop;
end;
/

--
Regards,
Frank van Bortel

Top-posting is one way to shut me up...

The truncate will fail if you have referential constraints. You have to
disable those constraints first.

--
Terry Dykstra
Eehhh - yes.

And one should add error trapping, too:

begin
for i in (select table_name from user_tables) loop
begin
execute immediate 'truncate table '||i.table_name;
exception
when others then
null;
end;
end loop;
end;
/

Maybe David is having a point after all (although he's running into
the same issue)



Reply With Quote
  #7  
Old   
fitzjarrell@cox.net
 
Posts: n/a

Default Re: drop and re-add schema - 11-14-2007 , 08:50 AM



On Nov 14, 6:03 am, Frank van Bortel <frank.van.bor... (AT) gmail (DOT) com>
wrote:
Quote:
On 13 nov, 23:09, "Terry Dykstra" <tddyks... (AT) forestoil (DOT) ca> wrote:





"Frank van Bortel" <frank.van.bor... (AT) gmail (DOT) com> wrote in messagenews:fhd3s0$p7a$1 (AT) news1 (DOT) zwoll1.ov.home.nl...

fitzjarr... (AT) cox (DOT) net wrote:
On Nov 12, 8:44 pm, Geoff Muldoon <geoff.muld... (AT) trap (DOT) gmail.com
wrote:
In article <1194920706.612932.209... (AT) 22g2000hsm (DOT) googlegroups.com>, ddog
says...

What are the commands to drop a schema you are logged in to (or is
this even possible?) and recreate it? All of the tables need to be
dropped and recreated. Rather than drop 100+ tables one at a time, I
was hoping there would be a reasonable way to accomplish this.
Assuming that you can only operate within the schema (don't have DBA
rights where you could EXPort the schema and IMPort it with the
appropriate "with no data" flags), then I guess all you really want to
do
is just to TRUNCATE the tables (completely empty the data in them all)
rather than drop and then recreate them ...

... create a PL/SQL procedure which has a cursor returning all the table
names (select TABLE_NAME from USER_TABLES) and loop through this cursor
using EXECUTE IMMEDIATE dynamic SQL to truncate them.

GM

A bit of overkill, really, as a simple SQL*Plus script and a spool
operation would do the same thing without context switching:

set head off pagesize 0 linesize 200 trimspool on verify off feedback
off termout off

select 'truncate table '||table_name||';'
from user_tables

spool trunc_usr_tbls.sql
/
spool off

set feedback on termout on head on pagesize 60

@trunc_usr_tbls

David Fitzjarrell

Hm, not really:

begin
for i in (select table_name from user_tables) loop
execute immediate 'truncate table '||i.table_name;
end loop;
end;
/

--
Regards,
Frank van Bortel

Top-posting is one way to shut me up...

The truncate will fail if you have referential constraints. You have to
disable those constraints first.

--
Terry Dykstra

Eehhh - yes.

And one should add error trapping, too:

begin
for i in (select table_name from user_tables) loop
begin
execute immediate 'truncate table '||i.table_name;
exception
when others then
null;
end;
end loop;
end;
/

Maybe David is having a point after all (although he's running into
the same issue)- Hide quoted text -

- Show quoted text -
I did not take into account any constraints and, yes, the truncates
will fail on parent tables when the child tables have data.
Presumably a second run of the script would truncate the parents after
the children have been successfully truncated.

My issue was with the original respondent blindly recommending PL/SQL
to do such a job when, in many cases, a simple SQL*Plus script would
suffice and probably be a bit more performant.


David Fitzjarrell



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.