Question thrown:
When executing multiple SQL statements at the same time, if one of the statements has an error and the other statements are executed correctly, the database data will not be on the line. With the business!
Transactions (Transaction)
Concept: A transaction performs a series of operations as a logical unit of work, which is performed as a whole, or all is not executed, ensuring data consistency!
Characteristics:
Classification:
Explicit transactions: Begins with a BEGIN transaction explicit transaction.
Implicit transactions:
Commit transactions automatically:
Each individual SQL statement is automatically defaulted to a transaction.
How does a transaction work?
When a transaction starts, all operations are first written to the log, and when the entire process is error-free, all operations in the transaction are committed once, and if there is an error in the execution, the previous action is revoked. Even when a database exception is caused by an external cause during the execution of a transaction, the next time you start the database, you will be able to complete the unfinished operation based on the log contents.
Creation and use of transactions
Basic steps:
Start transaction: Begin TRANSACTION
Commit TRANSACTION: Commit TRANSACTION
ROLLBACK TRANSACTION: ROLLBACK TRANSACTION
Use @ @error to determine whether you want to roll back or commit.
code example:
Ado. NET for transaction writing:
Index
The use of the index is used to improve the query speed, and so on after the actual need to contact to add!
sqlsever--Transactions and Indexes