Oracle Database Data Sheet Data Clear method

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Weixiaohuai/article/details/91040261

I. Introduction

In a recent test version of the project made when the export dmp when accidentally put some dirty data development library guide came out, after the test there imported into a bunch of non-standard data, in order not to affect the test results, then summed up a database data tables quickly emptied of all data.

Second, the method

(1) The first method: achieved step by step

 [A] The first step: a ban on all foreign key constraints.

Open plsql, create a new query window, enter:

SELECT 'ALTER TABLE ' || table_name || ' disable CONSTRAINT ' ||

       constraint_name || ';'

  FROM user_constraints

 where CONSTRAINT_TYPE = 'R';

Open the window to be able to execute the sql

[B] Step two: Delete all the contents of the table with a delete or truncate

SELECT 'DELETE FROM ' || table_name || ';'

  FROM USER_TABLES

 ORDER BY TABLE_NAME;

[C] The third step: enable all foreign key constraints

SELECT 'ALTER TABLE ' || table_name || ' enable CONSTRAINT ' ||

       constraint_name || ';'

  FROM user_constraints

 where CONSTRAINT_TYPE = 'R';

Turn after completing the implementation of these three steps, we found data in the database all the data tables are empty.

(2) The second method: execute the generated script sql, then the uniform implementation

[A] plsql open query window, execute the following script plsql

begin
  dbms_output.put_line('--禁用外键SQL【开始】');
  for var1 in (SELECT 'ALTER TABLE ' || table_name || ' disable CONSTRAINT ' ||
                      constraint_name || ';' as sqltext
                 FROM user_constraints
                where CONSTRAINT_TYPE = 'R') loop
    dbms_output.put_line(var1.sqltext);
  end loop;
  dbms_output.put_line('--禁用外键SQL【结束】');
  dbms_output.new_line;

  dbms_output.put_line('--删除表记录SQL【开始】');
  for var1 in (SELECT 'DELETE FROM ' || table_name || ';' as sqltext
                 FROM USER_TABLES
                ORDER BY TABLE_NAME) loop
    dbms_output.put_line(var1.sqltext);
  end loop;
  dbms_output.put_line('--删除表记录SQL【结束】');
  dbms_output.new_line;

  dbms_output.put_line('--启用外键SQL【开始】');
  for var1 in (SELECT 'ALTER TABLE ' || table_name || ' enable CONSTRAINT ' ||
                      constraint_name || ';' as sqltext
                 FROM user_constraints
                where CONSTRAINT_TYPE = 'R') loop
    dbms_output.put_line(var1.sqltext);
  end loop;
  dbms_output.put_line('--启用外键SQL【结束】');
  dbms_output.new_line;
end;

[B] and then copy all of the output sql in this tab

[C] the uniform implementation of sql:

Recommended to use the second method is more simple and convenient, but if so, all related to the deletion of data should exercise caution in a production environment, to prevent users from data loss, if you can consider when there is a need to clear data using this method .

 

 

Guess you like

Origin blog.csdn.net/Weixiaohuai/article/details/91040261