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

  • Learning PostgreSQL 11
  • Salahaldin Juba Andrey Volkov
  • 304字
  • 2021-07-02 13:11:37

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)
主站蜘蛛池模板: 勐海县| 图木舒克市| 郯城县| 赤水市| 三门县| 丰台区| 冕宁县| 扎兰屯市| 平凉市| 昭平县| 临江市| 象州县| 海兴县| 清水县| 太湖县| 海原县| 杂多县| 台中市| 安西县| 赞皇县| 云林县| 阿巴嘎旗| 三原县| 建阳市| 潼关县| 闻喜县| 谷城县| 开阳县| 电白县| 老河口市| 和平县| 扶沟县| 新乡市| 贵定县| 溧阳市| 嘉荫县| 额济纳旗| 阿城市| 抚松县| 车致| 波密县|