Detailed explanation and application of MySQL variables
Variables in MySQL are containers that temporarily store values for the duration of query execution. There are three main types: user-defined variables, local variables and server system variables.
User-defined variables (@ prefix)
Initialize user-defined variables:
SET @start = 1, @finish = 10;
Example: Using user-defined variables in a SELECT query
SELECT * FROM places WHERE place BETWEEN @start AND @finish;
Local variables (no prefix)
Declare and initialize local variables in stored procedures:
DECLARE start INT unsigned DEFAULT 1; DECLARE finish INT unsigned DEFAULT 10;
Example: Using local variables in stored procedures
CREATE PROCEDURE sp_test(var1 INT) BEGIN DECLARE start INT unsigned DEFAULT 1; DECLARE finish INT unsigned DEFAULT 10; SELECT var1, start, finish; SELECT * FROM places WHERE place BETWEEN start AND finish; END;
Server system variables (@@ prefix)
View the current values of server system variables:
SELECT @@sort_buffer_size;
Modify the value of the server system variable:
SET GLOBAL sort_buffer_size=1000000;
The above is the detailed content of How to Declare and Use User-Defined, Local, and Server System Variables in MySQL?. For more information, please follow other related articles on the PHP Chinese website!