Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all contents in a schema in Oracle

Tags:

sql

oracle

plsql

Is possible to delete all contents in scheme in Oracle? I found this script:

Begin for c in (select table_name from user_tables) loop execute immediate ('drop table '||c.table_name||' cascade constraints); end loop; End; 

But I would like to know if are there anything to drop everything in the schema, indexes,tables,contraints... but not the schema (drop user ...).

Thanks.

like image 491
zerosssa Avatar asked Apr 28 '15 17:04

zerosssa


People also ask

How do I drop all objects in a schema in Oracle?

select 'drop '||object_type||' '|| object_name || ';' from user_objects where object_type in ('VIEW','PACKAGE','SEQUENCE', 'PROCEDURE', 'FUNCTION', 'INDEX'); But what if this query is run by the DBA with a SYS or SYSDBA login (what all objects are present in the user_objects view when logged in using sys/sysdba user) ?

How do I delete a schema in Oracle?

Use the DROP USER statement to remove a database user and optionally remove the user's objects. When you drop a user, Oracle Database also purges all of that user's schema objects from the recycle bin.

How delete all values from column in Oracle?

You have three options in Oracle: Set all the values to NULL using update. Remove the column from the table. Set the column to unused.


1 Answers

Normally, it is simplest to drop and add the user. This is the preferred method if you have system or sysdba access to the database.

If you don't have system level access, and want to scrub your schema, the following sql will produce a series of drop statments, which can then be executed.

select 'drop '||object_type||' '|| object_name|| DECODE(OBJECT_TYPE,'TABLE',' CASCADE CONSTRAINTS','') || ';'  from user_objects 

Then, I normally purge the recycle bin to really clean things up. To be honest, I don't see a lot of use for oracle's recycle bin, and wish i could disable it, but anyway:

purge recyclebin; 

This will produce a list of drop statements. Not all of them will execute - if you drop with cascade, dropping the PK_* indices will fail. But in the end, you will have a pretty clean schema. Confirm with:

select * from user_objects 

Also, just to add, the Pl/sql block in your question will delete only tables, it doesn't delete all other objects.

ps: Copied from some website, was useful to me. Tested and working like a charm.

like image 120
venki Avatar answered Sep 20 '22 16:09

venki