- MariaDB - Useful Functions
- MariaDB - Backup Loading Methods
- MariaDB - Backup Methods
- MariaDB - SQL Injection Protection
- MariaDB - Managing Duplicates
- MariaDB - Sequences
- MariaDB - Table Cloning
- MariaDB - Temporary Tables
- Indexes & Statistics Tables
- MariaDB - Alter Command
- MariaDB - Transactions
- MariaDB - Regular Expression
- MariaDB - Null Values
- MariaDB - Join
- MariaDB - Order By Clause
- MariaDB - Like Clause
- MariaDB - Delete Query
- MariaDB - Update Query
- MariaDB - Where Clause
- MariaDB - Select Query
- MariaDB - Insert Query
- MariaDB - Drop Tables
- MariaDB - Create Tables
- MariaDB - Data Types
- MariaDB - Select Database
- MariaDB - Drop Database
- MariaDB - Create Database
- MariaDB - Connection
- MariaDB - PHP Syntax
- MariaDB - Administration
- MariaDB - Installation
- MariaDB - Introduction
- MariaDB - Home
MariaDB Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MariaDB - Null Values
When working with NULL values, remember they are unknown values. They are not empty strings or zero, which are vapd values. In table creation, column specifications allow for setting them to accept null values, or reject them. Simply utipze a NULL or NOT NULL clause. This has apppcations in cases of missing record information pke an ID number.
User-defined variables have a value of NULL until exppcit assignment. Stored routine parameters and local variables allow setting a value of NULL. When a local variable has no default value, it has a value of NULL.
NULL is case-insensitive, and has the following apases −
UNKNOWN (a boolean value)
N
NULL Operators
Standard comparison operators cannot be used with NULL (e.g., =, >, >=, <=, <, or !=) because all comparisons with a NULL value return NULL, not true or false. Comparisons with NULL or possibly containing it must use the “<=>” (NULL-SAFE) operator.
Other available operators are −
IS NULL − It tests for a NULL value.
IS NOT NULL − It confirms the absence of a NULL value.
ISNULL − It returns a value of 1 on discovery of a NULL value, and 0 in its absence.
COALESCE − It returns the first non-NULL value of a pst, or it returns a NULL value in the absence of one.
Sorting NULL Values
In sorting operations, NULL values have the lowest value, so DESC order results in NULL values at the bottom. MariaDB allows for setting a higher value for NULL values.
There are two ways to do this as shown below −
SELECT column1 FROM product_tbl ORDER BY ISNULL(column1), column1;
The other way −
SELECT column1 FROM product_tbl ORDER BY IF(column1 IS NULL, 0, 1), column1 DESC;
NULL Functions
Functions generally output NULL when any parameters are NULL. However, there are functions specifically designed for managing NULL values. They are −
IFNULL() − If the first expression is not NULL it returns it. When it evaluates to NULL, it returns the second expression.
NULLIF() − It returns NULL when the compared expressions are equal, if not, it returns the first expression.
Functions pke SUM and AVG ignore NULL values.
Inserting NULL Values
On insertion of a NULL value in a column declared NOT NULL, an error occurs. In default SQL mode, a NOT NULL column will instead insert a default value based on data type.
When a field is a TIMESTAMP, AUTO_INCREMENT, or virtual column, MariaDB manages NULL values differently. Insertion in an AUTO_INCREMENT column causes the next number in the sequence to insert in its place. In a TIMESTAMP field, MariaDB assigns the current timestamp instead. In virtual columns, a topic discussed later in this tutorial, the default value is assigned.
UNIQUE indices can hold many NULL values, however, primary keys cannot be NULL.
NULL Values and the Alter Command
When you use the ALTER command to modify a column, in the absence of NULL specifications, MariaDB automatically assigns values.
Advertisements