loading..
Ðóññêèé    English
07:59

Three-valued logic and WHERE clause

Let’s examine the following example.

Let it be required to define the ships with the unknown launch year (database “Ships”).

If we write

Solution 8.5.1

Console
Execute
  1. SELECT *
  2. FROM Ships
  3. WHERE launched = NULL;
then, however strange it may seem, we won’t get any entries even if such vessels exist there. As there are no ships with a known launch year in the available database, let’s create them to be able to check the truth of this statement:

Console
Execute
  1. SELECT *
  2. FROM (SELECT name, launched,
  3. CASE
  4. WHEN launched < 1940
  5. THEN NULL
  6. ELSE launched
  7. END year
  8. FROM Ships
  9. ) x
  10. WHERE year = NULL;

Here we added the ‘year’ field that contains NULL in case the vessel was launched earlier than 1940 to the subquery. 

So why didn’t we get anything? At this point one should remember that in SQL, as well as in the relational theory, three-valued logic is used. That means the true value of a comparison operation can be not only TRUE or FALSE, but also UNKNOWN. It’s caused by NULL values existence, and the comparison with it brings this case about. It’s intuitively understandable, if you keep in mind Null value is used to replace undefined data. If we ask: “Is the year of Bismarck’s launch 1939?” the answer will be: “I don’t know”, as there is no information on the launch year of this vessel in the database. And this “don’t know” turns into UNKNOWN.

And what happens if we use comparison with NULL value explicitly or implicitly (with the NULL-value in a column we compare with) in WHERE clause? The entry falls into the resulting set if the predicate gives TRUE. And that’s all – in case of FALSE or UNKNOWN the entry is not included. That’s why we got nothing in the example above, as we have UNKNOWN for all rows.

So, how can we get a list of ships with undefined launch year? For this purpose there’s a special IS NULL predicate in  SQL(Structured Query Language) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.SQL Standard (and its opposite IS NOT NULL). The true value of this predicate can’t be UNKNOWN, that means, the year is whether known (FALSE) or not (TRUE). Then we can write to solve our problem:

Solution 8.5.2

Console
Execute
  1. SELECT *
  2. FROM Ships
  3. WHERE launched IS NULL;

This is the standard, but what about realizations? All said above is correct about SQL Server. But it’s not the only way. Probably to make SQL programming more customary for those who use traditional programming languages it’s possible to turn off the standard NULL-value interpretation (it’s set on by default) by changing the ANSI_NULLS parameter properly:

  1. SET ANSI_NULLS OFF|ON

Type the following code in Management Studio (or in Query Analyzer for  A database management system (DBMS) by Microsoft Corporation.SQL Server 2000)  and everything will become clear:

Console
Execute
  1. SET ANSI_NULLS OFF
  2. SELECT *
  3. FROM (SELECT name, launched,
  4. CASE
  5. WHEN launched < 1940
  6. THEN NULL
  7. ELSE launched
  8. END year
  9. FROM Ships
  10. ) x
  11. WHERE year = NULL;

Bookmark and Share
Ðàçâåðíóòü âñ¸
Ñâåðíóòü âñ¸

Content:

Tags
aggregate functions Airport ALL AND AS keyword ASCII AVG Battles Bezhaev Bismarck C.J.Date calculated columns Cartesian product CASE cast CHAR CHARINDEX Chebykin check constraint classes COALESCE common table expressions comparison predicates Computer firm CONSTRAINT CONVERT correlated subqueries COUNT CROSS APPLY CTE data type conversion data types database schema DATEADD DATEDIFF DATENAME DATEPART DATETIME date_time functions DDL DEFAULT DEFAULT VALUES DELETE DISTINCT DML duplicates edge equi-join EXCEPT exercise (-2) More tags
The book was updated
month ago
©SQL-EX,2008 [Evolution] [Feedback] [About] [Links] [Team]
All right reserved.