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.
SQL Truncate is a data definition language (DDL) command. It removes all rows in a table.
The syntax also supports deleting rows from multiple tables at once. To delete rows from both tables where there are matching id values, name them both after the DELETE keyword: DELETE t1, t2 FROM t1 INNER JOIN t2 ON t1.id = t2.id; What if you want to delete nonmatching rows?
To delete all the rows in a table, always use TRUNCATE TABLE. TRUNCATE TABLE which is faster than a SQL delete statement and it uses fewer system and transaction-log resources.
There is no command 'ALTER TABLE XXX DISABLE ALL CONSTRAINTS'
I propose this;
BEGIN FOR c IN (SELECT table_name, constraint_name FROM user_constraints WHERE constraint_type = 'R') LOOP EXECUTE IMMEDIATE ('alter table ' || c.table_name || ' disable constraint ' || c.constraint_name); END LOOP; FOR c IN (SELECT table_name FROM user_tables) LOOP EXECUTE IMMEDIATE ('truncate table ' || c.table_name); END LOOP; FOR c IN (SELECT table_name, constraint_name FROM user_constraints WHERE constraint_type = 'R') LOOP EXECUTE IMMEDIATE ('alter table ' || c.table_name || ' enable constraint ' || c.constraint_name); END LOOP; END;
Generate a script to truncate (= remove all rows from) all tables:
select 'truncate table ' || table_name || ';' from user_tables
And then execute the script.
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