Variables
System variables:
global variables
Session variables
Custom variables:
user variables
local variables
# a, system variables
Description: Variable provided by the system is not user-defined, belong to the server level
using the syntax:
1. Check all system variables
show global | [session ] the variables;
2. Check the condition of the parts of the system variables
Show, Ltd. Free Join | session the variables like '% char%';
3. view a system variable to specify the value of the
select @@ global | [session] system variable name.
4. assigned to a system variable
① set global | [session] system variable name = value.
② the SET @@, Ltd. Free Join |. [session] system variable name = value
* /
#####
# global variables
#####
/ *
scope: every time you start the server will assign initial values of all global variables, valid for all sessions, but can not cross restart
* /
. # 1 view all global variables
Global Variables Show;
. # 2 which satisfy the condition section view of global variables
Show Global Variables like '% A%';
. #. 3 view of a specified value of a global variable
SELECT @@ TX_ISOLATION;
. #. 4 for a specified global variable assignment
set @@ global.autocommit = 0;
#####
# session variables
#####
/ *
only for the current session
* /
# 1 view all session variables.
Show the session the Variables;
Show the Variables;
. See # 2 which satisfy the condition part of the local variables
show session variables like '% char% ';
# 3 view the value of a local variable to specify the
select @@ session.tx_isolation;
# 4 assigned to a given local variable
set @@ session.tx_isolation = 'read-uncommitted ';
#######
## custom variables
#######
/ *
Description: User-defined variables, not the system
using the steps:
declaration
assignment
use (view, compare, operations, etc.)
* /
# user variables
/ *
scope: for the current session
* /
## ① declare and initialize
#set @ user variable = value
#set @ user variable: = value
#select @ user variables: value =
## ② assignment (update user variable value)
# a way:
#set @ user variable = value
#set @ user variable: = value
#select @ User variables: value =
# Second way:
#select field into a variable name from the table ;
the SET @count = 1;
the SELECT COUNT (*) INTO @count from copy2;
# View variable
select @count;
#######
## local variables
#######
Scope: just define it begin end, the first sentence and begin end of the
statement:
DECLARE type variable name;
DECLARE Variable Name Type default value;
assignment
Method one: set or SELECT
set local variable name = value;
set local variable names: = value;
SELECT @ local variable names: = value
Second way: by SELECT into
SELECT field into a local variable name from the table;
use :
the SELECT local variable names
The difference between local variables and user variables:
scope and definition of the position of syntax used in
any current position of a user session session with @ variables must, without defining the type of
the local variable can only begin end begin end in the, and must begin end a first generally without @, necessary to define the type
# User variable
SET = @m. 1;
SET @n = 2;
SET + @sum @m = @n;
SELECT @sum;
#局部变量
/*
declare m int default 1;
declare n int default 2;
declaer sum int;
set sum = m + n;
select sum;
*/