Declaring Variable in MariaDB
Last Updated :
13 Feb, 2024
Variables serve as essential components in database management systems like MariaDB facilitating the storage and manipulation of data within the database environment. Variable declaration in MariaDB is a fundamental concept, allowing users to store values temporarily, perform calculations, and streamline complex queries. In this article, we will delve into the various methods of variable declaration in MariaDB, providing detailed examples to illustrate their usage.
Variable in MariaDB
A Variable is the name of a storage location that holds a value of it. Generally, Variables are used to store data temporarily within the scope of a session or procedure execution. A variable can hold various types of data such as integers, strings, dates and more complex data types like arrays or JSON objects.
Variables in MariaDB are typically prefixed with the @symbol to define them from other identifiers like column or table names. For example, @my_variable is a valid variable name in MariaDB.
Variables serve several purposes in MariaDB, including:
- Temporary Storage: Variables can hold values temporarily during the execution of queries or stored procedures.
- Dynamic Query Construction: Variables are used to construct dynamic SQL queries where sub-parts of the query such as table names or column names are defined at runtime.
- Data Manipulation: The variables allow the users to perform calculations, comparisons as well as other data manipulation tasks within procedural code.
- Configuration Settings: Some variables in MariaDB are system variables that can control various aspects of the database server's behavior such as maximum connections or buffer sizes.
- Overall the variables provide flexibility and power to MariaDB users. It allows them to effectively manage and manipulate data within the database environment.
Ways to Declaring Variable in MariaDB
To understand the Declaring Variable in MariaDB we need a table on which we will perform various operations. So here we will create a employees table.
Query:
CREATE TABLE employees (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (name, department, salary) VALUES
('Alice', 'Engineering', 60000),
('Bob', 'Engineering', 65000),
('Charlie', 'Marketing', 55000),
('David', 'Marketing', 58000),
('Emma', 'HR', 50000);
Output:
TABLE DATAMethod 1: Using SET Statement
The SET statement method is used for declaring variables in MariaDB. It allows for the assignment of values to variables.
Syntax:
SET variable_name = value;
Query:
SET @age = 30;
Output: No direct output. Variable @age is assigned the value 30.
Explanation: In the above query, We have declare a variable @age and assign a value of 30.
Method 2: Using DECLARE Statement
The DECLARE statement is typically used within stored procedures or blocks of procedural code. It enables the explicit declaration of variables along with their data types.
Syntax:
DECLARE variable_name datatype [DEFAULT value];
Query:
DECLARE employee_count INT DEFAULT 0;
SELECT COUNT(*) INTO employee_count FROM employees;
SELECT employee_count;
Output:

Explanation: In the above query, We have declares a variable called employee_count of type INT and initializes with a default value of 100.
Method 3: Using SELECT INTO Statement
The SELECT INTO statement is useful for assigning values from a query result directly to variables.
Syntax:
SELECT column_name INTO variable_name FROM table_name WHERE condition;
Query:
SELECT salary INTO @max_salary FROM employees WHERE department = 'Engineering';
Output:

Explanation: In the above query, We retrieve the maximum salary from the employees in the Engineering department and store it in the variable @max_salary.
Method 4: Using User-Defined Variables
User-defined variables in MariaDB are prefixed with the @symbol and can be assigned values using any valid SQL expression. They are session-specific and persist until the session ends.
Example:
Query:
SET @width = 5;
SET @height = 10;
SET @area = @width * @height;
Output:

Explanation: In the above query, We have calculate the area of a rectangle using user-defined variables called @width and @height and storing their result in the variable called @area.
Method 5: Using System Variables
MariaDB offers a range of system variables which is used for various purposes such as configuration settings and performance tuning.
Example:
SET GLOBAL max_connections = 1000;
Output: No output. The maximum number of connections allowed to the MariaDB server is set to 1000.
Explanation: In the above query, we set the maximum number of connections allowed to the MariaDB server using the system variable max_connections.
Conclusion
Mastering variable declaration in MariaDB is essential for effective database management and query optimization. Whether it's for storing temporary values, retrieving query results, or configuring server settings, understanding the different methods of variable declaration empowers users to efficiently manipulate data within the database environment. By leveraging the diverse capabilities offered by variable declaration, users can enhance the performance and functionality of their MariaDB databases
Similar Reads
SET Variable in MariaDB
In MariaDB, the SET statement is a main tool in variable handling. Users can assign values to variables, operate with them, and control database operations in various respects. This article includes a look at the SET variable usage in MariaDB and its syntax together with some examples. SET Variable
4 min read
Drop Table in MariaDB
MariaDB is one of the most widely used open-source relational database management systems, it offers many useful commands for manipulating database structures. It supports the same features that MySQL does but with additional features. Some of the most significant features are new storage engines, J
4 min read
Alter Table in MariaDB
MariaDB is an open-source RDBMS, that offers an extensive collection of features for handling database structures effectively. One important part of database management is changing tables to meet needs or improve performance. The ALTER TABLE command in MariaDB assists in these changes, allowing user
5 min read
Create Table in MariaDB
MariaDB is an open-source RDBMS that has become famous for its speed, and scalability. MariaDB Stores data in tables with structured relationships between them. In terms of working with databases, one crucial element involves the construction of tables for organizing and storing data effectively. In
4 min read
Comparison Operator in MariaDB
In the world of database management, precise comparisons are essential for accurate data retrieval and manipulation. MariaDB, a powerful open-source relational database system, offers a range of comparison operators to help us filter and query our data effectively. In this article, We will learn abo
5 min read
Unique Constraint in MariaDB
MariaDB uses relational databases similar to SQL. SQL is the precedence of MariaDB similar to the SQL mariaDB also uses some of the querying language to process through the databases. The main advantage of the MariaDB is that it runs on various operating systems and supports many programming languag
7 min read
MariaDB Drop View
MariaDB is an open-source relational database management system that is based on SQL(Structured query language). It is an improved version of MySQL and has various features, security, and performance when compared to MySQL. This database is open source with a strong community that can be trusted in
4 min read
Creating an index in MariaDB
MariaDB is an open-source and database managemeÂnt system. MariaDB is used for several purposes like as data warehousing, e-commerce, and logging applications. MariaDB is faster than MySQL in the replication and querying process. MariaDB supports invisible columns and temporary table space. In this
6 min read
Dropping an Index in MariaDB
In MariaDB, indexes play a crucial role in enhancing query performance by facilitating fast data access within a table. However, there are scenarios where you might need to drop indexes. This article explores various methods to drop indexes in MariaDB, including using DROP INDEX, ALTER TABLE DROP IN
4 min read
Show Tables in MariaDB
MariaDB is an open-source relational database management system (RDBMS). MariaDB is a very successful RDBMS that is known for its performance, scalability, and ease of use. When dealing with databases, understanding the structure and organization of their table type becomes important. MariaDB provid
4 min read