MySQL variable principles and application examples

MySQL variable principles and application examples

In the MySQL documentation, MySQL variables can be divided into two categories, namely system variables and user variables.

However, according to actual applications, they are further divided into four types, namely local variables, user variables, session variables and global variables.

1. Local variables

MySQL local variables can only be used in begin/end statement blocks, such as begin/end statement blocks in stored procedures.

Its scope is limited to this statement block.

-- The declare statement is used to define local variables. You can use default to specify the default value.
declare age int default 0;

-- Local variable assignment method 1
set age=18;

-- Local variable assignment method 2
select StuAge
into age
from demo.student
where StuNo='A001';

2. User variables

MySQL user variables. User variables in MySQL do not need to be declared in advance. When using them, just use "@variable name" directly.

Its scope is the current connection.

-- The first usage, when using set, you can use the "=" or ":=" two assignment symbols to assign values
set @age=19;

set @age:=20;

-- The second usage, when using select, you must use the ":=" assignment symbol to assign a value
select @age:=22;

select @age:=StuAge
from demo.student
where StuNo='A001';

Session variables

MySQL session variables, the server maintains a series of session variables for each connected client.

Its scope is limited to the current connection, that is, the session variables in each connection are independent.

-- Display all session variables
show session variables;

-- Three ways to set the value of a session variable
set session auto_increment_increment=1;
set @@session.auto_increment_increment=2;
set auto_increment_increment=3; -- When the session keyword is omitted, the default is session, which sets the value of the session variable

-- Three ways to query the value of session variables
select @@auto_increment_increment;
select @@session.auto_increment_increment;
show session variables like '%auto_increment_increment%'; -- The session keyword can be omitted

-- The keyword session can also be replaced by the keyword local
set @@local.auto_increment_increment=1;
select @@local.auto_increment_increment;

4. Global variables

MySQL global variables, global variables affect the overall operation of the server, when the service starts, it initializes all global variables to default values. To modify global variables, you must have super privileges.

Its scope is the entire life cycle of the server.

-- Display all global variables
show global variables;

-- Two ways to set the value of a global variable
set global sql_warnings=ON; -- global cannot be omitted
set @@global.sql_warnings=OFF;

-- Two ways to query the value of global variables
select @@global.sql_warnings;
show global variables like '%sql_warnings%';

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of writing lazy UNION in MySQL using custom variables
  • Detailed explanation of mysql user variables and set statement examples
  • Detailed explanation of MySQL startup options and system variables examples
  • Example analysis of mysql variable usage [system variables, user variables]
  • How to create (CREATE PROCEDURE) and call (CALL) a MySQL stored procedure and how to create (DECLARE) and assign (SET) a variable
  • 15 important variables you must know about MySQL performance tuning (summary)
  • MySQL 8.0.12 installation and environment variable configuration tutorial under win10
  • Detailed explanation of two methods for setting global variables and session variables in MySQL
  • MySQL 5.6.23 Installation and Configuration Environment Variables Tutorial
  • The concept and characteristics of MySQL custom variables

<<:  Vue3.0 implements encapsulation of checkbox components

>>:  Solve the problem of IDEA configuring tomcat startup error

Recommend

Solution to the error reported by Mysql systemctl start mysqld

Error message: Job for mysqld.service failed beca...

How to build LNMP environment on Ubuntu 20.04

Simple description Since it was built with Centos...

Solve the error "Can't locate ExtUtils/MakeMaker.pm in @INC"

When installing mha4mysql, the steps are roughly:...

Causes and solutions to the garbled character set problem in MySQL database

Preface Sometimes when we view database data, we ...

Summary of the most commonly used knowledge points about ES6 new features

Table of contents 1. Keywords 2. Deconstruction 3...

Detailed explanation of HTML document types

Mine is: <!DOCTYPE html> Blog Garden: <!...

Vue2.x - Example of using anti-shake and throttling

Table of contents utils: Use in vue: explain: Ima...

Summary of 3 ways to lazy load vue-router

Not using lazy loading import Vue from 'vue&#...

Detailed tutorial on installation and configuration of MySql 5.7.17 winx64

1. Download the software 1. Go to the MySQL offic...

Seven solutions for classic distributed transactions between MySQL and Golan

Table of contents 1. Basic theory 1.1 Transaction...

Detailed explanation of the process of installing msf on Linux system

Or write down the installation process yourself! ...

Use native js to simulate the scrolling effect of live bullet screen

Table of contents 1. Basic principles 2. Specific...