Solved

Drop selected objects (demo tables) from user_tables as sys

Posted on 2011-03-08
7
576 Views
Last Modified: 2013-12-19
Dear experts and dbas,
I discovered that there is a vast number of users in our Oracle instance, that have the oracle demo tables (demo_customers, emp, salgrade etc) installed.

Since it takes a while to delete them all manually for each user, I'd like to know how I can drop these tables and correlated objects such as triggers, indizes and views at once.

Do you have a script to select a number of tables and delete all associated objects for these tables?

Please be so kind and share your knowledge with me!

Many thanks,

Sebastian
0
Comment
Question by:skahlert2010
  • 3
  • 3
7 Comments
 
LVL 4

Expert Comment

by:kevb258
Comment Utility
DROP USER user CASCADE;

that normally does the job for me
0
 

Author Comment

by:skahlert2010
Comment Utility
Thanks kevb258!

Well, I don't want to delete the user scheme itself! In fact I just want to delete the demo tables within all user schemes. I need to be able to specify the tables to delete in order to keep the custom user tables!

Any other ideas?

Brgds!

0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
Comment Utility
You can easily write a script to generate the necessary drop commands.  I would caution you before you do this:  Automatically dropping a tables based on name can be a bad thing.  What if I have a table called EMP but it is NOT the table you think it is...

You can generate the list of tables with:

select 'drop table ' || owner || '.' || table_name || ';' from dba_tables where table_name in ('DEMO_CUSTOMERS','EMP');

If all the referenced objects don't use dynamic SQL you can get a list of them from DBA_DEPENDENCIES.

Indexes should go away when you drop the tables.

Just as a double check:  look for invalid objects before and after the drops.  Anything new that is invalid should be manually checked.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:skahlert2010
Comment Utility
Hello slightwv!

Thanks for your comment! I intend to delete only tables like "DEMO_CUSTOMERS", which I am sure is an Oracle demo table. I have checked on the dba_dependencies and dba_constraints views.

My final question is if correlated constraints, triggers are also deleted when I drop the tables via dba_tables using the CASCADE option! Do you have any experience with this? Or do I need to delete these objects all manually?

Why am I asking these questions? For curiosity! I am just looking for the most efficient way to delete any objects related to demo_tables in all my schemes.

Thanks for your help!

Regards,

Sebastian
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>DEMO_CUSTOMERS", which I am sure is an Oracle demo table.

Not one I've ever heard of but Oracle has so many 'products'.  They just don't typically name sample tables 'DEMO'

>>Or do I need to delete these objects all manually?

Looks like triggers are automatically deleted as well even without cascade.

I have to verify this myself so I created a simple test.

You can expand this test to include any object types in question if needed.
drop table tab1 purge;
create table tab1 (col1 char(1));

create or replace trigger tab1_trig
before insert on tab1
for each row
begin
	null;
end;
/

show errors

select object_type from user_objects where object_name='TAB1_TRIG';

drop table tab1;

select object_type from user_objects where object_name='TAB1_TRIG';

Open in new window

0
 

Author Comment

by:skahlert2010
Comment Utility
Hello slightwv!

I just tested if triggers are deleted simultaneously with the table itself and can confirm it works!
However, the only objects that seem to be persistent are sequences. They are not deleted when the table is dropped and need to be deleted manually. The only difficulty is to find out,w hich respective sequence belongs to which table if names seq_names. If you have an idea, please don't hesitate to tell me!

I will close the question know and award you the points for your good help!

Thank you!
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
The problem is sequences are not tied to tables at the database layer.  They are set up and used at the application layer.  They are independent objects.

If your shop set them up one-to-one then hopefully the sequence names are tied to table names in some way.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now