Variables in MySQL
Variables are used for storing data or information during the execution of a program. It is a way of labeling data with an appropriate name that helps to understand the program more clearly by the reader. The main purpose of the variable is to store data in memory and can be used throughout the program.
MySQL can use variables in three different ways, which are given below:
- User-Defined Variable
- Local Variable
- System Variable
User-Defined Variable
Sometimes, we want to pass values from one statement to another statement. The user-defined variable enables us to store a value in one statement and later can refer it to another statement. MySQL provides a SET and SELECT statement to declare and initialize a variable. The user-defined variable name starts with @ symbol.
The user-defined variables are not case-sensitive such as @name and @NAME; both are the same. A user-defined variable declares by one person cannot visible to another person. We can assign the user-defined variable into limited data types like integer, float, decimal, string, or NULL. The user-defined variable can be a maximum of 64 characters in length.
Syntax
The following syntax is used to declare a user-defined variable.
- By using the SET statement
SET @var_name = value;
2. By using the SELECT statement
SELECT @var_name := value;
Local Variable
It is a type of variable that is not prefixed by @ symbol. The local variable is a strongly typed variable. The scope of the local variable is in a stored program block in which it is declared. MySQL uses the DECLARE keyword to specify the local variable. The DECLARE statement also combines a DEFAULT clause to provide a default value to a variable. If you do not provide the DEFAULT clause, it will give the initial value NULL. It is mainly used in the stored procedure program.
Syntax
We can use the DECLARE statement with the following syntax:
DECLARE variable_name datatype(size) [DEFAULT default_value];
Let us see the following example to use the local variable.
Example
mysql> DECLARE total_price Oct(8,2) DEFAULT 0.0;
System Variable
System variables are a special class to all program units, which contains predefined variables. MySQL contains various system variables that configure its operation, and each system variable contains a default value. We can change some system variables dynamically by using the SET statement at runtime. It enables us to modify the server operation without stop and restart it. The system variable can also be used in the expressions.
MySQL server gives a bunch of system variables such as GLOBAL, SESSION, or MIX types. We can see the GLOBAL variable throughout the lifecycle of the server, whereas the SESSION variable remains active for a particular session only.
We can see the names and values of the system variable by using the following ways:
- To see the current values used by the running server, execute the following command.
mysql> SHOW VARIABLES;
OR,
Mysql> SELECT @@var_name;
Example
mysql> SHOW VARIABLES LIKE '%wait_timeout%';
No comments:
Post a Comment