1 / 8

NULL Values in SQL

NULL Values in SQL. CSE 4504/6504 Lab. Null Values. Special values that SQL provides for the column values - either unknown or inapplicable Example: -- Rating for a new Sailor INSERT INTO Sailors (sid, sname, rating, age) VALUES (101, ‘John Doe’, null, 18);

marinel
Download Presentation

NULL Values in SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. NULL Values in SQL CSE 4504/6504 Lab

  2. Null Values • Special values that SQL provides for the column values - either unknown or inapplicable • Example: -- Rating for a new Sailor INSERT INTO Sailors (sid, sname, rating, age) VALUES (101, ‘John Doe’, null, 18); -- Maiden name for a male Student

  3. Comparisons Using Null Values • The column value with Null is defined as unknown • The result of the comparison (>, <, =, etc.) between Null values or between a Null value and a known value is always unknown. • In SQL: -- IS NULL -- IS NOT NULL

  4. Logical Connectives AND, OR, and NOT Third-value logic: NOT unknown = unknown attrA OR unknown = true (when attrA = true) = unknown (when attrA = false) attrA AND unknown = false (when attrA = false) = unknown (when attrA = true)

  5. Impact on SQL Constructs • SQL queries eliminates rows that evaluate unknown. • Two rows are duplicates if corresponding columns are both null. • Arithmetic operations +, -, *, and / all return null if one their arguments is null.

  6. In Aggregate Operations • Null values get counted in COUNT (*). • All the other aggregate operations (SUM, AVG, MIN, MAX, and variations using DISTINCT) simply discard null values.

  7. Outer Joins Instance of Sailors Instance of Reserves SELECT Sailors.sid, Reserves.bid FROM Sailors NATURAL LEFT OUTER JOIN Reserves;

  8. Disallowing Null Values • Null values can be disallowed by specifying NOT NULL as part of the field definition. • There is an implicit NOT NULL constraint for primary keys.

More Related