When SQL is just getting started, we filter rows with a column value of null. The following method is generally used:
SELECT * FROM Table AS T WHERE T.Col=NULL
In fact, this writing method cannot get the desired result. The solution we found on the internet is T. Col is null. Although this method can obtain data, I and some of my peers do not know why T. Col = NULL cannot obtain the result of a row whose col is null.
Recently I want to enhance the knowledge of SQL, so I found sqlserver 2005 Technical Insider: T-SQL query this book. The three-value Logic Knowledge points are shown in the book. This knowledge point helps me solve the problem above.
What is a three-value Logic?
The following is the original article of the book:
In SQL, the possible values of logical expressions include true, false, and unknown. They are called three-value logic. The three-value Logic is unique to SQL. The logical expressions in most programming languages are only true or false. The unknown logical values in SQL usually appear in logical expressions that contain null values. For example, the values of the following three expressions are unknown:
NULL<42;NULL=NULL;X+NULL>Y;
Null usually indicates a lost or unrelated value. When comparing a lost value with another value (this value may also be null), the logical result is always unknown.
It is easy to confuse unknown logic results with null. Not true is equal to false, not false is equal to true, and the negative unknown (not unknown) or unknown.
The unknown logical result and null are treated differently in different language elements. For example, all query filters (on, where, and having) treat unknown as false. The rows whose filter is unknown are excluded from the result set, and the unknown value in the check constraint is treated as true. The construction table contains a check constraint that the salary column value must be greater than 0. It is acceptable to insert salary to the table as null because (null> 0) is equal to unknown, the check constraint is considered to be the same as true.
When two null values are compared in the filter, unknown is obtained. It is treated as false, as if one null is not equal to the other null.
Unique constraints. The sorting operation and grouping operation assume that the two null values are equal.
If a column in the table defines the unique constraint, you cannot insert two rows whose column value is null to the table.
The group by clause groups all null values in a group.
The order by clause lists all null values.
Understanding the unknown logical result and null in different language elements is advantageous, which can avoid future troubles.
A lot of dense words are dizzy. Let me explain it in plain words.
For example, in C #, the values of conditional expressions in C # are true and false. However, there is a third condition value in SQL: unknown.
In C #If (null = NULL)The result is:True,Not equalFalse.
However, when SQL uses =, <,>, <>, <=,> = to determine the null value, it does not get the expectedTrueOrFalse,Unknown.
SQL pairUnknown can be processed in two ways:
1. In the where, on, and having clauses, unknown is processed as false. So I had my question at the beginning.
2. the unknown value in the check constraint is treated as true. The construction table contains a check constraint that the salary column value must be greater than 0. It is acceptable to insert salary to the table as null because (null> 0) is equal to unknown, in the check constraintsTrueSame.
According to the above explanation, we know why is null or is not null is required for filtering null in the query, but the regular condition expression cannot be filtered out.