MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
In MySQL, you can define and use variables in your SQL queries, which can be very useful for storing intermediate results for use later in your queries, scripts, or stored procedures.
There are three types of variables in MySQL:
Local Variables: These are declared in the body of a stored procedure or function using the DECLARE
statement and are only accessible within that procedure or function.
Here's an example of declaring and using a local variable:
DECLARE @v_employee_id INT; SET @v_employee_id = 1; SELECT * FROM employees WHERE id = @v_employee_id;
In this example, a local variable named v_employee_id
is declared and set to 1. Then it's used in a SELECT
statement to fetch an employee with an ID of 1.
User-Defined Variables: You can define your own variables using the SET
or SELECT
statement. These variables are accessible during your entire session with the MySQL server.
Here's an example of declaring and using a user-defined variable:
SET @employee_id = 1; SELECT * FROM employees WHERE id = @employee_id;
In this example, a user-defined variable named employee_id
is set to 1, and then it's used in a SELECT
statement to fetch an employee with an ID of 1.
System Variables: MySQL has many system variables that control its operation. You can change most of these variables using the SET
statement, though some are read-only.
Here's an example of setting a system variable:
SET @@global.max_connections = 500;
In this example, the global system variable max_connections
is set to 500, which means the MySQL server can handle 500 concurrent connections.
Remember that the scope of a variable is the context in which it's defined. Local variables are only accessible within the stored procedure or function where they're defined, user-defined variables are accessible throughout your session, and system variables are accessible to all sessions (though some require special privileges to set).
Defining and assigning variables in MySQL:
DECLARE
statement for local variables and the SET
statement for assignment.-- Local variable declaration DECLARE my_variable INT; -- Variable assignment SET my_variable = 42;
How to declare variables in MySQL:
DECLARE
statement, specifying the variable name and data type.DECLARE my_variable INT;
MySQL SET statement for variable assignment:
SET
statement in MySQL to assign values to variables.SET my_variable = 42;
Assigning values to variables in MySQL:
SET
statement or directly in procedural code.-- Using SET statement SET my_variable = 42; -- Direct assignment in procedural code SELECT column1 INTO my_variable FROM my_table WHERE condition;
Local and session variables in MySQL:
-- Local variable declaration DECLARE my_local_variable INT; -- Session variable assignment SET @@session.my_session_variable = 42;
Using DECLARE in MySQL for variable definition:
DECLARE
statement in MySQL to define local variables within a stored procedure or function.DELIMITER // CREATE PROCEDURE my_procedure() BEGIN -- Declare local variable DECLARE my_local_variable INT; -- Rest of the procedure code END // DELIMITER ;
Examples of variable usage in MySQL:
-- Example: Calculations using variables SET @x = 5; SET @y = 7; SET @result = @x + @y; -- Example: Data retrieval using variables SELECT column1 INTO @data_variable FROM my_table WHERE condition;
Scope of variables in MySQL:
DELIMITER // CREATE PROCEDURE my_procedure() BEGIN -- Local variable DECLARE my_local_variable INT; -- Session variable SET @@session.my_session_variable = 42; END // DELIMITER ;