SQL must know - manage transaction processing

"SQL must know must know" reading notes

1. Transaction processing

Use transaction processing to maintain database integrity by ensuring that batches of SQL operations are either fully executed or not executed at all.

Here are a few terms you need to know about transaction processing:

  • A transaction refers to a set of SQL statements;
  • Rollback refers to the process of undoing the specified SQL statement;
  • Commit refers to writing the result of an unstored SQL statement to a database table;
  • A savepoint refers to a temporary placeholder set in a transaction over which rollbacks can be distributed (as opposed to rolling back an entire transaction).

Hint: Which statements can be rolled back?
Transaction processing is used to manage INSERT, UPDATE, and DELETE statements. A SELECT statement cannot be rolled back (and it is not necessary to roll back a SELECT statement), nor can a CREATE or DROP operation be rolled back. Transactions can use these statements, but they are not undone when rolled back.

2. Control transaction processing

The key to managing transactions is to decompose SQL statement groups into logical blocks and specify when data should and should not be rolled back.

BEGIN TRANSACTION
...
COMMIT TRANSACTION

-- 或者
START TRANSACTION
...

-- 或者
SET TRANSACTION
...

-- 或者
BEGIN
...

Typically, COMMIT is used to save changes and ROLLBACK is used to undo.

3. Use ROLLBACK

DELETE FROM Orders;
ROLLBACK;

4. Use COMMIT

General SQL statements are directly executed and written against database tables. This is called implicit commit, i.e. commit (write or save) operations are done automatically.

Within a transaction block, commits do not occur implicitly. However, different DBMSs do this differently. Some DBMSs handle the transaction side by implicit commit, others don't.

To make an explicit commit, use the COMMIT statement.

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

5. Use Retention Points

With simple ROLLBACK and COMMIT statements, entire transactions can be written or undone. However, this is only possible for simple transactions, and complex transactions may require partial commits or rollbacks.

To support rolling back partial transactions, placeholders must be placed at appropriate locations within the transaction block. This way, if you need to fall back, you can fall back to a placeholder.

In SQL, execution placeholders are called reserve points. To create placeholders in MariaDB, MySQL and Oracle, use the SAVEPOINT statement.

-- MariaDBMySQLOracle
SAVEPOINT delete1;

-- SQL Serve
SAVE TRANSACTION delete1;

To fall back to the save point given in this example, in SQL you can do the following:

ROLLBACK TRANSACTION delete1;

In MariaDB, MySQL, Oracle, proceed as follows:

ROLLBACK TO delete1;

The following should be a complete SQL Server example:

BEGIN TRANSACTION
INSERT INTO Customers(cust_id, cust_name)
VALUES('10000010', 'Toy Emporium');
SAVE TRANSACTION StartOrder;
INSERT INTO Orders(order_num, order)_date, cust_id)
VALUES(20100, '2001/12/1', '10000010')
IF @@ERROR <> ROLLBACK TRANSACTION StartOrder;
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20100,1,'BRO1',100,5.49);
IF @@ERROR <> ROLLBACK TRANSACTION StartOrder;
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20100,2,'BR03',100,10.99);
IF @@ERROR <> ROLLBACK TRANSACTION StartOrder;
COMMIT TRANSACTION

A holdpoint is defined after the first INSERT statement, so if any of the following INSERT operations fail, the transaction is most recently rolled back here. In SQL Server, a variable called @@ERROR can be checked to see if the operation was successful. (Other DBMSs use different functions or variables to return this information.) If @@ERROR returns a non-zero value, indicating that an error occurred, the transaction rolls back to the reserved point. If the entire transaction is successful, issue a COMMIT to preserve the data.

Tip: The more retention points
, the better You can set as many retention points as you want in the SQL code, the more the better. why? Because the more points you keep, the more flexibility you have for rolling back.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324809772&siteId=291194637