MySQL database basic study notes 05-TCL

TCL

Transaction control language
1. Transaction
Transaction: One or a group of sql statements form an execution unit, which is either all executed or not executed. If a SQL statement in the unit fails or generates an error, the entire unit will be rolled back. All affected data will be returned to the state before the transaction started. If all SQL statements in the unit are executed successfully, the transaction is executed smoothly.

The attributes of the transaction:
1. Atomicity (atomicity)
transaction is an indivisible unit of work, the operations in the transaction either occur or do not occur
2. Consistency (consistency)
transaction must make the database from a consistent state to Another consistency state
3. Isolation (isolation)
The execution of a transaction cannot be interfered by other transactions, that is, the internal operations and data used by a transaction are isolated from other concurrent transactions, and the various transactions executed concurrently cannot be mutually exclusive Interference
4. Durability
Once a transaction is committed, its changes to the data in the database are permanent, and other operations and database failures should not have any impact on it.

2. Syntax
Step 1: Open the transaction
set autocommit=0;
Step 2: Write sql statement
Step 3: End the transaction
commit;
rollback;

3. Concurrency issues
Dirty read: For two transactions T1, T2, T1 read the fields that have been updated by T2 but have not yet been committed. After that, if T2 rolls back, the content read by T1 is invalid.
Non-repeatable read: For two transactions T1, T2, T1 read a field, then T2 updates the field, after that, T1 reads the same field again, the value is different.
Phantom read: For two transactions T1, T2, T1 read a field from a table, and then T2 inserts some new rows in the table, after that, if T1 reads the same table again, there will be more A few lines.
You can set the isolation level

4. View
Meaning: virtual table
Application scenario: the
same query result is used in multiple places, and the query result SQL statement is more complicated

create:

create view 视图名
as
查询语句;

modify:

create or replace view 视图名
as
查询语句;
alter view 视图名
as
查询语句

delete:

drop view 视图名,...

View:

show create view 视图名;

View update:
1. SQL statements containing the following keywords are not allowed to be updated: grouping function, distinct, group by, having, union
2. Constant views are not allowed to update
3. Select contains sub-queries are not allowed to update
4. Join 5.from
one Views that cannot be updated
6. The subquery in the where clause refers to the table in the from clause

Comparison of view and table:
1. The view does not occupy physical space. Table occupies physical space
2. Views generally cannot be added, deleted, or modified

Guess you like

Origin blog.csdn.net/qq_44708714/article/details/105878050