sqlite-Advanced-Transaction (Transaction)

Source: Internet
Author: User
Tags sqlite

transactions (Transaction)

A transaction (Transaction) is a unit of work that executes against a database. A transaction (Transaction) is a unit or sequence of work that is completed in a logical order, either manually by the user or by a database program that is automatically completed.

A transaction (Transaction) refers to one or more extensions that change the database. For example, if you are creating a record or updating a record or deleting a record from a table, you are executing a transaction on that table. It is important to control transactions to ensure data integrity and to handle database errors.

In fact, you can combine many of the SQLite queries into one group, putting all of them together as part of a transaction. Properties of a transaction

A transaction (Transaction) has the following four standard properties, usually based on the initial abbreviation of ACID:

    • atomicity (atomicity): ensures that all operations within the work unit are completed successfully, otherwise the transaction terminates in the event of a failure, and the previous operation is rolled back to its previous state.

    • Consistency (consistency): ensure that the database correctly changes state on a successfully committed transaction.

    • Isolation (Isolation): makes transactional operations independent and transparent to each other.

    • Persistence (Durability): ensures that the result or effect of a committed transaction persists in the event of a system failure.
Transaction control

Use the following command to control transactions:

    • Begin TRANSACTION: Begins transaction processing.

    • COMMIT: Save the changes, or you can use the END TRANSACTION command.

    • ROLLBACK: Roll Back the changes you made.

Transaction control commands are used only with the DML command INSERT, UPDATE, and DELETE. They cannot be used when creating tables or deleting tables, because these operations are automatically committed in the database.

----------------------------------------------------------------------

Example:

1: Open things-things handling-things to submit

Sqlite> BEGIN TRANSACTION;
Sqlite> INSERT into company VALUES (6, ' LiF ', A, ' Home ', 600000.0);
Sqlite> Select*from Company;
1| lia|20| china|100000.0
2| lib|25| america|200000.0
3| lic|30|earth|300000.0
5| lie|40| ground|500000.0
6| lif|45| home|600000.0
Sqlite> COMMIT TRANSACTION;
Sqlite> Select*from Company;
1| lia|20| china|100000.0
2| lib|25| america|200000.0
3| lic|30|earth|300000.0
5| lie|40| ground|500000.0
6| lif|45| home|600000.0

-------------------------------------------------------------------

2: Open things--things handling--Things handling rollback (thing handling canceled)

sqlite>. Dump
PRAGMA Foreign_keys=off;
BEGIN TRANSACTION;
CREATE TABLE Company (ID INT PRIMARY KEY isn't null, NAME TEXT NOT NULL, age INT not NULL, ADDRESS TEXT, SALARY REAL);
INSERT into "Company" VALUES (1, ' LiA ', +, ' China ', 100000.0);
INSERT into "Company" VALUES (2, ' LiB ', +, ' America ', 200000.0);
INSERT into "Company" VALUES (3, ' LiC ', +, ' earth ', 300000.0);
COMMIT;
sqlite> BEGIN TRANSACTION;
Sqlite> INSERT into company VALUES (4, ' LiC ', +, ' Air ', 400000.0);
Sqlite> Select*from Company;
1| lia|20| china|100000.0
2| lib|25| america|200000.0
3| lic|30|earth|300000.0
4| lic|35| air|400000.0
sqlite> ROLLBACK TRANSACTION;
Sqlite> Select*from Company;
1| lia|20| china|100000.0
2| lib|25| america|200000.0
3| lic|30|earth|300000.0
Sqlite>

sqlite-Advanced-Transaction (Transaction)

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.