How to delete all data from the database [message #666187] |
Wed, 18 October 2017 14:27 |
youngb912
Messages: 56 Registered: October 2007 Location: New York
|
Member |
|
|
Hello,
I have a dev database that I will like to delete/truncate/purge all the data. I don't want to drop the database. I want to delete all data for all schema and all tables. How can I do this?
|
|
|
|
|
Re: How to delete all data from the database [message #666195 is a reply to message #666189] |
Thu, 19 October 2017 02:58 |
gazzag
Messages: 1119 Registered: November 2010 Location: Bedwas, UK
|
Senior Member |
|
|
I do this regularly at my current site to refresh Dev/Test schemas from the Live environment. Essentially, I've written scripts that build dynamic SQL to:
1. Drop all tables in the relevant user schemas (i.e. exclude Oracle built-in/system ones)
2. Drop all objects in the relevant user schemas (i.e. exclude Oracle built-in/system ones)
3. Drop all public synonyms pertaining to those schemas.
Run a Data Pump export from the Live environment into the environment.
I don't do a DROP USER as BlackSwan suggests as some SYS grants can be lost and can be a pain to recreate. However, BlackSwan's DROP USER option has its advantages over my approach as Oracle will not drop a connected user i.e. it's difficult to run against the wrong environment by mistake!
[Edit: typo]
[Updated on: Thu, 19 October 2017 03:13] Report message to a moderator
|
|
|
Re: How to delete all data from the database [message #666196 is a reply to message #666195] |
Thu, 19 October 2017 04:13 |
cookiemonster
Messages: 13959 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
If you just want to clear out the data then the simplest thing to do is
1) loop over all foreign keys in user_constraints (constraint_type = 'R') and use dynamic sql to disable them
2) loop over user_tables and use dynamic sql to truncate each table
3) repeat step 1 but this time enable the foreign keys
|
|
|
Re: How to delete all data from the database [message #666202 is a reply to message #666196] |
Thu, 19 October 2017 09:54 |
youngb912
Messages: 56 Registered: October 2007 Location: New York
|
Member |
|
|
Thanks for the responses. My main goal is to make sure that the DDL for each table and schemas remain after the cleanup. I want to present the empty database to a third party without the data but they need the schemas and tables DDL.
|
|
|
|
|
|
Re: How to delete all data from the database [message #666206 is a reply to message #666202] |
Thu, 19 October 2017 12:36 |
John Watson
Messages: 8960 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
youngb912 wrote on Thu, 19 October 2017 15:54Thanks for the responses. My main goal is to make sure that the DDL for each table and schemas remain after the cleanup. I want to present the empty database to a third party without the data but they need the schemas and tables DDL.
I wouldn't do it your way at all. I'ld use Data Pump: export and import, using the option to exclude the rows.
|
|
|
|
|
|
Re: How to delete all data from the database [message #666216 is a reply to message #666209] |
Fri, 20 October 2017 06:42 |
youngb912
Messages: 56 Registered: October 2007 Location: New York
|
Member |
|
|
Issue resolved. Thanks to everyone for their contribution especially "BlackSwan" for his timely responses.
Here is the route I took:
Performed a Data Pump Export (to capture all newly created table in prod) from the prod db to the dev environment using the following:
compression=ALL
directory=data_pump_dir
dumpfile=full_PPRD-%U.dmp
reuse_dumpfiles=Y
filesize=4G
parallel=3
full=Y
content=METADATA_ONLY
logfile=full_export.log
Used the following to perform a Data Pump Import:
impdp system directory=data_pump_dir_dir dumpfile=full_PPRD_%U.dmp full=y content=METADATA_ONLY TABLE_EXISTS_ACTION=TRUNCATE parallel=3 logfile=full_import.log
My initial Data Pump command didn't include "TABLE_EXISTS_ACTION=TRUNCATE" which caused data to still exist in the tables after the import. I thought using content=METADATA_ONLY would have automatically truncate tables with existing data but I was wrong. To eliminate the data the TABLE_EXISTS_ACTION option must be used.
Thank you all.
|
|
|
Re: How to delete all data from the database [message #666218 is a reply to message #666216] |
Fri, 20 October 2017 07:56 |
|
BlackSwan
Messages: 26766 Registered: January 2009 Location: SoCal
|
Senior Member |
|
|
youngb912 wrote on Fri, 20 October 2017 04:42Issue resolved. Thanks to everyone for their contribution especially "BlackSwan" for his timely responses.
Here is the route I took:
Performed a Data Pump Export (to capture all newly created table in prod) from the prod db to the dev environment
Above is SERIOUSLY wrong procedurally!
All application code changes should be developed in DEV, tested in QA, before being released into Production.
Moving DDL changes from Prod to Dev shows a totally unprofessional & amateurish IT staff.
|
|
|