MySqlt 1105, TCL language

TCL language

1. Meaning

Transaction
Transaction: one or more sql statements form an execution unit, and a group of sql statements are either executed or not executed

2. Characteristics (ACID)

A Atomicity: A transaction is an indivisible whole, either executed or not executed
C Consistency: A transaction can switch data from one consistent state to another consistent state I Isolation: A transaction is not affected by other transactions Interference, multiple transactions are isolated from each other
D Persistence: Once a transaction is committed, it is permanently persisted to the local

3. Steps to use the transaction★ Understand:

Implicit (automatic) transaction: There is no obvious start and end, and a transaction itself can be automatically committed, such as insert, update, delete
Explicit transaction: With obvious start and end Use explicit transactions:
1 Turn on the transaction
set autocommit=0 ;
start transaction; #Can be omitted
2 Write a set of logical sql statements Note: sql statements support insert, update, delete
to set the rollback point: savepoint rollback rollback;
3 end transaction
Submit: commit;
rollback: rollback; rollback Go to the specified place: rollback to rollback roll call;

4. Concurrent transactions

1. How does the concurrency problem of transactions occur?
When multiple transactions operate the same data in the same database at the same time.
2. What are the concurrency problems?
Dirty read: A transaction reads data that has not been submitted by other transactions, and the read It is the data "updated" by other transactions
Non-repeatable read: a transaction reads multiple times, the result is different

Phantom reading: A transaction reads data that has not been committed by other transactions, but only reads the data "inserted" by other transactions
Other
3. How to solve concurrency problems by setting the isolation level to solve concurrency problems
4.
Dirty read read at isolation level uncommitted: read uncommitted
read committed: read committed
repeatable read: repeatable read
serializable: serialization
1. Meaning
Non-repeatable read phantom read
× × ×
√ × ×
√ √ ×
√ √ √
View
the new version of mysql5.1 The feature itself is a virtual table whose data comes from the table and is dynamically generated during execution.
Benefits:
1. Simplify the sql statement 2. Improve the reusability of sql 3. Protect the data of the base table and improve the security
2. Create

create view 视图名 as

Query statement;
3. Modification method 1:

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

Method 2:

alter view 视图名 as
查询语句

4. Delete

drop view 视图1,视图2,...;

Five, view

desc 视图名;
show create view 视图名;

6. Use 1. Insert
insert 2. Modify update 3. Delete delete 4. View select
Note: Views are generally used for query, not update, so views with the following characteristics are not allowed to be updated
1. Contains grouping functions, group by , distinct, having, union, 2join
3 constant view
4where subquery uses the table in from 5 uses a non-updatable view
7. Comparison between views and tables
Does the keyword occupy physical space?

View view table table
classification 1. System variables Occupy small, only save sql logic .
Generally used to query and save actual data addition, deletion, modification
and
query . session] variables like ''; If there is no explicit declaration of global or session, the default is session 2 to view the value of the specified system variable


select @@【global|session】. variable name; if there is no explicit declaration of global or session, the default is session
3 to assign values ​​to system variables
Method 1:

set [global|session] variable name=value; if there is no explicit declaration of global or session, the default is session
method 2:
set @@global. variable name=value; set @@variable name=value;
1. Global variable
server At the level, you must have super permission to assign values ​​to system variables, and the scope is the entire server, which is valid for all connections (sessions). 2. The
session variable server provides system variables for each connected client, and the scope is For the current connection (session)
2. Custom variable description:
1. User variable scope: for the current connection (session) effective position: inside the begin end, and can also be used outside:
1 Declaration and assignment:
set @variable name=value; or set @variable name:=value; or select @variable name:=value;
2 update value method one:
set @variable name=value; or set @variable name:=value; or select @variable name: = value;
method two:

select xx into @变量名 from;

3. Use
select @variable name;
2. Local variable
scope: only valid in the begin end where it is defined. Position: only in the begin end, and only in the first sentence. Use:
1
Declare the variable name type [ default value]; 2 assign or update
Method 1:

set 变量名=;set 变量名:=;select @变量名:=; 方式二:
select xx into 变量名 from;

3Use
select variable name;
1. Create
stored procedure
create procedure stored procedure name (parameter mode parameter name parameter type) begin
stored procedure body end
note: 1. parameter mode: in, out, inout, where in can be omitted 2. storage Each sql statement in the process body needs to end with a semicolon
2.
Call the stored procedure name (actual parameter list) For example:
call the parameters in the in mode: call sp1('value');
call the parameters in the out mode: set @name ; call sp1(@name); select @name;
call the parameters of inout mode: set @name=value; call sp1(@name); select @name; 3. View
the name of the stored procedure in show create procedure;
4. Delete
the drop procedure Stored procedure name;
function
1. Create

create function 函数名(参数名 参数类型) returns 返回类型 begin
函数体 end

Note: There must be a return statement in the function body
2. Call

select 函数名(实参列表); 

3. View

show create function 函数名; 

4. Delete

drop function 函数名;

Features: 1. If function
Function: Realize simple double-branch syntax: if (condition, value 1, value 2)
branch structure
Position:
Can be placed in any position as an expression
2. Case structure
Function: Realize multi-branch syntax 1:

case 表达式或字段 when1 then 语句1; when2 then 语句2; ..
else 语句n; end [case];

Syntax 2: case

when 条件1 then 语句1; when 条件2 then 语句2; ..
else 语句n;
end [case];

Position:
It can be placed at any position.
If it is placed outside the begin end, it can be used as an expression combined with other statements. If it is placed inside the begin end, it is generally used as
an
independent
statement
. then statement 1; elseif condition 2 then statement 2; ...
else statement n;
end if;
position:
can only be placed in begin end
position:
can only be placed in begin end
loop structure
features: can realize loop structure
comparison:
1 this The name can be omitted for all three loops, but if a loop control statement (leave or iterate) is added to the loop, the name must be added
2
loop is generally used to implement a simple infinite loop
while judge first and then execute
repeat execute first and then judge, unconditionally at least execute Once
1, while syntax:

【名称:】while 循环条件 do 循环体
end while 【名称】;

2. Loop syntax:
[name:] loop loop body
end loop [name];
3. repeat syntax:

【名称:】repeat 循环体 until 结束条件

end repeat [name];
2. Loop control statement
leave: similar to break, used to jump out of the loop iterate: similar to continue, used to end this loop and continue to the next one

Guess you like

Origin blog.csdn.net/weixin_44391817/article/details/128034036
TCL