Your cart is currently empty!
Author: alien
-
Khóa học miễn phí SQLite – Transactions nhận dự án làm có lương
SQLite – Transactions
A transaction is a unit of work that is performed against a database. Transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or automatically by some sort of a database program.
A transaction is the propagation of one or more changes to the database. For example, if you are creating, updating, or deleting a record from the table, then you are performing transaction on the table. It is important to control transactions to ensure data integrity and to handle database errors.
Practically, you will club many SQLite queries into a group and you will execute all of them together as part of a transaction.
Properties of Transactions
Transactions have the following four standard properties, usually referred to by the acronym ACID.
-
Atomicity − Ensures that all operations within the work unit are completed successfully; otherwise, the transaction is aborted at the point of failure and previous operations are rolled back to their former state.
-
Consistency − Ensures that the database properly changes states upon a successfully committed transaction.
-
Isolation − Enables transactions to operate independently of and transparent to each other.
-
Durability − Ensures that the result or effect of a committed transaction persists in case of a system failure.
Transaction Control
Following are the following commands used to control transactions:
-
BEGIN TRANSACTION − To start a transaction.
-
COMMIT − To save the changes, alternatively you can use END TRANSACTION command.
-
ROLLBACK − To rollback the changes.
Transactional control commands are only used with DML commands INSERT, UPDATE, and DELETE. They cannot be used while creating tables or dropping them because these operations are automatically committed in the database.
BEGIN TRANSACTION Command
Transactions can be started using BEGIN TRANSACTION or simply BEGIN command. Such transactions usually persist until the next COMMIT or ROLLBACK command is encountered. However, a transaction will also ROLLBACK if the database is closed or if an error occurs. Following is the simple syntax to start a transaction.
BEGIN; or BEGIN TRANSACTION;
COMMIT Command
COMMIT command is the transactional command used to save changes invoked by a transaction to the database.
COMMIT command saves all transactions to the database since the last COMMIT or ROLLBACK command.
Following is the syntax for COMMIT command.
COMMIT; or END TRANSACTION;
ROLLBACK Command
ROLLBACK command is the transactional command used to undo transactions that have not already been saved to the database.
ROLLBACK command can only be used to undo transactions since the last COMMIT or ROLLBACK command was issued.
Following is the syntax for ROLLBACK command.
ROLLBACK;
Example
Consider table with the following records.
ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 3 Teddy 23 Norway 20000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
Now, let”s start a transaction and delete records from the table having age = 25. Then, use ROLLBACK command to undo all the changes.
sqlite> BEGIN; sqlite> DELETE FROM COMPANY WHERE AGE = 25; sqlite> ROLLBACK;
Now, if you check COMPANY table, it still has the following records −
ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 3 Teddy 23 Norway 20000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
Let”s start another transaction and delete records from the table having age = 25 and finally we use COMMIT command to commit all the changes.
sqlite> BEGIN; sqlite> DELETE FROM COMPANY WHERE AGE = 25; sqlite> COMMIT;
If you now check COMPANY table is still has the following records −
ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 3 Teddy 23 Norway 20000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí SQLite – Views nhận dự án làm có lương
SQLite – Views
A view is nothing more than a SQLite statement that is stored in the database with an associated name. It is actually a composition of a table in the form of a predefined SQLite query.
A view can contain all rows of a table or selected rows from one or more tables. A view can be created from one or many tables which depends on the written SQLite query to create a view.
Views which are kind of virtual tables, allow the users to −
-
Structure data in a way that users or classes of users find natural or intuitive.
-
Restrict access to the data such that a user can only see limited data instead of a complete table.
-
Summarize data from various tables, which can be used to generate reports.
SQLite views are read-only and thus you may not be able to execute a DELETE, INSERT or UPDATE statement on a view. However, you can create a trigger on a view that fires on an attempt to DELETE, INSERT, or UPDATE a view and do what you need in the body of the trigger.
Creating Views
SQLite views are created using the CREATE VIEW statement. SQLite views can be created from a single table, multiple tables, or another view.
Following is the basic CREATE VIEW syntax.
CREATE [TEMP | TEMPORARY] VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE [condition];
You can include multiple tables in your SELECT statement in a similar way as you use them in a normal SQL SELECT query. If the optional TEMP or TEMPORARY keyword is present, the view will be created in the temp database.
Example
Consider table with the following records −
ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 3 Teddy 23 Norway 20000.0 4 Mark 25 Rich-Mond 65000.0 5 David 27 Texas 85000.0 6 Kim 22 South-Hall 45000.0 7 James 24 Houston 10000.0
Following is an example to create a view from COMPANY table. This view will be used to have only a few columns from COMPANY table.
sqlite> CREATE VIEW COMPANY_VIEW AS SELECT ID, NAME, AGE FROM COMPANY;
You can now query COMPANY_VIEW in a similar way as you query an actual table. Following is an example −
sqlite> SELECT * FROM COMPANY_VIEW;
This will produce the following result.
ID NAME AGE ---------- ---------- ---------- 1 Paul 32 2 Allen 25 3 Teddy 23 4 Mark 25 5 David 27 6 Kim 22 7 James 24
Dropping Views
To drop a view, simply use the DROP VIEW statement with the view_name. The basic DROP VIEW syntax is as follows −
sqlite> DROP VIEW view_name;
The following command will delete COMPANY_VIEW view, which we created in the last section.
sqlite> DROP VIEW COMPANY_VIEW;
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc