MySQL 中的变量分为 用户变量
、存储过程变量
、全局变量
和 会话变量
,而存储过程变量
是 MySQL 中使用的主要方式。
用户变量
用户变量使用 set
语句直接赋值,变量名以 @ 开头。它可以在一个会话的任何地方声明,作用域是整个会话。
用户变量定义成功后,可以使用 select 语句进行查看。
- 使用
set
定义一个 name 变量,并赋值。
[root@localhost][(none)]> set @name = "九皇叔叔";
Query OK, 0 rows affected (0.00 sec)
- 使用
select
查看变量.
[root@localhost][(none)]> select @name;
+--------------+
| @name |
+--------------+
| 九皇叔叔 |
+--------------+
1 row in set (0.00 sec)
注意
在一个会话内,用户变量只需初始化一次。在同一个会话内保存的都是上一次计算的结果。
- 使用
select
语句直接定义用户变量,并赋值。
[root@localhost][(none)]> select @age := 100;
+-------------+
| @age := 100 |
+-------------+
| 100 |
+-------------+
1 row in set (0.01 sec)
[root@localhost][(none)]> select @age;
+------+
| @age |
+------+
| 100 |
+------+
1 row in set (0.00 sec)
存储过程变量
存储过程变量以 declare
关键字声明,主要用于存储过程中,示例如下:
[root@localhost][testdb]>
[root@localhost][testdb]> DELIMITER //
[root@localhost][testdb]>
[root@localhost][testdb]> CREATE PROCEDURE hello_proc()
-> BEGIN
-> DECLARE say VARCHAR(20);
-> SET say = '你好';
-> SELECT say;
-> END;
-> //
DELIMITER ;
Query OK, 0 rows affected (0.06 sec)
[root@localhost][testdb]>
[root@localhost][testdb]> DELIMITER ;
[root@localhost][testdb]>
[root@localhost][testdb]>
[root@localhost][testdb]> call hello_proc();
+--------+
| say |
+--------+
| 你好 |
+--------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
[root@localhost][testdb]>
注意
由于存储过程变量只能存储过程中使用,因此存储过程变量也可以叫做局部变量,其作用域仅限于该存储过程的语句块。在该语句块执行完毕后,变量就消失了。
全局变量
全局变量将影响 MySQL 数据库服务器的整体,它的初始值可以在 my.cnf
文件中进行定义。当 MySQL 服务器启动时,它将所有全局变量初始化为默认值。
示例:
- 查看所有的全局变量
[root@localhost][(none)]> show global variables \G
... ...
*************************** 19. row ***************************
Variable_name: binlog_order_commits
Value: ON
*************************** 20. row ***************************
Variable_name: binlog_row_image
Value: FULL
... ...
- 模糊匹配全局变量
[root@localhost][(none)]> show global variables like '%connection%';
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_connection | utf8mb4 |
| collation_connection | utf8mb4_general_ci |
| max_connections | 3000 |
| max_user_connections | 0 |
+--------------------------+--------------------+
4 rows in set (0.00 sec)
[root@localhost][(none)]>
- 查询某一个全局变量的值
[root@localhost][(none)]> select @@max_connections;
+-------------------+
| @@max_connections |
+-------------------+
| 3000 |
+-------------------+
1 row in set (0.00 sec)
会话变量
MySQL 数据库服务器为每个连接的客户端维护一系列会话变量。在客户端连接 MySQL 实例时,会使用全局变量对客户端的会话变量进行初始化。客户端只能更改自己的会话变量,并且会话变量与用户变量一样,仅作用于当前连接的会话。
示例:
- 查看所有的会话变量。
[root@localhost][(none)]> show session variables \G
... ...
*************************** 500. row ***************************
Variable_name: thread_stack
Value: 262144
*************************** 501. row ***************************
Variable_name: time_format
Value: %H:%i:%s
... ...
- 模糊匹配会话变量
[root@localhost][(none)]> show session variables like '%connection%';
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_connection | utf8mb4 |
| collation_connection | utf8mb4_general_ci |
| max_connections | 3000 |
| max_user_connections | 0 |
+--------------------------+--------------------+
4 rows in set (0.00 sec)
- 查询某一个会话变量的值
[root@localhost][(none)]> select @@session.character_set_connection;
+------------------------------------+
| @@session.character_set_connection |
+------------------------------------+
| utf8mb4 |
+------------------------------------+
1 row in set (0.00 sec)
- 修改 会话变量的值
[root@localhost][(none)]> set @@session.character_set_connection=utf8;
Query OK, 0 rows affected (0.00 sec)
[root@localhost][(none)]>
[root@localhost][(none)]> select @@session.character_set_connection;
+------------------------------------+
| @@session.character_set_connection |
+------------------------------------+
| utf8 |
+------------------------------------+
1 row in set (0.00 sec)
退出在重新登录数据库,变量character_set_connection
又恢复默认值.
[root@localhost][(none)]> select @@session.character_set_connection;
+------------------------------------+
| @@session.character_set_connection |
+------------------------------------+
| utf8 |
+------------------------------------+
1 row in set (0.00 sec)
[root@localhost][(none)]>
[root@localhost][(none)]>
[root@localhost][(none)]> exit
Bye
[root@localhost ~]#
[root@localhost ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.26-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
[root@localhost][(none)]>
[root@localhost][(none)]> select @@session.character_set_connection;
+------------------------------------+
| @@session.character_set_connection |
+------------------------------------+
| utf8mb4 |
+------------------------------------+
1 row in set (0.00 sec)
[root@localhost][(none)]>