MariaDB: Declaring Variables
This MariaDB tutorial explains how to declare variables in MariaDB with syntax and examples.
What is a variable in MariaDB?
In MariaDB, a variable allows a programmer to store data temporarily during the execution of code.
Syntax
The syntax to declare a variable in MariaDB is:
DECLARE variable_name datatype [ DEFAULT initial_value ]
Parameters or Arguments
- variable_name
- The name to assign to the variable.
- datatype
- The datatype to assign to the variable.
- DEFAULT initial_value
- Optional. It is the value initially assigned to the variable when it is declared. If an initial_value is not specified, the variable is assigned an initial value of NULL.
Example - Declaring a variable
Below is an example of how to declare a variable in MariaDB called Website.
DECLARE Website VARCHAR(45);
This example would declare a variable called Website as a VARCHAR(45) data type.
You can then later set or change the value of the Website variable, as follows:
SET Website = 'CheckYourMath.com';
This SET statement would set the Website variable to a value of 'CheckYourMath.com'.
Example - Declaring a variable with an initial value (not a constant)
Below is an example of how to declare a variable in MariaDB and give it an initial value. This is different from a constant in that the variable's value can be changed later.
DECLARE Website VARCHAR(45) DEFAULT 'CheckYourMath.com';
This would declare a variable called Website as a VARCHAR(45) data type and assign an initial value of 'CheckYourMath.com'.
You could later change the value of the Website variable, as follows:
SET Website = 'TechOnTheNet.com';
This SET statement would change the Website variable from a value of 'CheckYourMath.com' to a value of 'TechOnTheNet.com'.
Advertisements