NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Ver 1,12/09/2012Kode :CCs 111,sistem basisdataFASILKOM Chapter 2: Relational Model Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
1 Lecture 12: Further relational algebra, further SQL
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
From Relational Algebra to the Structured Query Language Rose-Hulman Institute of Technology Curt Clifton.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
Introduction to Structured Query Language (SQL)
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
1 Chapter 5 SQL: QUERIES, CONSTRAINTS, TRIGGERS. 2 INTRODUCTION - The current presentation is consistent with both SQL-92 and SQL: 99 (differences will.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
Database Systems More SQL Database Design -- More SQL1.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Chapter 3 Single-Table Queries
Missing Information Database Systems Lecture 10 Natasha Alechina.
BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E) 1. LECTURE OUTLINE  SQL Data Definition and Data Types  Specifying Constraints in SQL  Basic Retrieval Queries.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
The Relational Database Model
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
Yufis Azhar – Teknik Informatika – UMM.  Aggregation function takes a collection of values (of a single attribute) and returns a single value as a result.
SE305 Database System Technology 23/10/2014 Quiz-2.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
Database Terms Hernandez, Chapter 3. Data/Information The values you store in the database are data. Pieces of Data in and of themselves is not particularly.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
Installment Numbers 6 Answers to Puzzle Corner Problems (Installment Numbers1-5) 會研所 黃潔.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
CS 405G: Introduction to Database Systems SQL III.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Thinking in Sets and SQL Query Logical Processing.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Subqueries CIS 4301 Lecture Notes Lecture /23/2006.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 10 July 10,2012 More SQL: Complex Queries, Triggers,
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Aggregating Data Using Group Functions
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Schedule Today: Next After that Subqueries, Grouping and Aggregation.
Chapter 3: Relational Model III
Aggregating Data Using Group Functions
More SQL Nested and Union queries, and more
CS 405G: Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL: Structured Query Language
Presentation transcript:

NULL VALUES CHAPTER 5 (6/E) CHAPTER 8 (5/E) 1

LECTURE OUTLINE  Dealing with null values Three-valued logic Effects in WHERE clauses IS NULL Effects on aggregation Effects on GROUP BY, set operations, and SELECT DISTINCT Treatment in ORDER BY clauses Effects in CHECK constraints  Outer joins 2

SEMANTICS OF NULL  Recall possible meanings of NULL Unknown value Unavailable or withheld value Not applicable attribute  Each stored NULL value incomparable to every other stored value Even if other value also NULL unknown ≟ 5  unknown unknown ≟ unknown  unknown Comparisons involving unknown values are neither true nor false.  Thus, SQL uses a three-valued logic: TRUE, FALSE, and UNKNOWN 3

THREE-VALUED LOGIC  Similarly, any operation involving an unknown value produces an unknown value for the result. e.g., unknown + 5  unknown 4

EVALUATING WHERE  Recall that WHERE clause evaluates each tuple in turn and returns only those for which the condition evaluates to true.  Tuples that evaluate to false or unknown are rejected. Cannot use WHERE phone = NULL to test for null value in a tuple.  Many tautologies do not hold for columns with NULLs. e.g., no “law of the excluded middle” SELECT * FROM Student WHERE age > 18 OR NOT age > 18 might not return all Student tuples 5

IS NULL  Most SQL operators are NULL-intolerant. They return unknown if an operand is NULL.  SQL provides special test that is NULL-tolerant IS [NOT] NULL  Need to account for NULLs when formulating queries Not handling NULLs is a common source of errors 6

WHEN NULLS ARE IGNORED  Consider aggregating values for budget in the following. e.g., max(budget), sum(budget), average(budget) NULL values in tuples ignored for aggregation (even for COUNT ) Only non-NULL values included in aggregations. i.e., sum() handled differently from + Example: SELECT COUNT(*), COUNT(budget), AVERAGE(gross-budget) FROM Film WHERE genre = 'comedy'; all comedies counted for first aggregation; only comedies with non-NULL budget counted for second aggregation; only comedies with non-NULL budget and non-NULL gross included in third aggregation 7 Film titlegenreyeardirectorminutes budget gross

WHEN ALL NULLS ARE TREATED EQUAL  Grouping and set operations treat all NULLs as the same value e.g., GROUP BY budget forms separate group for all tuples with NULL value in budget Similarly for set operations: all NULLs treated as if a single value e.g., {(A,B,NULL),(A,B,C)} ∩ {(A,B,D),(A,B,NULL)} = {(A,B,NULL)} (SELECT genre, budget FROM Film WHERE gross > ) UNION (SELECT genre, budget FROM Film WHERE year > 2000) Similarly, too, for duplicate elimination with SELECT DISTINCT  Finally ORDER BY NULLs sorted together, but sort order with respect to other values is implementation-dependent 8

NULLS IN SQL’S DDL  By default, must be aware of possible NULLs for all columns.  Recall, however, a column can be declared NOT NULL. NULL values cannot occur; querying simplified Recall: Primary key columns must be declared NOT NULL  Unlike WHERE clause, CHECK constraints and FOREIGN KEY constraints ensure that no tuple returns false. Therefore NULLs accepted e.g., CHECK (age > 18) allows tuples with NULL value for age 9

JOIN OPERATOR 10

LEFT OUTER JOIN OPERATOR  Every tuple in left table appears in result If matching tuple(s) in right table, works like inner join If no matching tuple in right table, one tuple in result with left tuple values padded with NULL values for columns of right table Table1 LEFT [OUTER] JOIN Table2 ON SELECT * FROM Customer LEFT JOIN Sale ON Customer.custid = Sale.custid 11 Customer custidnameaddressphone 1205Lee633 S. First Willis41 King Smith213 Main Ng5 Queen N Harrison808 Main Sale saleiddatecustid A175 Dec3122 B8235 Dec1697 B2199 Dec3122 C4115 Dec1205 X0023 DecNULL Customer.custidnameaddressphonesaleiddateSale.custid 1205Lee633 S. First C4115 Dec Willis41 King A175 Dec3122 Willis41 King B2199 Dec Smith213 Main NULL 1697Ng5 Queen N B8235 Dec Harrison808 Main NULL

OTHER OUTER JOIN OPERATORS  Table1 RIGHT [OUTER] JOIN Table2 ON Every tuple in right table appears in result (padded on left if needed)  Table1 FULL [OUTER] JOIN Table2 ON Every tuple in either table appears in result (padded if needed) SELECT * FROM Customer FULL JOIN Sale ON Customer.custid = Sale.custid 12 Customer custidnameaddressphone 1205Lee633 S. First Willis41 King Smith213 Main Ng5 Queen N Harrison808 Main Sale saleiddatecustid A175 Dec3122 B8235 Dec1697 B2199 Dec3122 C4115 Dec1205 X0023 Dec0000 Customer.custidnameaddressphonesaleiddateSale.custid 1205Lee633 S. First C4115 Dec Willis41 King A175 Dec3122 Willis41 King B2199 Dec Smith213 Main NULL 1697Ng5 Queen N B8235 Dec Harrison808 Main NULL X0023 Dec0000

LECTURE SUMMARY  NULL values need careful consideration. Most operators are NULL-intolerant. Some queries must use IS [NOT] NULL to operate correctly. Aggregations ignore NULLs. Partitioning and set operators treat all NULLs as equal. Check constraints are NULL-tolerant. Include NOT NULL for column declarations where appropriate. Recall: required for primary keys  Outer joins LEFT, RIGHT, and FULL 13