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:
<code class="language-sql">SET @start = 1, @finish = 10;</code>
Example: Using user-defined variables in a SELECT query
<code class="language-sql">SELECT * FROM places WHERE place BETWEEN @start AND @finish;</code>
Local variables (no prefix)
Declare and initialize local variables in stored procedures:
<code class="language-sql">DECLARE start INT unsigned DEFAULT 1; DECLARE finish INT unsigned DEFAULT 10;</code>
Example: Using local variables in stored procedures
<code class="language-sql">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; </code>
Server system variables (@@ prefix)
View the current values of server system variables:
<code class="language-sql">SELECT @@sort_buffer_size;</code>
Modify the value of the server system variable:
<code class="language-sql">SET GLOBAL sort_buffer_size=1000000;</code>
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!