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

2020年10月26日 / 6次阅读 / Last Modified 2020年10月26日

说简单点,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

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


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

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

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

-- EOF --



电子邮件地址不会被公开。 必填项已用*标注



©Copyright 麦新杰 Since 2019 Python笔记

go to top