SQL is required. Note chapter 20th to manage transaction processing. SQL is required.

Source: Internet
Author: User
Tags savepoint sql server example

SQL is required. Note chapter 20th to manage transaction processing. SQL is required.
20.1 Transaction Processing

Transaction processing can be used to maintain the integrity of the database,It ensures that batch SQL operations are either completely executed or not executed at all.
Relational Database Design stores data in multiple tables, making it easier to manipulate, maintain, and reuse data. There is no need to look at how and why to design relational databases. To some extent, a well-designed database model is associated.
Transaction refers to a group of SQL statements.
Rollback refers to the process of revoking a specified SQL statement.
Commit refers to writing unstored SQL statement results to the database table.
A savepoint is a temporary placeholder (placeholder) set in transaction processing. You can release a rollback (different from the process of rolling back the entire transaction ).
Which statements can be rolled back?
Transaction processing is used to manage INSERT, UPDATE, and DELETE statements. The SELECT statement cannot be rolled back or the CREATE or DROP operation cannot be rolled back.These statements can be used in transaction processing, but they are not revoked during rollback.

20.2 control transaction processing

Differences in transaction processing: Different DBMS syntaxes are different for implementing transaction processing.
The key to managing transaction processing is to split SQL statement components into logical blocks and specify when data should be rolled back and when data should not be rolled back.

BEGIN TRANSACTION……COMMIT TRANSACTION
20.2.1 use ROLLBACK

The ROLLBACK command of SQL is used to roll back SQL statements.

DELETE FROM Order;ROLLBACK;
COMMIT 2.2 Use COMMIT

Generally, SQL statements are executed and written directly for database tables. This is the so-called implicit commit, that is, the commit operation is automatically performed. However, in transaction processing blocks, commit is not performed implicitly.
SQL Server example

BEGIN TRANSACTIONDELETE OrderItems WHERE order_num = 12345DELETE Orders Orders WHERE order_num = 12345COMMIT TRANSACTION;

Example in Oracle

DELETE OrderItems WHERE order_num = 12345;DELETE Orders Orders WHERE order_num = 12345;COMMIT;

Delete the full order 12345 from the system. Because two database tables are updated, the transaction processing block ensures that the order is not partially deleted. The final COMMIT statement only writes the change when no error occurs. If the first DELETE takes effect, but the second fails, the DELETE statement is not submitted.

4.1.2.3 use retention point

Simple ROLLBACK and COMMIT statements can be used to write or cancel the entire transaction processing. However, this is only possible for simple transaction processing. More complex transaction processing may require partial commit or rollback.
To support partial transaction rollback, placeholders must be placed in the appropriate position in the transaction processing module. In this way, if you want to roll back, you can roll back to a placeholder.
Create a placeholder in MySQL and Oracle using the following SAVEPOINT statement:

SAVEPOINT delete1;ROLLBACK TO delete1;

The more reserved points, the better.: You can set any number of reserved points in the SQL code. The more reserved points, the better. Why? Because the more reserved points, the more flexible you can roll back as you wish.

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.