- 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)
推薦閱讀
- C語言程序設(shè)計教程(第2版)
- BeagleBone Media Center
- Visual Basic程序設(shè)計習(xí)題解答與上機指導(dǎo)
- C語言程序設(shè)計
- Learning FuelPHP for Effective PHP Development
- Learning OpenStack Networking(Neutron)(Second Edition)
- 利用Python進行數(shù)據(jù)分析
- 動手學(xué)數(shù)據(jù)結(jié)構(gòu)與算法
- Learning Hadoop 2
- Citrix XenServer企業(yè)運維實戰(zhàn)
- Mastering AWS Security
- R語言:邁向大數(shù)據(jù)之路(加強版)
- Shopify Application Development
- Functional Python Programming
- Clojure編程樂趣