当前位置:网站首页>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
边栏推荐
- Review of MySQL (10): three paradigms of database
- MySQL advanced learning notes
- leetcode:6097. 替换字符后匹配【set记录 + 相同长度逐一查询】
- Redis(三十二)-用Redis做分布式锁
- What is a network proxy
- 232-ch579m learning and development Ethernet routine TCP server (project application package, LAN or WAN test)
- Shenzhen has been shut down for 7 days since March 14. Home office experience | community essay solicitation
- Leetcode 494. Objectives and
- Problems that the sap Spartacus e-commerce cloud UI shipping method does not display in the unit test environment
- leetcode:6096. 咒语和药水的成功对数【排序 + 二分】
猜你喜欢

OpenGL shadow implementation (hard shadow)

美团获得小样本学习榜单FewCLUE第一!Prompt Learning+自训练实战

Review of MySQL (I): go deep into MySQL

Double non grind one, three side byte, cool. Next time

What is a network proxy

【矩阵论 & 图论】期末考试复习思维导图

Title 68: there are n integers, so that the previous numbers are moved backward m positions, and the last m numbers become the first m numbers

kali局域网ARP欺骗(arpspoof)并监听(mitmproxy)局域内其它主机上网记录

How to download Vega in China

Review of MySQL (3): query operation
随机推荐
国内如何下载Vega
leetcode:6096. Success logarithm of spells and potions [sort + dichotomy]
leetcode:6097. Match [set record + query one by one with the same length] after replacing characters
leetcode:5259. Calculate the total tax payable [simple simulation + see which range]
Review of MySQL (V): Joint table query and sub query
【矩阵论 & 图论】期末考试复习思维导图
What is SAP support package stack
Review of MySQL (IX): index
How to break the black screen after cleaning the dust and applying silicone grease on the laptop?
从应无所住说起
Title 68: there are n integers, so that the previous numbers are moved backward m positions, and the last m numbers become the first m numbers
基于Halcon的矩形卡片【手动绘制ROI】的自由测量
chrome浏览器解决跨域问题
leetcode:5270. Minimum path cost in Grid [simple level DP]
Research Report on the overall scale, major manufacturers, major regions, products and applications of Electric Screwdrivers in the global market in 2022
Partial scratch and corrosion detection of bolts and screws based on Halcon
wireshark基本使用命令
Leetcode 1049. 最后一块石头的重量 II
Free measurement of rectangular card [manual drawing ROI] Based on Halcon
Enhanced version of unit test code displayed by SAP e-commerce cloud Spartacus UI checkout spinner