Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: delete all the data from all available tables

Tags:

sql

oracle

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.

like image 493
Lily Avatar asked May 06 '09 22:05

Lily


People also ask

Can be used to delete only the complete data from the table all at once?

SQL Truncate is a data definition language (DDL) command. It removes all rows in a table.

How delete multiple records from multiple tables in SQL?

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?

Which method is used to remove all rows in all tables in a dataset?

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.


2 Answers

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; 
like image 119
michaldo Avatar answered Sep 21 '22 04:09

michaldo


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

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

And then execute the script.

like image 45
Andomar Avatar answered Sep 24 '22 04:09

Andomar