English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
NULL values represent missing unknown data.
In most cases, table columns can store NULL values.
This chapter explains the IS NULL and IS NOT NULL operators.
The basic syntax for NULL when creating a table is as follows:
CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
Here, NOT NULL indicates that the field must always contain a value. This means that new records cannot be inserted or records updated if a value is not added to the field.
Fields with NULL values indicate that they can be left blank when creating records.
When querying data, NULL values may cause some issues because an unknown value is compared with any other value, and the result is always unknown.
Additionally, NULL and 0 cannot be compared because they are not equivalent.
Create COMPANY table (Download COMPANY SQL file ),数据内容如下:
w3codeboxdb# select * from COMPANY; id | name | age | address | salary ----+-------+-----+-----------+-------- 1 | Paul | 32 | California| 20000 2 | Allen | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | South-Hall| 45000 7 | James | 24 | Houston | 10000 (7 rows)
Next, we use the UPDATE statement to set several fields that can be set to NULL to NULL:
w3codeboxdb=# UPDATE COMPANY SET ADDRESS = NULL, SALARY = NULL where ID IN(6,7);
Now the COMPANY table looks like this:
w3codeboxdb=# select * from company; id | name | age | address | salary ----+-------+-----+---------------------+-------- 1 | Paul | 32 | California | 20000 2 | Allen | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | | 7 | James | 24 | | (7 rows)
IS NOT NULL
Now, we use the IS NOT NULL operator to list all records where the SALARY (salary) value is not empty:
w3codeboxdb=# SELECT ID, NAME, AGE, ADDRESS, SALARY FROM COMPANY WHERE SALARY IS NOT NULL;
The results are as follows:
id | name | age | address | salary ----+-------+-----+------------+-------- 1 | Paul | 32 | California | 20000 2 | Allen | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 (5 rows)
IS NULL
IS NULL is used to find fields with NULL values.
Below is the usage of the IS NULL operator, listing records where the SALARY (salary) value is empty:
w3codeboxdb=# SELECT ID, NAME, AGE, ADDRESS, SALARY FROM COMPANY WHERE SALARY IS NULL;
The results are as follows:
id | name | age | address | salary ----+-------+-----+---------+-------- 6 | Kim | 22 | | 7 | James | 24 | | (2 rows)