tags:

views:

517

answers:

6

I am using oracle DB to maintain more than 30 tables, how can I delete all the data from all the tables? I only want to delete the data but not drop the tables.

Thank you so much.

A: 

Google for "Oracle truncate"...

StaxMan
+2  A: 

Clone the schema and then drop the old tables?

BCS
+1: Simplest solution by far
Tom H.
Or the "real DBA" way of doing it: backup the full DB sans the data, and then rebuilt the DB (... the server ... from the motherboard on up ;).
BCS
+1  A: 

Try this script. Not tested (but what's the worst that can happen, you lose all your data? :))

Matthew Flaschen
Funny... yes...
User
+4  A: 

Generate a script to truncate (= remove all rows from) all tables:

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

And then execute the script.

Andomar
ouch .. add a where clause (where owner = <owner_name>) please or use User_Tables instead.
David
I did edit this answer, sorry about that but the all_tables thing was quite dangerous.
Neil Barnwell
very neat solution, thank you very much
Lily
+2  A: 

The potential drawback with a truncate is that it may fail on referential integrity constraints. So you'd want to disable foreign key constraints first, then do the truncate, then re-enable constraints. The 'plus' with cloning the schema (exp and imp) is that you could also drop and recreate the tablespace too (which you may want to do if you want to reclaim some physical disk space as a result of removing all the data).

Gary
+4  A: 

To address the issue of constraints, something like this should work:

BEGIN

    FOR T in (SELECT table_name FROM user_tables) LOOP
      EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' DISABLE ALL CONSTRAINTS';
    END LOOP;

    FOR T in (SELECT table_name FROM user_tables) LOOP
      EXECUTE IMMEDIATE 'TRUNCATE TABLE '||T.table_name;
    END LOOP;

    FOR T in (SELECT table_name FROM user_tables) LOOP
      EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' ENABLE ALL CONSTRAINTS';
    END LOOP;
END;
DCookie