官术网_书友最值得收藏!

The null value

Predicates in relational databases use three-valued logic (3VL), where there are three truth values: true, false, and null,. In a relational database, the third value, null, can be interpreted in many ways, such as unknown data, missing data, not applicable, or will be loaded later. The 3VL is used to remove ambiguity. For example, no two null values are equal.

In the next chapter, you will learn how to connect to the database and run queries. Now, I would like to show how a logical OR/AND truth table can be generated by the SQL language:

Logical AND and OR operators are commutative, that is,  A AND B = B AND A.
\pset null null
WITH data (v) as (VALUES (true), (false),(null))
SELECT DISTINCT
first.v::TEXT as a,
second.v::TEXT as b,
(first.v AND second.v)::TEXT AS "a and b",
(first.v OR second.v)::TEXT as "a or b"
FROM
data as first cross join
data as second
ORDER BY a DESC nulls last, b DESC nulls last;
a | b | a and b | a or b
-------+-------+---------+--------
true | true | true | true
true | false | false | true
true | null | null | true
false | true | false | true
false | false | false | false
false | null | false | null
null | true | null | true
null | false | false | null
null | null | null | null
(9 rows)

The following table, which is generated by SQL, shows the NOT truth operator:

WITH data (v) as (VALUES (true), (false),(null)) SELECT v::text as a, (NOT v)::text as "NOT a" FROM data order by a desc nulls last;
a | NOT a
-------+-------
true | false
false | true
null | null
(3 rows)
主站蜘蛛池模板: 馆陶县| 成武县| 贞丰县| 阜宁县| 钟祥市| 册亨县| 钟山县| 尚志市| 横山县| 蓬溪县| 建始县| 杭锦后旗| 黎城县| 南阳市| 如东县| 中西区| 重庆市| 新昌县| 阳曲县| 昭平县| 彩票| 华容县| 隆化县| 巩留县| 兴安县| 扶余县| 金阳县| 平凉市| 彰化市| 封开县| 阳曲县| 平乐县| 阿巴嘎旗| 涟源市| 澄江县| 资中县| 凤阳县| 长宁区| 额济纳旗| 拉萨市| 商洛市|