- 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)
推薦閱讀
- Web應(yīng)用系統(tǒng)開發(fā)實踐(C#)
- Developing Mobile Web ArcGIS Applications
- Learning RabbitMQ
- 前端架構(gòu):從入門到微前端
- 區(qū)塊鏈:以太坊DApp開發(fā)實戰(zhàn)
- PLC編程及應(yīng)用實戰(zhàn)
- Java Web開發(fā)技術(shù)教程
- Unity 2D Game Development Cookbook
- Python Data Structures and Algorithms
- Visual Basic程序設(shè)計習(xí)題與上機實踐
- Mastering Python Design Patterns
- 細(xì)說Python編程:從入門到科學(xué)計算
- Python青少年趣味編程
- Ext JS 4 Plugin and Extension Development
- Ionic3與CodePush初探:支持跨平臺與熱更新的App開發(fā)技術(shù)