Play with Mysql Series - Part 16: Detailed explanation of variables

This is the 16th article in the Mysql series.

Environment: mysql5.7.25, demonstrated in cmd command.

The code enclosed by [] means optional, and the one separated by | symbol means one of them is optional.

When we use mysql, variables are often used. For example, when querying the system configuration, we can learn about it by viewing the system variables. When we need to modify some configurations of the system, we can also modify the values ​​of the system variables. .

When we need to do some batch processing scripts, we can use custom variables to reuse data. So variables are also very important, I hope everyone can master them.

Text content

  • Detailed explanation of the use of system variables

  • Detailed explanation of the use of custom variables

Variable classification

  • system variable

  • Custom variables

system variable

concept

System variables are defined by the system, not user-defined, and belong to the MySQL server level.

System variable classification

  • global variables

  • session variables

Steps for usage

View system variables
//1.查看系统所有变量
show [global | session] variables;
//查看全局变量
show global variables;
//查看会话变量
show session variables;
show variables;

The show keyword is used above

View system variables that meet the conditions

Fuzzy matching of variables specified by like

//查看满足条件的系统变量(like模糊匹配)
show [global|session] like '%变量名%';

The show and like keywords are used above.

View specified system variables
//查看指定的系统变量的值
select @@[global.|session.]系统变量名称;

Note selectthat @@there is a . symbol after the keywords global and session.

Assignment
//方式1
set [global|session] 系统变量名=值;

//方式2
set @@[global.|session.]系统变量名=值;

Notice:

As introduced in the above usage, global variables need to be added with the global keyword, and session variables need to be added with the session keyword. If not written, the default is the session level.

Keywords are used in the use of global variables @@. Custom variables will be introduced later. A @symbol is used in custom variables. This needs to be distinguished from global variables.

global variables

Scope

The mysql server sets initial values ​​for all system variables every time it is started.

We assign values ​​to system variables, which are valid for all sessions (connections) and can span connections, but cannot span restarts. After restarting, the MySQL server will assign initial values ​​to all system variables again.

Example

View all global variables
/*查看所有全局变量*/
show global variables;
View variables containing 'tx' characters
/*查看包含`tx`字符的变量*/
mysql> show global variables like '%tx%';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
| tx_read_only  | OFF             |
+---------------+-----------------+
2 rows in set, 1 warning (0.00 sec)

/*查看指定名称的系统变量的值,如查看事务默认自动提交设置*/
mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
|                   0 |
+---------------------+
1 row in set (0.00 sec)
Assign a value to a variable
/*为某个系统变量赋值*/
set global autocommit=0;
set @@global.autocommit=1;
mysql> set global autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
|                   0 |
+---------------------+
1 row in set (0.00 sec)

mysql> set @@global.autocommit=1;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
|                   1 |
+---------------------+
1 row in set (0.00 sec)

session variables

Scope

Valid for the current session (connection) and cannot cross connections.

Session variables are variables automatically set by MySQL to the current session when the connection is created.

Example

View all session variables
/*①查看所有会话变量*/
show session variables;
View session variables that meet the conditions
/*②查看满足条件的步伐会话变量*/
/*查看包含`char`字符变量名的会话变量*/
show session variables like '%char%';
View the value of a specified session variable
/*③查看指定的会话变量的值*/
/*查看事务默认自动提交的设置*/
select @@autocommit;
select @@session.autocommit;
/*查看事务隔离级别*/
select @@tx_isolation;
select @@session.tx_isolation;
Assign a value to a session variable
/*④为某个会话变量赋值*/
set @@session.tx_isolation='read-uncommitted';
set @@tx_isolation='read-committed';
set session tx_isolation='read-committed';
set tx_isolation='read-committed';

Effect:

mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> set tx_isolation='read-committed';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)

Custom variables

concept

Variables are user-defined, not system-provided.

use

使用步骤:
1. 声明
2. 赋值
3. 使用(查看、比较、运算)

Classification

  • user variables

  • local variables

user variables

Scope

Valid for the current session (connection), and the scope is the same as session variables.

User variables can be used anywhere, that is, they can be used inside the begin end or outside it.

use

Declare and initialize (requires initialization when declaring)
/*方式1*/
set @变量名=值;
/*方式2*/
set @变量名:=值;
/*方式3*/
select @变量名:=值;

Notice:

The above uses @conformity, and the above introduction uses two @symbols for global variables. Please pay attention to distinguish this point.

The colon before the = sign in set is optional, and there must be a colon before = in the select method.

Assignment (updating the value of a variable)
/*方式1:这块和变量的声明一样*/
set @变量名=值;
set @变量名:=值;
select @变量名:=值;

/*方式2*/
select 字段 into @变量名 from 表;

Pay attention to the two ways of selecting above.

use
select @变量名;

Comprehensive example

/*set方式创建变量并初始化*/
set @username='路人甲java';
/*select into方式创建变量*/
select 'javacode2018' into @gzh;
select count(*) into @empcount from employees;

/*select :=方式创建变量*/
select @first_name:='路人甲Java',@email:='[email protected]';
/*使用变量*/
insert into employees (first_name,email) values (@first_name,@email);

local variables

Scope

declare is used to define local variables. In stored procedures and functions, variables are defined through declare in begin...end and before the statement. and multiple variables can be defined by repeating

The scope of declare variables is similar to that in programming, here it is generally between the corresponding begin and end. After end, this variable has no effect and cannot be used. This is the same as programming.

use

statement
declare 变量名 变量类型;
declare 变量名 变量类型 [default 默认值];
Assignment
/*方式1*/
set 局部变量名=值;
set 局部变量名:=值;
select 局部变量名:=值;

/*方式2*/
select 字段 into 局部变量名 from 表;

Note: There is no @symbol in front of local variables

Use (view the value of a variable)
select 局部变量名;

Example

/*创建表test1*/
drop table IF EXISTS test1;
create table test1(a int PRIMARY KEY,b int);

/*声明脚本的结束符为$$*/
DELIMITER $$
DROP PROCEDURE IF EXISTS proc1;
CREATE PROCEDURE proc1()
BEGIN
  /*声明了一个局部变量*/
  DECLARE v_a int;

  select ifnull(max(a),0)+1 into v_a from test1;
  select @v_b:=v_a*2;
  insert into test1(a,b) select v_a,@v_b;
end $$

/*声明脚本的结束符为;*/
DELIMITER ;

/*调用存储过程*/
call proc1();
/*查看结果*/
select * from test1;

Stored procedures are used in the code. Detailed explanations of stored procedures are introduced in the next chapter.

delimiter keyword

When we write sql, how does mysql determine whether the sql has ended and can be executed?

A terminator is required. When mysql sees this terminator, it means that the previous statement can be executed. By default, mysql uses a semicolon as the terminator.

When we create a stored procedure or custom function, we write a large piece of SQL, which contains many semicolons. The entire creation statement is a whole and needs to be executed together. At this time, we cannot use semicolons as the terminator. .

Then we can delimitercustomize the end character through keywords.

usage:

delimiter 分隔符
The effect of the above example
mysql> /*创建表test1*/
mysql> drop table IF EXISTS test1;
Query OK, 0 rows affected (0.01 sec)

mysql> create table test1(a int PRIMARY KEY,b int);
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql> /*声明脚本的结束符为$$*/
mysql> DELIMITER $$
mysql> DROP PROCEDURE IF EXISTS proc1;
    -> CREATE PROCEDURE proc1()
    -> BEGIN
    ->   /*声明了一个局部变量*/
    ->   DECLARE v_a int;
    ->
    ->   select ifnull(max(a),0)+1 into v_a from test1;
    ->   select @v_b:=v_a*2;
    ->   insert into test1(a,b) select v_a,@v_b;
    -> end $$
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> /*声明脚本的结束符为;*/
mysql> DELIMITER ;
mysql>
mysql> /*调用存储过程*/
mysql> call proc1();
+-------------+
| @v_b:=v_a*2 |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

Query OK, 1 row affected (0.01 sec)

mysql> /*查看结果*/
mysql> select * from test1;
+---+------+
| a | b    |
+---+------+
| 1 |    2 |
+---+------+
1 row in set (0.00 sec)

Comparison between user variables and local variables

Scope Define location grammar
user variables current session session anywhere Add @sign without specifying type
local variables Define between his begin and end The first sentence in begin end Without @a sign, you need to specify the type

Summarize

  • This article provides a detailed explanation of the use of system variables and custom variables. The knowledge points are relatively detailed. You can read them several times to deepen your understanding.

  • System variables can set some configuration information of the system and will be restored after the database is restarted.

  • Session variables can set some configuration information of the current session and take effect on the current session.

  • Local variables created by declare are often used in the creation of stored procedures and functions.

  • Scope: Global variables are valid for the entire system, session variables are valid for the current session, user variables are valid for the current session, and local variables are valid for begin and end.

  • Note that `@@` is used in global variables, `@` is used in user variables, and local variables do not have this symbol.

  • The `delimiter` keyword is used to declare the end of the script

Guess you like

Origin blog.csdn.net/weixin_46228112/article/details/132731132