当前位置:网站首页>Mysql database (28): Variables

Mysql database (28): Variables

2022-06-12 18:46:00 Pengshiyu

Variable variables

MySQL It is essentially a programming language

1、 System variables

Valid for all user clients

1.1、 Look at the system variables

1、 Mode one

show variables [like 'pattern'];

Example

mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+

2、 Mode two :

Use select Query the data value of the variable

select @@ Variable name ;

Example

select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
|            1 |
+--------------+

1.2、 Modify system variables

1、 Partial modification ( Session level )

--  Only for the current client when the connection is valid 
set  Variable name  =  value ;

-- eg:
set autocommit = 'off';
show variables like 'autocommit';

2、 Global modification

--  All clients , Are effective 
set global  Variable name  =  value ;
set @@global. Variable name  =  value ;

-- eg:
set global autocommit = 'off';

Be careful , After global modification , Restart the client to take effect

2、 Session variables

Also known as user variables , Set the variable , Only valid for the client used by the current user

--  Define user variables 
set @ Variable name  =  value ;

set @age = 23;

mysql There is no comparison symbol in == , It uses =;
To avoid confusion between assignment and comparison , Assignment use :=

set @ Variable name  :=  value ;

set @name := 'Tom';

mysql Allow data to be fetched from tables and stored in variables , Only one row of data

-- 1、 Assign and view the assignment process 
select @ Variable 1 :=  Field 1, @ Variable 2 :=  Field 2 from  surface  where  Conditions 

select @name := name, @age := age from my_student limit 1;
+---------------+-------------+
| @name := name | @age := age |
+---------------+-------------+
|  Liu bei           |          18 |
+---------------+-------------+

--- 2、 Only assign values without looking at the process 
select  Field 1,  Field 2 from  surface  where  Conditions  into @ Variable 1, @ Variable 2;
select name, age from my_student limit 1 into @name, @age;

Check the variable

select @ Variable name 

mysql> select @name, @age;
+--------+------+
| @name  | @age |
+--------+------+
|  Liu bei    |   18 |
+--------+------+

3、 local variable

Scope of action begin To end Between statement blocks , Variables set in this statement block

  • declare Statement is used to define local variables
  • local variable declare The statement appears in begin To end Between statement blocks

Declarative grammar

declare  Variable name   data type  [ attribute ];

4、 Variable scope

The range of areas where variables can be used

4.1、 Local scope

declare Keyword declaration ( Used in structures : function / stored procedure / trigger )

declare The variables declared by the keyword do not have any symbolic decoration , It's an ordinary string , If the variable is accessed externally , The system will automatically consider it as a field

4.2、 Conversation scope

User defined , Use @ Symbolic defined variables , Use set keyword

Conversation scope , When the secondary connection is valid, only in this connection , Can be used anywhere ( It can be found in the structure content , You can also cross Library )

Session variables can be used inside functions

set @name = ' Zhang San ';

create function get_name() returns char(4)
return @name;

select get_name();
+------------+
| get_name() |
+------------+
|  Zhang San        |
+------------+

Session variables can span libraries

use mydatabase2;

mysql> select @name;
+--------+
| @name  |
+--------+
|  Zhang San    |
+--------+ 

4.3、 Global scope

All the clients , All connections are valid , You need to use global symbols to define

set global  Variable name  =  value ;
set @@global. Variable name  =  value ;

Usually , stay sql When programming , Do not use custom variables to control the global , Generally, session variables are defined or local variables are used in structures to solve problems

原网站

版权声明
本文为[Pengshiyu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121833305495.html