当前位置:网站首页>MySQL learning notes (1) -- variables
MySQL learning notes (1) -- variables
2022-07-27 19:16:00 【Xiao Li, who loves traveling】
mysql Learning notes (1)—— Variable
List of articles
One 、 Classification of variables :
stay mysql The variables in are divided into System variables and Custom variable .
The system variables are divided into Global variables and Session variables
Custom variables are divided into User variables and local variable 
Two 、 System variables
Global variables
(1) Concept and classification :
System variables : Provided by the system , Data server level .
The system variables are divided into Global variables and Session variables
(2) Commonly used sql sentence :
1. Look at all the system variables
show global [session] variables;
2. Look at some of the system variables that meet the criteria ( Fuzzy query )
show global [session] variables like ‘%char%’
3. View the value of a specified system variable
select @@global [session]. System variable name
4. Assign a value to a system variable
Mode one :
set global [session] System variable name = value ;
Mode two :
set @@global [session]. System variable name = value ;
(3) demonstration :
-- View global variables
SHOW GLOBAL VARIABLES;

-- Look at some of the global variables
SHOW GLOBAL VARIABLES LIKE '%char%';

-- View the value of the specified global variable
SELECT @@global.autocommit;
SELECT @@tx_isolation; -- View the isolation level of the transaction
-- Assign a value to a specified global variable
SET @@global.autocommit = 0; -- Turn off automatic transaction commit
SELECT @@global.autocommit; -- 0

(4) Scope
Every time the server starts, it will assign initial values to all global variables , For all conversations ( Connect ) It works , But not across servers .
For example, I changed the automatic submission of Alibaba cloud server sessions , Set up in order to 0, But in the local session, the value of automatic submission is 1
Session variables
demonstration
-- Session variables
SHOW SESSION VARIABLES;
SHOW VARIABLES;

You can see that there are more than ten global variables in the system
-- Look at some session variables
SHOW SESSION VARIABLES LIKE '%char%';

-- View the specified session variable
SELECT @@session.tx_isolation;
SELECT @@tx_isolation;

-- Assign a value to a session variable
-- Mode one :
SET @@session.tx_isolation='read-uncommitted';
-- Mode two :
SET SESSION tx_isolation='read-committed';
-- Change back ( Repeatable )
SET SESSION tx_isolation='repeatable-read';
3、 ... and 、 Custom variable
Concept :
Custom variable :
explain : Variables are user-defined
Use : Declare variables assignment Use ( see 、 Compare 、 Operations, etc )
User variables
(1) Scope
User variables :
Scope : For the current session ( Connect ) It works , Same as the scope of the conversation variable
(2) Use steps
Declare and initialize :
Assignment operator : = or :=( Recommended )
set @ User variable name = value ; or
set @ User variable name := value , or
select @ User variable name := value
assignment :
Mode one : adopt set or select
set @ User variable name = value ; or
set @ User variable name := value , or
select @ User variable name := value
Mode two : adopt select into
select Field into @ User variable name from surface ;
3. Look at the value of the user variable
select @ User variable name ;
(3) demonstration
-- Case study
-- Mode one
SET @name='jack';
SELECT @name;

-- Mode two
SELECT @count:=0;
SELECT COUNT(*) INTO @count FROM stu;
SELECT @count;

local variable
(1) Scope
Just defining it begin end Effective in ( First sentence )
(2) Use
– Statement :
declare Variable name type ;
declare Variable name type default value ;
– assignment ( Similar to user variables )
Mode one : adopt set or select
set Local variable name = value ; or
set Local variable name := value , or
select @ Local variable name := value
Mode two : adopt select into
select Field into Local variable name from surface ;
see
select Local variable name ;
(2) demonstration
-- User variables
SET @a=1;
SET @b=2;
SET @sum= @a + @b;
SELECT @sum;

Compare user variables with local variables
| Column 1 | Scope | Where to define and use | grammar |
|---|---|---|---|
| User variables | Current session | Anywhere in the conversation | Have to add ’@' Symbol . There is no need to qualify the type |
| local variable | begin end in | Only in begin end in , And for the first sentence | Generally, there is no need to add ’@' Symbol , You need to qualify the type |
Test with local variables :
-- 2. local variable ( The following operations are not supported , Because the scope of the local variable is begin end in )
DECLARE a INT DEFAULT 1;
DECLARE b INT DEFAULT 2;
DECLARE SUM INT;
SET SUM = a + b;
SELECT SUM;
Wrong report , It shows that the scope of local variables is limited
边栏推荐
- Subscription and use of Alibaba cloud video on demand service
- C#与Mysql数据库交互-Mysql配置及增删查改操作
- 专项测试之「 性能测试」总结
- PHP string operation
- Unity learning notes - six common functions of object movement
- Unity learning notes (rigid body physics collider trigger)
- JS common utils encapsulation
- Latex use - subfigure vertical graphics
- Selenium automated test interview questions family bucket
- 转行软测&跳槽到新公司,工作怎样快速上手?
猜你喜欢

Selenium automated test interview questions family bucket

200行代码快速入门文档型数据库MonogoDB

Kinect for Unity3d----KinectManager

图的遍历的定义以及深度优先搜索和广度优先搜索(二)

连续时间系统的性能分析(1)-控制系统性能指标及一二阶分析

专项测试之「 性能测试」总结

Basic use of Nacos (1) - getting started

NPM basic use

Unity learning notes (rigid body physics collider trigger)

NPM's ID card and dependence
随机推荐
ref 关键字的用法
win10小技巧(1)——转移桌面位置
The great idea of NS2
Using functions to extract numbers from text strings in Excel
怎样产生标准分布或高斯分布的随机数
kettle8.2 安装及常见问题
Using vscode to build u-boot development environment
Sword finger offer17- print from 1 to the maximum n digits - Analog
Electromagnetic field learning notes - vector analysis and field theory foundation
kettle入门级操作第一篇(读取excel、输出excel)
大冤种们,新进测试行业,如何正确选择意向企业?
Express get/post/delete... Request
Subscription and use of Alibaba cloud video on demand service
又有一个Repeater的例子
C # one method returns multiple values. Suggestions collection
电磁场学习笔记-矢量分析和场论基础
kettle 分列、合并记录
SSM project uses filter to realize login monitoring
Nacos集群部署-高可用保证
[cloud picture theory] the first time to know Huawei cloud micro service engine CSE in issue 250