How to rescue data after a mistake
The picture come from https://www.complexsql.com/oracle-flashback-query/

How to rescue data after a mistake

Oracle give you the opportunity to recover data also if you delete them or dropped the table.

Here 2 simple example very useful in a panic moment.

Drop table

1) Create the table

CREATE TABLE TEST01 (
        first_name VARCHAR2(10),
        last_name VARCHAR2(20));        

2) Check if the table exist

SELECT table_name FROM user_tables WHERE table_name = 'TEST01';        
1 row selected


3) Drop the table

DROP TABLE TEST01;        

4) Check if the table exist

SELECT table_name FROM user_tables WHERE table_name = 'TEST01';        
No rows returned1

5) Restore the table

FLASHBACK TABLE TEST01 TO BEFORE DROP;        

6) Check if the table exist

SELECT table_name FROM user_tables WHERE table_name = 'TEST01';        
1 row selected

6.1) Optionally, you can rename the table

FLASHBACK TABLE TEST01 TO BEFORE DROP RENAME TO test01_restored;        

6.2) Check if the table exist. Here I check if both tables exists

SELECT table_name FROM user_tables WHERE table_name = 'TEST01'
UNION ALL
SELECT table_name FROM user_tables WHERE table_name = 'TEST01_RESTORED';        
1 row selected

Delete data

1) Create table

CREATE TABLE TEST01 (
    first_name VARCHAR2(10),
    last_name VARCHAR2(20));        

2) Insert data

INSERT ALL
    INTO TEST01 (first_name, last_name) VALUES ('Andrea', 'Salzano')
    INTO TEST01 (first_name, last_name) VALUES ('Diego', 'Maradona')
    INTO TEST01 (first_name, last_name) VALUES ('Francesco', 'Totti')
SELECT 1 FROM DUAL;
COMMIT;        

3) Show the records

SELECT * FROM TEST01;        
3 rows selected

4) Delete records

DELETE FROM TEST01;
COMMIT;        

5) Show the records

No rows selected

6) Restore data

SELECT *
FROM TEST01 
AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '5' MINUTE);        
3 rows selected

You save the results in a temporary table and then choose the best way to proceed

CREATE TABLE TEST01_RES
AS
SELECT *
FROM TEST01
AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '5' MINUTE);        
3 rows selected

NOTE.




要查看或添加评论,请登录

Andrea Salzano的更多文章

  • 2 physical concepts in Oracle database

    2 physical concepts in Oracle database

    It could be strange, but yes, there are 2 concepts that came from physic that you can apply to your oracle database…

  • Using SQL like Excel - First example

    Using SQL like Excel - First example

    In my previous post, I have shown the relation between SQL MODEL clause and an Excel spreadsheet. In this post, you can…

  • Using SQL like Excel

    Using SQL like Excel

    SQL allow you to manage a result set as a spreadsheet. It is possible using the MODEL clause.

  • MATCH_RECOGNIZE order of application

    MATCH_RECOGNIZE order of application

    Starting 12c, Oracle introduced THE MATCH_RECOGNIZE analytic function to recognize patterns. IF you are NEW TO this…

  • Analytic Function preserve the partitioning

    Analytic Function preserve the partitioning

    Analytic Functions are great. They are SQL standard so all databases implement them.

  • ITOUG Tech Day 2019 (Milano)

    ITOUG Tech Day 2019 (Milano)

    Il primo giorno del Tech Day 2019 a Milano, si è concluso. Alla grande dire.

    2 条评论
  • ITOUG 2019: Oracle, Mediamente, R1

    ITOUG 2019: Oracle, Mediamente, R1

    L'ITOUG event che si terrà il 30/Gennaio/2019 a Milano ed il 01/Febbraio/2019 a Roma è stato possibile grazie a tre…

  • ITOUG 2019: ancora insieme

    ITOUG 2019: ancora insieme

    Ormai ci abbiamo preso gusto. E' bello iniziare un nuovo anno (più o meno :) sapendo che poi sarà tutto in discesa.

  • ITOUG 2019 Thech Days

    ITOUG 2019 Thech Days

    Finalmente ci siamo. il 30 Gennaio ed il 1 Febbraio IOTUG organizzerà un nuovo evento.

  • Blocking session

    Blocking session

    Some time ago, I wrote a post on how to show a chain of blocked sessions. In this days Kaley Crum wrote a new version…

社区洞察

其他会员也浏览了