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.
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;
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;
Delete all the data from all tables in oracle
DECLARE str VARCHAR2(100); BEGIN FOR i IN (SELECT object_name FROM user_objects WHERE object_type='TABLE' ) LOOP str := 'Truncate table '|| i.object_name; EXECUTE IMMEDIATE str; DBMS_OUTPUT.PUT_LINE('table data deleted :' || i.object_name); END LOOP; END;
For more info: http://www.oracleinformation.com/2014/10/delete-all-the-data-from-all-tables.html
Clone the schema and then drop the old tables?
A slight variation on Andomar's answer to truncate all tables for a specific user instead of only those of the current user:
SELECT 'TRUNCATE TABLE ' || owner || '.' || table_name || ';' FROM all_tables WHERE owner = 'user/schema'
Replace the user/schema
bit above with the name of the user/schema (between the quotes) you are interested in.
these two line script are the best
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
EXEC sp_MSForEachTable 'DELETE FROM ?'
GO