English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SQLite NULL Values

SQLite NULL is the term used to represent missing values. Empty values in the table are values that look empty in the field.

Fields with NULL values are fields without values. It is very important to understand that NULL values are different from zero values or fields containing spaces.

Syntax

The following}}NULLBasic syntax used when creating a table.

sqlite> CREATE TABLE COMPANY(
   ID INT PRIMARY KEY NOT NULL,
   NAME TEXT NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR;50),
   SALARY REAL;

Here,NOT NULLThis indicates that the column should always accept an explicit value of the given data type. There are two columns we did not use NOT NULL, which means these columns may be NULL.

Fields with NULL values are fields left blank during the record creation process.

Online Examples

NULL values may cause problems in selecting data because when unknown values are compared with any other value, the result is always unknown and not included in the final result. Consider the following records in the table COMPANY-

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22          South-Hall  45000.0
7           James       24          Houston     10000.0

Let's use the UPDATE statement to set some nullable values to NULL as shown below:

sqlite> UPDATE COMPANY SET ADDRESS = NULL, SALARY = NULL where ID IN(6,7);

Now, the COMPANY table will have the following records.

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0
6           Kim         22
7           James       24

Next, let's take a look atIS NOT NULLUsage of the operator to list all records where SALARY is not NULL.

sqlite> SELECT ID, NAME, AGE, ADDRESS, SALARY
        FROM COMPANY
        WHERE SALARY IS NOT NULL;

The above SQLite statement will produce the following result-

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
1           Paul        32          California  20000.0
2           Allen       25          Texas       15000.0
3           Teddy       23          Norway      20000.0
4           Mark        25          Rich-Mond   65000.0
5           David       27          Texas       85000.0

The following}}IS NULLUsage of the operator, which will list all records where SALARY is NULL.

sqlite> SELECT ID, NAME, AGE, ADDRESS, SALARY
        FROM COMPANY
        WHERE SALARY IS NULL;

The above SQLite statement will produce the following result.

ID          NAME        AGE         ADDRESS     SALARY
----------  ----------  ----------  ----------  ----------
6           Kim         22
7           James       24