MariaDB DEFAULT() Function
In MariaDB, DEFAULT() is a built-in function that returns the default value of a given table column.
MariaDB DEFAULT() syntax
Here is the syntax of the MariaDB DEFAULT() function:
DEFAULT(column_name)
Parameters
column_name-
Optional. a column name.
Return value
MariaDB DEFAULT() function returns the default value of the specified column.
This function will report an error if the specified column does not have a default value defined.
MariaDB DEFAULT() Examples
To demonstrate the usages of the DEFAULT() function, let us create a table named test_default using the following statement:
CREATE TABLE test_default (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
default_int INT NOT NULL DEFAULT 0,
default_date DATE NOT NULL DEFAULT (CURRENT_DATE)
);
Here, we create a table with 4 columns, where the following 2 columns have default values:
- The
default_intcolumn has a default value0. - The
default_datecolumn has a default value produced by(CURRENT_DATE).
Then, let’s insert some rows:
INSERT INTO test_default (name, default_int)
VALUES ('Tom', 1);
Let’s look at all the rows in the table:
SELECT * FROM test_default;
+----+------+-------------+--------------+
| id | name | default_int | default_date |
+----+------+-------------+--------------+
| 1 | Tom | 1 | 2023-02-03 |
+----+------+-------------+--------------+Now, let’s look at the default value of the default_int column using the DEFAULT() function:
SELECT DEFAULT(default_int) FROM test_default;
+----------------------+
| DEFAULT(default_int) |
+----------------------+
| 0 |
+----------------------+The default value of default_int is 0, which is also shown in the output of the above statement.
Let’s see what happens when we call DEFAULT() on the name column:
SELECT DEFAULT(name) FROM test_default;
ERROR 1364 (HY000): Field 'name' doesn't have a default valueThe DEFAULT() function reported an error because we did not define a default value for the name column.
Let’s see what happens when we call DEFAULT() on the default_date column:
SELECT DEFAULT(default_date) FROM test_default;
+-----------------------+
| DEFAULT(default_date) |
+-----------------------+
| 2023-02-03 |
+-----------------------+Conclusion
In MariaDB, DEFAULT() is a built-in function that returns the default value of a given table column.