当前位置:网站首页>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
边栏推荐
- Power control
- win10小技巧(1)——转移桌面位置
- Using MATLAB to generate graphics for journals and conferences - plot
- [cloud picture theory] the first time to know Huawei cloud micro service engine CSE in issue 250
- Basic use of Nacos (1) - getting started
- SQL server top 关键字使用
- Kinect for Unity3D——BackgroundRemovalDemo学习
- Unity learning notes - six common functions of object movement
- NPM basic use
- Matrix of shell programming -- it's cute and cool
猜你喜欢

kettle入门级操作第一篇(读取excel、输出excel)

Unity学习笔记(刚体-物理-碰撞器-触发器)

kettle switch / case 控件实现分类处理

Extension of regular expression

Kinect2 for Unity3D——AvatarDemo学习

v-if,v-else,v-for

How to break the team with automated testing

连续时间系统的性能分析(2)-二阶系统性能改善方式PID,PR

Subscription and use of Alibaba cloud video on demand service

Win10 tips (1) -- transfer desktop location
随机推荐
Using MATLAB to generate graphics for journals and conferences - plot
sql 时间处理(SQL SERVER\ORACLE)
Kinect2 for Unity3D——AvatarDemo学习
X-shell remote connection virtual machine
Nacos的基本使用(1)——入门
Extension of ES6 value
JS common utils encapsulation
There is another example of repeater
2022 Ningde Vocational College Teachers' practical teaching ability improvement training - network construction and management
SSM project uses filter to realize login monitoring
IDEA连接数据库时区问题,报红Server returns invalid timezone. Need to set ‘serverTimezone‘ property.
Browser rendering principle analysis suggestions collection
An experience
编程式跳转
Unity显示Kinect捕获的镜头
js常用utils封装
Mongodb learning notes (1) - install mongodb and its related configurations
新系统安装MySQL+SQLyog
[Luogu p3175] bitwise OR (min max inclusive) (high-dimensional prefix and / FWT)
v-if,v-else,v-for