1. Overview of MySQL Variables
The Role and Benefits of Variables in MySQL
MySQL variables are useful tools that allow you to store values within queries and reuse those values across multiple queries. This eliminates the need to repeatedly retrieve the same data and helps make SQL statements more concise and efficient.
There are mainly two types of variables in MySQL:
- User-defined variables: Temporary variables used within a specific session.
- System variables: Configuration variables used to control the behavior of the MySQL server.
In this section, we will first take a detailed look at user-defined variables and then explain how to use system variables.
2. Types of MySQL Variables
2.1 User-Defined Variables
User-defined variables are scoped to a single session and cannot be accessed by other clients. This ensures that variables can be safely reused within the same session. Variables are declared using the @ symbol.
Example:
SET @user_id = 123;
SELECT @user_id;As shown above, you can define a variable using SET and reuse its value in subsequent queries. Another method to store query results in variables is SELECT INTO.
SELECT name INTO @user_name FROM users WHERE id = @user_id;2.2 System Variables
System variables are used to adjust MySQL server settings. For example, you can manage the maximum number of connections or configure timeout settings.
Example:
SHOW VARIABLES LIKE 'max_connections';This query displays the maximum number of simultaneous connections allowed by the MySQL server. System variables can be modified using the SET command and can be applied either globally or at the session level.
3. Declaring and Using Variables
3.1 How to Declare Variables
You can declare variables using SET or SELECT INTO. The SET statement is straightforward and allows you to assign a value directly.
Example:
SET @user_name = 'Sato';
SELECT @user_name;On the other hand, using SELECT INTO allows you to store query results directly into a variable.
Example:
SELECT name INTO @user_name FROM users WHERE id = 123;3.2 Using Variables in Queries
By using variables, you can reuse them as parameters within queries. For example, the following query retrieves user information using the variable @user_id.
Example:
SELECT * FROM users WHERE id = @user_id;In this way, variables allow you to retain data across multiple queries within the same session.
4. Common Use Cases
4.1 Query Optimization
You can improve performance by storing frequently used data in a variable once and reusing it in subsequent queries.
Example:
SELECT MAX(id) INTO @max_id FROM users;Here, the maximum user ID is stored in a variable and reused in later queries.
4.2 Date and Time Manipulation Use Cases
Using variables for date and time calculations or managing historical data makes it easier to handle time-series data.
Example:
SELECT NOW() INTO @current_time;
SELECT @current_time - INTERVAL 1 DAY INTO @yesterday;In this example, the current time and a specific time difference are stored in variables and reused in other queries.

5. Using Variables in Stored Procedures
Using variables within stored procedures allows you to handle complex logic in a structured way and improves code reusability. The following example demonstrates a stored procedure that retrieves user information.
Example:
CREATE PROCEDURE get_user_info(IN user_id INT, OUT user_name VARCHAR(255))
BEGIN
SELECT name INTO user_name FROM users WHERE id = user_id;
END;By calling this procedure, you can pass a user ID as an argument and store the result in a variable.
6. Best Practices for Using Variables
6.1 Importance of Initialization
Variables should always be initialized before use. Referencing an uninitialized variable may return NULL. This is especially important when using the same variable across multiple queries.
6.2 Managing Scope Within a Session
User-defined variables are valid only within a session. When the session ends, the variables are reset. If you need to use variables across sessions, consider alternative approaches such as temporary tables.
7. Advanced Techniques
7.1 Data Processing Using Cursors
When processing large amounts of data, you can use cursors to handle query results row by row. This allows you to store query results in variables while performing sequential processing.
Example:
DECLARE cursor_user CURSOR FOR SELECT id, name FROM users;Using cursors enables efficient processing of multiple rows of data.
8. Conclusion
By using MySQL variables, you can efficiently manage queries and improve both code readability and performance. Properly distinguishing between user-defined and system variables enables more sophisticated data operations. In particular, combining variables with advanced techniques such as stored procedures and cursors can significantly enhance data processing capabilities in MySQL.


