当前位置:网站首页>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
边栏推荐
- [Luogu p3175] bitwise OR (min max inclusive) (high-dimensional prefix and / FWT)
- Useful resources for ns2
- IDEA连接数据库时区问题,报红Server returns invalid timezone. Need to set ‘serverTimezone‘ property.
- CMD command
- Self control principle learning notes - system stability analysis (2) - loop analysis and Nyquist bode criterion
- C # interaction with MySQL database - MySQL configuration and addition, deletion, query and modification operations
- 阿里云对象存储OSS的开通和使用
- sql 时间处理(SQL SERVER\ORACLE)
- 图的遍历的定义以及深度优先搜索和广度优先搜索(二)
- C语言打印菱形
猜你喜欢

MongoDB学习笔记(1)——安装MongoDB及其相关配置

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

Basic use of Nacos (1) - getting started

Unity显示Kinect捕获的镜头

I'm afraid I won't use the JMeter interface testing tool if I accept this practical case

Performance analysis of continuous time systems (2) - second order system performance improvement methods PID, PR

Nacos的基本使用(1)——入门

Unity-显示Kinect深度数据

Introduction to assembly language (1)

What if idea successfully connects to the database without displaying the table
随机推荐
【云图说】 第250期 初识华为云微服务引擎CSE
SQL server top 关键字使用
X-shell remote connection virtual machine
kettle学习——8.2版本的资源库配置变为灰色,且没有了Connect按钮
Using functions to extract numbers from text strings in Excel
An experience
mysql学习笔记(1)——变量
阿里云视频点播服务的开通和使用
一个经验
Basic concepts of Nacos and single machine deployment
Self control principle learning notes - system stability analysis (1) - BIBO stability and Routh criterion
v-if,v-else,v-for
Unity-显示Kinect深度数据
An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?
Performance analysis of continuous time system (1) - performance index and first and second order analysis of control system
kettle 合并记录 数据减少
图的遍历的定义以及深度优先搜索和广度优先搜索(二)
kettle 分列、合并记录
kettle switch / case 控件实现分类处理
normal distribution, lognormal distribution,正态随机数的生成