
- MySQL - Home
- MySQL - Introduction
- MySQL - Features
- MySQL - Versions
- MySQL - Variables
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Node.js Syntax
- MySQL - Java Syntax
- MySQL - Python Syntax
- MySQL - Connection
- MySQL - Workbench
- MySQL Databases
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Show Database
- MySQL - Copy Database
- MySQL - Database Export
- MySQL - Database Import
- MySQL - Database Info
- MySQL Users
- MySQL - Create Users
- MySQL - Drop Users
- MySQL - Show Users
- MySQL - Change Password
- MySQL - Grant Privileges
- MySQL - Show Privileges
- MySQL - Revoke Privileges
- MySQL - Lock User Account
- MySQL - Unlock User Account
- MySQL Tables
- MySQL - Create Tables
- MySQL - Show Tables
- MySQL - Alter Tables
- MySQL - Rename Tables
- MySQL - Clone Tables
- MySQL - Truncate Tables
- MySQL - Temporary Tables
- MySQL - Repair Tables
- MySQL - Describe Tables
- MySQL - Add/Delete Columns
- MySQL - Show Columns
- MySQL - Rename Columns
- MySQL - Table Locking
- MySQL - Drop Tables
- MySQL - Derived Tables
- MySQL Queries
- MySQL - Queries
- MySQL - Constraints
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Replace Query
- MySQL - Insert Ignore
- MySQL - Insert on Duplicate Key Update
- MySQL - Insert Into Select
- MySQL Indexes
- MySQL - Indexes
- MySQL - Create Index
- MySQL - Drop Index
- MySQL - Show Indexes
- MySQL - Unique Index
- MySQL - Clustered Index
- MySQL - Non-Clustered Index
- MySQL Operators and Clauses
- MySQL - Where Clause
- MySQL - Limit Clause
- MySQL - Distinct Clause
- MySQL - Order By Clause
- MySQL - Group By Clause
- MySQL - Having Clause
- MySQL - AND Operator
- MySQL - OR Operator
- MySQL - Like Operator
- MySQL - IN Operator
- MySQL - ANY Operator
- MySQL - EXISTS Operator
- MySQL - NOT Operator
- MySQL - NOT EQUAL Operator
- MySQL - IS NULL Operator
- MySQL - IS NOT NULL Operator
- MySQL - Between Operator
- MySQL - UNION Operator
- MySQL - UNION vs UNION ALL
- MySQL - MINUS Operator
- MySQL - INTERSECT Operator
- MySQL - INTERVAL Operator
- MySQL Joins
- MySQL - Using Joins
- MySQL - Inner Join
- MySQL - Left Join
- MySQL - Right Join
- MySQL - Cross Join
- MySQL - Full Join
- MySQL - Self Join
- MySQL - Delete Join
- MySQL - Update Join
- MySQL - Union vs Join
- MySQL Keys
- MySQL - Unique Key
- MySQL - Primary Key
- MySQL - Foreign Key
- MySQL - Composite Key
- MySQL - Alternate Key
- MySQL Triggers
- MySQL - Triggers
- MySQL - Create Trigger
- MySQL - Show Trigger
- MySQL - Drop Trigger
- MySQL - Before Insert Trigger
- MySQL - After Insert Trigger
- MySQL - Before Update Trigger
- MySQL - After Update Trigger
- MySQL - Before Delete Trigger
- MySQL - After Delete Trigger
- MySQL Data Types
- MySQL - Data Types
- MySQL - VARCHAR
- MySQL - BOOLEAN
- MySQL - ENUM
- MySQL - DECIMAL
- MySQL - INT
- MySQL - FLOAT
- MySQL - BIT
- MySQL - TINYINT
- MySQL - BLOB
- MySQL - SET
- MySQL Regular Expressions
- MySQL - Regular Expressions
- MySQL - RLIKE Operator
- MySQL - NOT LIKE Operator
- MySQL - NOT REGEXP Operator
- MySQL - regexp_instr() Function
- MySQL - regexp_like() Function
- MySQL - regexp_replace() Function
- MySQL - regexp_substr() Function
- MySQL Fulltext Search
- MySQL - Fulltext Search
- MySQL - Natural Language Fulltext Search
- MySQL - Boolean Fulltext Search
- MySQL - Query Expansion Fulltext Search
- MySQL - ngram Fulltext Parser
- MySQL Functions & Operators
- MySQL - Date and Time Functions
- MySQL - Arithmetic Operators
- MySQL - Numeric Functions
- MySQL - String Functions
- MySQL - Aggregate Functions
- MySQL Misc Concepts
- MySQL - NULL Values
- MySQL - Transactions
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - SubQuery
- MySQL - Comments
- MySQL - Check Constraints
- MySQL - Storage Engines
- MySQL - Export Table into CSV File
- MySQL - Import CSV File into Database
- MySQL - UUID
- MySQL - Common Table Expressions
- MySQL - On Delete Cascade
- MySQL - Upsert
- MySQL - Horizontal Partitioning
- MySQL - Vertical Partitioning
- MySQL - Cursor
- MySQL - Stored Functions
- MySQL - Signal
- MySQL - Resignal
- MySQL - Character Set
- MySQL - Collation
- MySQL - Wildcards
- MySQL - Alias
- MySQL - ROLLUP
- MySQL - Today Date
- MySQL - Literals
- MySQL - Stored Procedure
- MySQL - Explain
- MySQL - JSON
- MySQL - Standard Deviation
- MySQL - Find Duplicate Records
- MySQL - Delete Duplicate Records
- MySQL - Select Random Records
- MySQL - Show Processlist
- MySQL - Change Column Type
- MySQL - Reset Auto-Increment
- MySQL - Coalesce() Function
MySQL - Variables
In general, variables are the containers that store some information in a program. The value of a variable can be changed as many times as required. Each variable has a datatype specifying the type of data we can store in it such as integer, string, float etc.
In some programming languages such as Java, C, C++ etc., we need to declare the datatype of a variable before assigning values to it.
In languages like python the datatype of a variable is presumed based on the values assigned to it. There is no need of declaring the datatype separately.
In MySQL there is no need to declare the datatype we can simply define a variable with a value using the SET statement.
Variables in MySQL
The main purpose of a variable is to label a memory location(s) and store data in it so that it can be used throughout the program.
The characters we use to declare and define a variables are called literals and a literal can be anything other than special characters, numbers and, reserved keywords.
In MySQL, there are three types of variables. The same is described below −
User-Defined Variable
Local Variable
System Variable
User-Defined Variables
The User-Defined variable allows us to store a value in one statement and subsequently refer to it in another. To do so, MySQL provides SET and SELECT commands to declare a variable. These variable names will have the symbol "@" as a prefix. We can use either = or := symbols depending on the situation. The user-defined data type can be any of the following: integer, decimal, Boolean, etc.
Syntax
Following is the syntax to declare a user-defined variable in MySQL using the SET statement −
SELECT @variable_name = value
Example
In the following query, we are assigning a value to a variable using the SET statement as follows −
SET @Name = 'Michael';
Using the SELECT statement, we can display the value of @name variable −
SELECT @Name;
Output
The output for the query above is produced as given below −
@Name |
---|
Michael |
Example
Here, we are assigning a value to a variable using the SELECT statement −
SELECT @test := 10;
Output
On executing the given query, the output is displayed as follows −
@test := 10 |
---|
10 |
Example
Let us create table with the name CUSTOMERS using the following query −
CREATE TABLE CUSTOMERS( ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2) );
Now, let us insert values into the above-created table using the INSERT INTO statement −
INSERT INTO CUSTOMERS (NAME, AGE, ADDRESS, SALARY) VALUES ('Ramesh', 32, 'Ahmedabad', 2000.00), ('Khilan', 25, 'Delhi', 1500.00), ('Kaushik', 23, 'Kota', 2000.00), ('Chaitali', 25, 'Mumbai', 6500.00), ('Hardik', 27, 'Bhopal', 8500.00), ('Komal', 22, 'Hyderabad', 4500.00), ('Muffy', 24, 'Indore', 10000.00);
The CUSTOMERS table is created as follows −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
1 | Ramesh | 32 | Ahmedabad | 2000.00 |
2 | Khilan | 25 | Delhi | 1500.00 |
3 | Kaushik | 23 | Kota | 2000.00 |
4 | Chaitali | 25 | Mumbai | 6500.00 |
5 | Hardik | 27 | Bhopal | 8500.00 |
6 | Komal | 22 | Hyderabad | 4500.00 |
7 | Muffy | 24 | Indore | 10000.00 |
Now, let us declare a variable with the name @max_salary using the SELECT statement to display the maximum salary value from the CUSTOMERS table −
SELECT @max_salary := MAX(salary) FROM CUSTOMERS;
Then, we will select records from the table where the salary is equal to @max_salary variable −
SELECT * FROM CUSTOMERS WHERE SALARY = @max_salary;
Output
The output for the query above is produced as given below −
ID | NAME | AGE | ADDRESS | SALARY |
---|---|---|---|---|
7 | Muffy | 24 | Indore | 10000.00 |
Local Variables
The MySQL local variable can be declared using the DECLARE keyword. When we declare the local variable, the @ symbol is not used as prefix. This variable is a strongly typed variable, which means that we definitely need to declare a data type.
The MySQL DEFAULT keyword can be used while declaring a variable to set the default value of the variable. This is an optional parameter, if we do not define this, the initial value will be NULL.
Syntax
Following is the syntax to declare a local variable in MySQL −
DECLARE variable_name1, variabale_name2, ... data_type [DEFAULT default_value];
Example
In the following example, we are using the DECLARE statement in a stored procedure.
DELIMITER // CREATE PROCEDURE salaries() BEGIN DECLARE Ramesh INT; DECLARE Khilan INT DEFAULT 30000; DECLARE Kaushik INT; DECLARE Chaitali INT; DECLARE Total INT; SET Ramesh = 20000; SET Kaushik = 25000; SET Chaitali = 29000; SET Total = Ramesh+Khilan+Kaushik+Chaitali; SELECT Total,Ramesh,Khilan,Kaushik,Chaitali; END //
Now, let us call the stored procedure using the following query −
CALL salaries() //;
Output
Following is the output −
Total | Ramesh | Khilan | Kaushik | Chaitali |
---|---|---|---|---|
104000 | 20000 | 30000 | 25000 | 29000 |
System Variables
The system variables are predefined by the MySQL. These contains the data we need, to work with the database. Each MySQL system variable has a default value.
The SET command in MySQL can be used at the runtime to dynamically change the values of the system variables.
There are two variable scope modifiers available for the SHOW VARIABLES command. They are GLOBAL and SESSION.
The GLOBAL variables are active throughout the lifecycle.
The SESSION variables can be available only in the current session.
Following is the command to display all the system variables in MySQL −
SHOW [GLOBAL | SESSION] VARIABLES;
Example
In the following example, let us display the existing global system variables using the SHOW VARIABLES query −
SHOW VARIABLES LIKE '%table%';
The variables are displayed in the table format as follows −
Variable_name | Value |
---|---|
big_tables | OFF |
default_table_encryption | OFF |
innodb_file_per_table | ON |
innodb_ft_aux_table | |
innodb_ft_server_stopword_table | |
innodb_ft_user_stopword_table | |
innodb_table_locks | ON |
innodb_temp_tablespaces_dir | .\#innodb_temp\ |
innodb_undo_tablespaces | 2 |
innodb_validate_tablespace_paths | ON |
lower_case_table_names | 1 |
max_heap_table_size | 16777216 |
old_alter_table | OFF |
performance_schema_max_table_handles | -1 |
performance_schema_max_table_instances | -1 |
performance_schema_max_table_lock_stat | -1 |
show_create_table_skip_secondary_engine | OFF |
show_create_table_verbosity | OFF |
table_definition_cache | 2000 |
table_encryption_privilege_check | OFF |
table_open_cache | 4000 |
table_open_cache_instances | 16 |
tablespace_definition_cache | 256 |
temptable_max_mmap | 1073741824 |
temptable_max_ram | 1073741824 |
temptable_use_mmap | ON |
tmp_table_size | 99614720 |
updatable_views_with_limit | YES |
Now, using the query below, we will fetch the current value of the MySQL "key_buffer_size" variable −
SELECT @@key_buffer_size;
Output
Following is the output of the above query −
@@key_buffer_size |
---|
8388608 |