Missing Information. We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios:

Similar documents
SQL: Data Manipulation Language. csc343, Introduction to Databases Diane Horton Winter 2017

SQL: csc343, Introduction to Databases Renée J. Miller and Fatemeh Nargesian and Sina Sina Meraji. Winter 2018

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

Why SQL? SQL is a very-high-level language. Database management system figures out best way to execute query

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

The Extended Algebra. Duplicate Elimination. Sorting. Example: Duplicate Elimination

Chapter 6 The database Language SQL as a tutorial

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

Relational Databases

Relational Model, Relational Algebra, and SQL

EXTENDED RELATIONAL ALGEBRA OUTERJOINS, GROUPING/AGGREGATION INSERT/DELETE/UPDATE

Relational Algebra and SQL

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

CS 464/564 Introduction to Database Management System Instructor: Abdullah Mueen

RELATIONAL DATA MODEL: Relational Algebra

More SQL. Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update

NULLs & Outer Joins. Objectives of the Lecture :

Subqueries. Must use a tuple-variable to name tuples of the result

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

Lecture 3 More SQL. Instructor: Sudeepa Roy. CompSci 516: Database Systems

SQL: Queries, Constraints, Triggers

Database Design and Programming

Basic form of SQL Queries

Querying Data with Transact SQL

SQL Continued! Outerjoins, Aggregations, Grouping, Data Modification

SQL. Chapter 5 FROM WHERE

Relational Algebra. Procedural language Six basic operators

CMPT 354: Database System I. Lecture 5. Relational Algebra

Relational Algebra. ICOM 5016 Database Systems. Roadmap. R.A. Operators. Selection. Example: Selection. Relational Algebra. Fundamental Property

SQL: Queries, Programming, Triggers

Enterprise Database Systems

CSEN 501 CSEN501 - Databases I

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

SQL - Lecture 3 (Aggregation, etc.)

SQL - Data Query language

CS122 Lecture 10 Winter Term,

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Chapter 6 The database Language SQL as a tutorial

Information Systems Engineering. SQL Structured Query Language DML Data Manipulation (sub)language

More on SQL Nested Queries Aggregate operators and Nulls

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Querying Data with Transact-SQL

SQL and Incomp?ete Data

CS 317/387. A Relation is a Table. Schemas. Towards SQL - Relational Algebra. name manf Winterbrew Pete s Bud Lite Anheuser-Busch Beers

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 3: Introduction to SQL

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

The SQL data-definition language (DDL) allows defining :

Querying Data with Transact SQL Microsoft Official Curriculum (MOC 20761)

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

SQL: Data Querying. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 4

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

RELATIONAL ALGEBRA II. CS121: Relational Databases Fall 2017 Lecture 3

SQL: Structured Query Language Nested Queries

SQL Data Querying and Views

SQL: Queries, Programming, Triggers. Basic SQL Query. Conceptual Evaluation Strategy. Example of Conceptual Evaluation. A Note on Range Variables

SQL: Data Manipulation Language

Chapter 3: Introduction to SQL

SQL queries II. Set operations and joins

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

CS 582 Database Management Systems II

Chapter 6: Formal Relational Query Languages

CS122 Lecture 4 Winter Term,

CSC 261/461 Database Systems Lecture 5. Fall 2017

Lecture 3 SQL. Shuigeng Zhou. September 23, 2008 School of Computer Science Fudan University

Semantic Errors in Database Queries

Querying Data with Transact-SQL

SQL:Union, Intersection, Difference. SQL: Natural Join

More SQL: Complex Queries, Triggers, Views, and Schema Modification

Learning Alliance Corporation, Inc. For more info: go to

Outline. Note 1. CSIE30600 Database Systems More SQL 2

Database Management Systems. Chapter 5

Database Usage (and Construction)

RAQUEL s Relational Operators

1. Data Definition Language.

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

Join (SQL) - Wikipedia, the free encyclopedia

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

Relational Algebra & Calculus. CS 377: Database Systems

Database Management

The SQL Query Language. Creating Relations in SQL. Adding and Deleting Tuples. Destroying and Alterating Relations. Conceptual Evaluation Strategy

SQL: Part II. Introduction to Databases CompSci 316 Fall 2018

Chapter 7 Advanced SQL. Objectives

Simple queries Set operations Aggregate operators Null values Joins Query Optimization. John Edgar 2

Database Applications (15-415)

Joins, NULL, and Aggregation

Ian Kenny. November 28, 2017

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL. Overview

SQL. Lecture 4 SQL. Basic Structure. The select Clause. The select Clause (Cont.) The select Clause (Cont.) Basic Structure.

Principles of Data Management

Transcription:

NULL values

Missing Information We ve assumed every tuple has a value for every attribute. But sometimes information is missing. Two common scenarios: Missing value. E.g., we know a student has some email address, but we don t know what it is. Inapplicable attribute. E.g., the value of attribute spouse for an unmarried person. 40

How to represent that? One possibility: use a special value as a placeholder. E.g., If age unknown, use 0. If StNum unknown, use 999999999. Pros and cons? Better solution: use a value not in any domain. We call this a null value. Tuples in SQL relations can have NULL as a value for one or more components. 41

In SQL we have 3 truth values Comparisons can evaluate to TRUE or FALSE. But if one or both operands to a comparison is NULL, the comparison always evaluates to UNKNOWN. 42

Combining truth values We need to know how the three truth-values combine with AND, OR and NOT. Can think of it in terms of the truth table. Or can think in terms of numbers: TRUE = 1, FALSE = 0, UNKNOWN = 0.5 AND is min, OR is max, NOT x is (1-x), i.e., it flips the value 43

The three-valued truth table A B A and B A or B T T T T TF or FT F T F F F F TU or UT U T FU or UF F U U U U U 44

Thinking of the truth-values as numbers A B as nums A and B min A or B max T T 1, 1 T 1 T 1 TF or FT 1, 0 F 0 T 1 F F 0, 0 F 0 F 0 TU or UT 1, 0.5 U 0.5 T 1 FU or UF 0, 0.5 F 0 U 0.5 U U 0.5, 0.5 U 0.5 U 0.5 45

Surprises from 3-valued logic Some laws you are used to still hold in threevalued logic. For example, AND is commutative. But others don t. For example, The law of the excluded middle breaks: (p or (NOT p)) might not be TRUE. (0*x) might not be 0. 46

Example: NULL surprises

Impact of NULL values on WHERE A tuple is in a query result iff the WHERE clause is TRUE. UNKNOWN is not good enough. WHERE is picky. 48

Impact of NULL values on aggregation Aggregation ignores NULL. NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column (unless every value is NULL). If there are no non-null values in a column, then the result of the aggregation is NULL. Exception: COUNT of an empty set is 0. 49

Aggregation ignores NULLs some nulls in A All nulls in A min(a) max(a) null sum(a) ignore the nulls avg(a) count(a) 0 count(*) all tuples count 50

Example: NULLs and aggregation

Checking for NULL You can compare an attribute value to NULL with IS NULL IS NOT NULL Example: SELECT * FROM Course WHERE breadth IS NULL; 52

Views

The idea A view is a relation defined in terms of stored tables (called base tables) and other views. Access a view like any base table. Two kinds of view: Virtual: no tuples are stored; view is just a query for constructing the relation when needed. Materialized: actually constructed and stored. Expensive to maintain! PostgreSQL did not support materialized views until version 9.3 (which we are not running). 54

Example: View Definition A view for students who earned an 80 or higher in a CSC course. CREATE VIEW topresults as SELECT firstname, surname, cnum FROM Student, Took, Offering WHERE Student.sid = Took.sid AND Took.oid = Offering.oid AND grade >= 80 AND dept = 'CSC'; 55

Uses for views Break down a large query. Provide another way of looking at the same data, e.g., for one category of user. 56

Subqueries

Subqueries in a FROM clause In place of a relation name in the FROM clause, we can use a subquery. The subquery must be parenthesized. Must name the result, so you can refer to it in the outer query. 58

Example What does this do? SELECT sid, dept cnum as course, grade FROM Took, (SELECT * FROM Offering WHERE instructor= Horton ) Hoffering WHERE Took.oid = Hoffering.oid; This FROM is analogous to: Took ρhoffering («subquery») Can you suggest another version? 59

Subquery as a value in a WHERE If a subquery is guaranteed to produce one tuple, then the subquery can be used as a value. Simplest situation: that one tuple has only one component. 60

Example Find all students with a cgpa greater than that of student 99999. SELECT sid, surname FROM Student WHERE cgpa > (SELECT cgpa FROM Student where SID = 99999); This is analogous to something we can t do in RA: πsid, surname σcgpa > («subquery») 61

Special cases What if the subquery returns NULL? What if the subquery could return more than one value? When a subquery can return multiple values, we can make comparisons using a quantifier: cgpa > all of them cgpa > at least one of them 62

More operators for use in a WHERE clause

The Operator ANY Syntax: x «comparison» ANY («subquery») Semantics: Its value is true iff the comparison holds for at least one tuple in the subquery result, i.e., y «subquery results» x «comparison» y x can be a list of attributes, but this feature is not supported by psql. 64

Potential confusion In some cases, the query translates to English better using the word some than any. Example: WHERE cgpa >= ANY (SELECT cgpa...) With equality as the comparison, it does not mean what equals usually means in programming. E.g., WHERE x = ANY (SELECT AGE... ) Always refer back to the semantics. 65

The Operator ALL Syntax: x «comparison» ALL («subquery») Semantics: Its value is true iff the comparison holds for every tuple in the subquery result, i.e., y «subquery results» x «comparison» y x can be a list of attributes, but this feature is not supported by psql. 66

Example: any and all

The Operator IN Syntax: x IN («subquery») Semantics: Its value is true iff x equals at least one of the tuples in the subquery result. x can be a list of attributes, and psql does support this feature. 68

Example: IN SELECT sid, dept cnum AS course, grade FROM Took NATURAL JOIN Offering WHERE grade >= 80 AND (cnum, dept) IN ( SELECT cnum, dept FROM Took NATURAL JOIN Offering NATURAL JOIN Student WHERE surname = 'Lakemeyer'); 69

An equivalence? Suppose we have tables R(a, b) and S(b, c). Can we express this query without using IN? SELECT a FROM R WHERE b IN (SELECT b FROM S); Is the result the same? 70

The Operator EXISTS Syntax: EXISTS («subquery») Semantics: Its value is true iff the subquery has at least one tuple. 71

Example What does this do? SELECT instructor FROM Offering Off1 WHERE NOT EXISTS ( SELECT * FROM Offering WHERE oid <> Off1.oid AND instructor = Off1.instructor ); 72

Scope Queries are evaluated from the inside out. If a name might refer to more than one thing, use the most closely nested one. If a subquery refers only to names defined inside it, it can be evaluated once and used repeatedly in the outer query. If it refers to any name defined outside of itself, it must be evaluated once per tuple in the outer query. These are called correlated subqueries. 73

Renaming can make scope explicit SELECT instructor FROM Offering Off1 WHERE NOT EXISTS ( SELECT * FROM Offering Off2 WHERE Off2.oid <> Off1.oid and Off2.instructor = Off1.instructor ); 74

Summary: where subqueries can go As an operand to a set operation. As a relation in a FROM clause. As a value in a WHERE clause. With ANY, ALL, IN or EXISTS in a WHERE clause. Reference: textbook, section 6.3 75

Outer joins

Dangling tuples With joins that require some attributes to match, tuples with no match are left out of the results. We say that they are dangling. An outer join preserves dangling tuples by padding them with NULLs in the other relation. 77

Three kinds of outer join LEFT OUTER JOIN Preserves dangling tuples from the relation on the LHS by padding with nulls on the RHS. RIGHT OUTER JOIN The reverse. FULL OUTER JOIN Does both. Join syntax summarized on next slide... 78

Summary of join expressions Cartesian product A cross join B (same as A, B) Theta-join A join B on C: no padding of tuples A {left right full} join B on C: padding Natural join A natural join B: no padding of tuples A natural {left right full} join B: padding 79

Keywords INNER and OUTER There are keywords INNER and OUTER, but you never need to use them. Your intentions are clear anyway: You get an outer join iff you use the keywords LEFT, RIGHT, or FULL. If you don t use the keywords LEFT, RIGHT, or FULL you get an inner join. 80

Example: joining R and S various ways R A B S B C 1 2 4 5 2 3 6 7 R NATURAL JOIN S A B C 1 2 3 81

Example R A B S B C 1 2 4 5 2 3 6 7 R NATURAL FULL JOIN S A B C 1 2 3 4 5 NULL NULL 6 7 82

Example R A B S B C 1 2 4 5 2 3 6 7 R NATURAL LEFT JOIN S A B C 1 2 3 4 5 NULL 83

Example R A B S B C 1 2 4 5 2 3 6 7 R NATURAL RIGHT JOIN S A B C 1 2 3 NULL 6 7 84

Modifying a Database

Database Modifications Queries return a relation. A modification command does not; it changes the database in some way. Three kinds of modifications: Insert a tuple or tuples. Delete a tuple or tuples. Update the value(s) of an existing tuple or tuples. 86

Two ways to insert We ve already seen two ways to insert tuples into an empty table: INSERT INTO «relation» VALUES «list of tuples»; INSERT INTO «relation» («subquery»); These can also be used to add tuples to a nonempty table. 87

Naming attributes in INSERT Sometimes we want to insert tuples, but we don t have values for all attributes. If we name the attributes we are providing values for, the system will use NULL or a default for the rest. Convenient! 88

Example: insert with subquery

Deletion Delete tuples satisfying a condition: DELETE FROM «relation» WHERE «subquery»; Delete all tuples: DELETE FROM «relation»; 90

Example 1: Delete Some Tuples DELETE FROM Course WHERE NOT EXISTS ( SELECT * FROM Took JOIN Offering ON Took.oid = Offering.oid WHERE grade > 50 AND Offering.dept = Course.dept AND Offering.cnum = Course.cnum ); 91

Updates To change the value of certain attributes in certain tuples to given values: UPDATE «relation» SET «list of attribute assignments» WHERE «condition on tuples»; 92

Example: update one tuple Updating one tuple: UPDATE Student SET campus = UTM WHERE sid = 999999; Updating several tuples: UPDATE Took SET grade = 50 WHERE grade >= 47 and grade < 50; 93