SQL的三元逻辑(Three-Valued Logic)

-- TOC --

说简单点,Three-Valued Logic就是把NULL值加入与或非的逻辑运算,给它定个合理的值。

SQL allows any value to be assigned a NULL. NULL is not a value in itself (SQLite actually implements it as a unique valueless type), but is used as a marker or flag to represent unknown or missing data. The thought is that there are times when values for a specific row element may not be available or may not be applicable.

(SQLite允许primary key取null,所以要显示定义not null)

A NULL may not be a value, but it can be assigned to data elements that normally have values, and can therefore show up in expressions. The problem is that NULLs don’t interact well with other values. If a NULL represents an unknown that might be any possible value, how can we know if the expression NULL > 3 is true or false?

To deal with this problem, SQL must employ a concept called three-valued logic. Three-valued logic is often abbreviated TVL or 3VL, and is more formally known as ternary logic. 3VL essentially adds an “unknown” state to the familiar true/false Boolean logic system.

三元逻辑(Three-Valued Logic):



False and Null = False,因为and只要出现false,就false;

True and Null = Null,因为 Null 是不知道,可能是True,也可能是False;

同理可得到Null or (True or False)的情况。


-- EOF --

-- MORE --