MySQL variable vs. variable. What's the difference?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the realm of MySQL, one might encounter two types of variables frequently used in scripting, querying, and database management: user-defined variables prefixed with an @ symbol (e.g., @variable) and session/system variables without it (e.g., variable). Understanding the differences between these variables, their scope, initialization, and application is critical for efficient and effective database operations. Below is a comprehensive guide to elucidate these differences.
User-Defined Variables (@variable)
Definition and Scope
User-defined variables are variables prefixed with an @ and are specific to a user's session. They can be initialized with a value and can hold different types of data, such as strings, integers, or floats. Once declared, these variables maintain their value throughout the entire session and are released once the session ends.
Initialization and Assignment
User-defined variables can be initialized using any valid SQL expression and do not require prior declaration. They can be assigned in a SELECT statement or using the SET keyword:
Usage in Queries
User-defined variables can be utilized in a variety of ways within SQL queries. They are particularly beneficial for storing intermediate results or iterating over rows in data manipulation processes:
Features and Caveats
- Scope: Lifespan limited to the session; lost upon disconnect.
- Flexibility: Can change type during its usage without the need for redefining.
- Null Handling: If used in a statement that does not produce a result, defaults to
NULL.
System Variables (variable)
Definition and Role
System variables control the operational aspects of MySQL server behavior. Two types of system variables exist:
- Global Variables: Affect the server's behavior and persist across sessions.
- Session Variables: Apply to individual user sessions, resetting upon session termination.
Initialization and Configuration
System variables can be configured dynamically using the SET command. They require either @@session or @@global to specify the scope:
Changes to session variables affect only the current session, while global changes require the SUPER privilege.
Features and Caveats
- Scope: Either session or global, with global changes affecting all future connections.
- Persistence: Global variables can be set in configuration files for persistence across server restarts.
- Data Integrity: Incorrectly configuring system variables can lead to misuse of server resources or security loopholes, needing cautious handling.
Key Differences and Considerations
The table below summarizes the essential differences between user-defined (@variable) and system variables (variable):
| Attribute | User-Defined (@variable) | System Variable (variable) |
| Scope | Session | Session or Global |
| Persistence | None (lost on session termination) | Global variables persistent across server restarts |
| Initialization | Using any SQL expression | Requires SET with @@session
or @@global |
| Main Use Cases | Storing intermediate results,
iteration (WHILE) | Configuration, tuning server behavior |
| Required Privileges | None | May require SUPER for global changes |
Practical Examples
Incremental Operations with User-Defined Variables
In this example, a user-defined variable @i is used to keep a running count during the selection of rows from a table, demonstrating dynamic row numbering within a query.
Configuration Adjustment with System Variables
Adjusting the global system variable wait_timeout configures the maximum time in seconds that the server waits for activity on a non-interactive connection before closing it, illustrating how operational parameters can be tuned.
Additional Considerations
- Concurrency: User-defined variables are not thread-safe, so their use in multi-threaded environments needs caution.
- Error Checking: Lack of compile-time checks for user-defined variables makes rigorous error-checking and debugging crucial.
Understand the functional distinctions between user-defined and system variables in MySQL can significantly aid developers and database administrators in writing robust, efficient, and maintainable database scripts and applications. Proper use of these variables enhances data manipulation capabilities and optimizes server performance and configuration.

