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.
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) ?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With