About the three-valued logic in the database (tree-value-logic)

Source: Internet
Author: User

In SQL, a logical expression (also called a predicate) can have three values: True, False, Unknown, which is called the three-value logic, which is a unique attribute of SQL.

In most programming languages, the logical expression has only two values, which is true and false, but in SQL, there are also unknown. Unknown generally appear in a logical expression with NULL, such as the following: Null>2, Null+x>y, Null=null. The results are unknown. Null this symbol represents a missing value, and when you compare a missing value to another value, you get the unknown result.

At some point, unknown and null can be easily confused on processing, such as the following: Not true equals false,not false equals True, but unknown's reverse is unknown.

In different language elements (that is, in SQL statements, different modules), unknown logical results and NULL are handled differently.

1, in the query filter (on, where, have) unknown as false to handle

2, in the check constraint, unknown is treated as true, such as a check constraint on a field in a table, then this table may not contain 2 rows or more of the field value is unknown row.

3. In a filter, if you compare two null values, the resulting value is unknown, but will still be treated as false.

4, in unique constraints, set operations (such as union and except), sorting, grouping operations, the two null values are considered equal.

⑴ this way, in a column with a unique constraint, you cannot insert two rows in the table that have null for the column value

The ⑵group by clause will divide all null values into a group, and the ORDER BY clause will arrange all data with null values together

⑶ when comparing rows in two sets, the set operator considers that the different null values are equal.

Basically read "Technical insider-t-sql" a book to get the notes, if there are errors please correct me, thank you.

About the three-valued logic in the database (tree-value-logic)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.