sqlite

Database; use; embedded relational database

null

英[nʌl] 美[nʌl ]

adj.<Technology> Zero value; equal to zero; (agreement) has no legal effect; invalid

SQLite NULL function syntax

Function:SQLite's NULL is used to represent an item with a missing value. A NULL value in a table is a value that appears as blank in the field. A field with a NULL value is a field with no value. It is important to understand that NULL values ​​are different from zero values ​​or fields containing spaces.

Syntax: The basic syntax for using NULL when creating a table is as follows:

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

Here, NOT NULL means that the column always accepts the given data Explicit value of type. There are two columns here that we are not using NOT NULL, which means that these two columns cannot be NULL.

Fields with NULL values ​​can be left empty when records are created.

SQLite NULL function example

NULL 值在选择数据时会引起问题,因为当把一个未知的值与另一个值进行比较时,结果总是未知的,且不会包含在最后的结果中。假设有下面的表,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

让我们使用 UPDATE 语句来设置一些允许空值的值为 NULL,如下所示:

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

现在,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
7           James       24

接下来,让我们看看 IS NOT NULL 运算符的用法,它用来列出所有 SALARY 不为 NULL 的记录:

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

上面的 SQLite 语句将产生下面的结果:

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

下面是 IS NULL 运算符的用法,将列出所有 SALARY 为 NULL 的记录:

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

上面的 SQLite 语句将产生下面的结果:

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