Oracle retrieval data consistency and transaction recovery

Source: Internet
Author: User



To ensure data consistency, Oracle uses the UNDO record to retrieve database data, oracle always enables users to only view submitted data or data at a specific time point (select statement time point), and the UNDO record will be stored in the rollback segment, if the data is not submitted, the user retrieves the data from the UNDO record. (For example :)

 

1. ORACLE retrieval data consistency

First open a SecureCRT. (First session)

Create a table first


SQL> create table c(a int);  
Table created.  
SQL> alter table c add b number;  
Table altered.  
SQL> desc c   
 Name                                      Null?    Type  
 ----------------------------------------- -------- --------------------------------------------  
 A                                                  NUMBER(38)  
 B                                                  NUMBER  


Insert and submit data in the table


SQL> insert into c values(1,2);  
1 row created.  
SQL> insert into c values(3,4);  
1 row created.  
SQL> select * from c;  
         A          B  
---------- -----------------------------  
         1          2  
         3          4  
SQL> commit;  
Commit complete.  


Open another SecureCRT. (second session)

Query

 



SQL> select * from c;  
         A          B  
---------- --------------------------  
         1          2  
         3          4  

The first session changes the data in the table but does not submit

 

 
  1. SQL> update c set b=10 where a=1;  
  2. 1 row updated.  

Second session query (modified but not submitted to retrieve data in UNDO)

 

 
  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- --------------------------  
  4.          1          2  
  5.          3          4  

Submit the first session

 

 
  1. SQL> commit;  
  2. Commit complete.  

Second session query (it can be seen that data in the data segment can be retrieved only after submission)

 

 
  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- -------------------------  
  4.          1         10  
  5.          3          4  

Conclusion: if the user modifies the data but does not submit the data, other users retrieve the UNDO data segment, which ensures data consistency.

2. rollback data transaction recovery)

1. When the user updata data has not been submitted

 

 
  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- -----------------------------  
  4.          1          10  
  5.          3          4  
  6. SQL> update c set b=2 where a=1;  
  7. SQL> select * from c;  
  8.          A          B  
  9. ---------- -----------------------------  
  10.          1          2  
  11.          3          4  

Then the user suddenly regretted it and wanted to restore it to its original state.

 

 
  1. SQL> rollback;  
  2. Rollback complete.  
  3. SQL> commit;  
  4.    
  5. SQL> select * from c;  
  6.          A          B  
  7. ---------- -----------------------  
  8.          1         10  
  9.          3          4  

It can be seen that the rollback can be rolled back to the initial state when the user uses the rollback command.

2. When the user updata data has been submitted

After the user updata data has been submitted, the data can be returned to the Source Based on the SCN record.

View raw data first

 

 
  1. SQL> select * from c;  
  2.          A          B  
  3. ---------- ----------  
  4.          1         10  
  5.          3          4  

Locate SCN

 

 
  1. SQL> select current_scn from v$database;  
  2. CURRENT_SCN  
  3. -----------  
  4.      693636  

Delete the data in the table and submit it now.

 

SQL> delete from c;  
2 rows deleted.  
SQL> commit;         
Commit complete.
 

Query (no data in the table now)

 

SQL> select * from c;  
no rows selected  

Retrieve data of a specific SCN

 

SQL> select * from c as of scn 693636;  
         A          B  
---------- ----------  
         1         10  
         3          4 

Restore data


SQL> insert into c select * from c as of scn 693636;  
2 rows created.  
SQL> commit;  
Commit complete.  


Query now

 

SQL> select * from c;  
         A          B  
---------- ----------------------  
         1         10  
         3          4  

It can be seen that the data can be restored to a certain checkpoint Based on the SCN. If the SCN is converted to a time point, the data can be restored to a certain time point.

The preceding section describes the ORACLE Data Retrieval consistency and transaction recovery methods. This article is from the "pursuit" blog. to communicate with this blogger, click here.

  1. Design Method of super large Oracle database application system
  2. Three criteria for creating an Oracle database index
  3. Oracle performance optimization achieved through Partitioning technology
  4. A simple view on Oracle concurrent processing mechanism
  5. Solution to Oracle locking





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.