CS 405G: Introduction to Database Systems SQL III.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Relational Algebra Ch. 7.4 – 7.6 John Ortiz. Lecture 4Relational Algebra2 Relational Query Languages  Query languages: allow manipulation and retrieval.
SQL Part II: Advanced Queries. 421B: Database Systems - SQL Queries II 2 Aggregation q Significant extension of relational algebra q “Count the number.
CS 405G: Introduction to Database Systems
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Systems More SQL Database Design -- More SQL1.
Advanced SQL SMSU Computer Services Short Course.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
SQL: Interactive Queries (1) John Ortiz Lecture 11SQL: Interactive Queries (1)2 Basic Select Statement  Basic form of the select statement: select target-attribute-list.
The Relational Model These slides are based on the slides of your text book.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CSc-340 2b1 Introduction to SQL Chapter 3 [2 of 2] Null Values Aggregate Functions Nested Subqueries Modification of the Database.
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
Database Management COP4540, SCS, FIU SQL (Continued) Querying Multiple Tables.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
Instructor: Jinze Liu Fall Basic Components (2) Relational Database Web-Interface Done before mid-term Must-Have Components (2) Security: access.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
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.
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.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
CS 405G: Introduction to Database Systems Relational Algebra.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Jennifer Widom Relational Databases The Relational Model.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu.
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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
SQL: Interactive Queries (2) Prof. Weining Zhang Cs.utsa.edu.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Chapter 3 Introduction to SQL(3)
SQL The Query Language R & G - Chapter 5
EECS 647: Introduction to Database Systems
CS 405G: Introduction to Database Systems
Lecture#7: Fun with SQL (Part 2)
SQL Structured Query Language 11/9/2018 Introduction to Databases.
CS 405G: Introduction to Database Systems
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
CS 405G: Introduction to Database Systems
Relational Databases The Relational Model.
Relational Databases The Relational Model.
CS 405G: Introduction to Database Systems
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CMPT 354: Database System I
CMPT 354: Database System I
SQL: Structured Query Language
CS 405G: Introduction to Database Systems
CSC 453 Database Systems Lecture
CS 405G: Introduction to Database Systems
SQL: The Query Language (Part III)
Recap – Relational languages
CS 405G: Introduction to Database Systems
Presentation transcript:

CS 405G: Introduction to Database Systems SQL III

2/16/2016Jinze University of Kentucky2 Summary of SQL features covered so far SELECT - FROM - WHERE statements Ordering Set and bag operations Aggregation and grouping Nested queries  Next: NULL ’s, outerjoins, data modification, constraints, …

2/16/2016Jinze University of Kentucky3 Incomplete information Example: Student (SID, name, age, GPA) Value unknown We do not know Nelson’s age Value not applicable Nelson has not taken any classes yet; what is his GPA?

2/16/2016Jinze University of Kentucky4 Solution 1 A dedicated special value for each domain (type) GPA cannot be –1, so use –1 as a special value to indicate a missing or invalid GPA Leads to incorrect answers if not careful SELECT AVG(GPA) FROM Student; Complicates applications SELECT AVG(GPA) FROM Student WHERE GPA <> -1; Remember the Y2K bug? “00” was used as a missing or invalid year value

2/16/2016Jinze University of Kentucky5 Solution 2 A valid-bit for every column Student (SID,name, name_is_valid, age, age_is_valid, GPA, GPA_is_valid) Complicates schema and queries SELECT AVG(GPA) FROM Student WHERE GPA_is_valid;

2/16/2016Jinze University of Kentucky6 Solution 3? Decompose the table; missing row = missing value StudentName (SID, name) StudentAge (SID, age) StudentGPA (SID, GPA) StudentID (SID) Conceptually the cleanest solution Still complicates schema and queries How to get all information about a student in a table? Would natural join work?

2/16/2016Jinze University of Kentucky7 SQL’s solution A special value NULL For every domain Special rules for dealing with NULL ’s Example: Student (SID, name, age, GPA) h 789, “Nelson”, NULL, NULL i

2/16/2016Jinze University of Kentucky8 Three-valued logic TRUE = 1, FALSE = 0, UNKNOWN = 0.5 x AND y = min(x, y) x OR y = max(x, y) NOT x = 1 – x WHERE and HAVING clauses only select rows for output if the condition evaluates to TRUE UNKNOWN is not enough ANDTrueFalseNULL True FalseUNK False NULLUNKFalseUNK AND ORTrueFalseNULL True FalseTRUE False UNK NULLTRUEUNK OR

2/16/2016Jinze University of Kentucky9 Computing with NULL’s (Arithmetic operation) when we operate on a NULL and another value (including another NULL ) using +, –, etc., the result is NULL Aggregate functions ignore NULL, except COUNT(*) (since it counts rows) When we compare a NULL with another value (including another NULL ) using =, >, etc., the result is UNKNOWN

2/16/2016Jinze University of Kentucky10 Unfortunate consequences SELECT AVG(GPA) FROM Student 3.4 SELECT SUM(GPA)/COUNT(*) FROM Student; 2.72 SELECT * FROM Student; SELECT * FROM Student WHERE GPA = GPA Not equivalent sidnameagegpa 1234John Smith Mary Carter Bob Lee22NULL 1204Susan Wong Kevin Kim182.9

2/16/2016Jinze University of Kentucky11 Another problem Example: Who has NULL GPA values? SELECT * FROM Student WHERE GPA = NULL; Does not work; never returns anything (SELECT * FROM Student) EXCEPT ALL (SELECT * FROM Student WHERE GPA = GPA) Works, but ugly Introduced built-in predicates IS NULL and IS NOT NULL SELECT * FROM Student WHERE GPA IS NULL;

2/16/2016Jinze University of Kentucky12 Outerjoin motivation Example: a master class list SELECT c.CID, s.SID FROM Enroll e, Student s, Course c WHERE e.SID = s.SID and c.CID = e.CID; What if a student take no classes For these students, CID column should be NULL What if a course with no student enrolled yet? For these courses, SID should be NULL

2/16/2016Jinze University of Kentucky13 Outerjoin SELECT * FROM R FULL OUTER JOIN S ON p; A full outer join between R and S (denoted R ! S) includes all rows in the result of R ! p S, plus “Dangling” R rows (those that do not join with any S rows) padded with NULL ’s for S’s columns “Dangling” S rows (those that do not join with any R rows) padded with NULL ’s for R’s columns

2/16/2016Jinze University of Kentucky14 Outerjoin (II) SELECT * FROM R LEFT OUTER JOIN S ON p;; SELECT * FROM R RIGHT OUTER JOIN S ON p; A left outer join (R ! S) includes rows in R ! S plus dangling R rows padded with NULL ’s A right outer join (R ! S) includes rows in R ! S plus dangling S rows padded with NULL ’s

2/16/2016Jinze University of Kentucky15 Outerjoin examples Department Employee DidMidDname 41123Research 51234Finance 61312HR EidName 1123John Smith 1234Mary Carter 1311Bob Lee SELECT * FROM Employee LEFT OUTER JOIN Department ON Eid = Mid EidNameDidMidDname 1123John Smith41123Research 1234Mary Carter51234Finance 1311Bob LeeNULL SELECT * FROM Employee RIGHT OUTER JOIN Department ON Eid = Mid EidNameDidMidDname 1123John Smith41123Research 1234Mary Carter51234Finance NULL 61312HR SELECT * FROM Employee FULL OUTER JOIN Department ON Eid = Mid EidNameDidMidDname 1123John Smith41123Research 1234Mary Carter51234Finance 1311Bob LeeNULL 61312HR

2/16/2016Jinze University of Kentucky16 Summary Query SELECT - FROM - WHERE statements Ordering Set and bag operations Aggregation and grouping Table expressions, subqueries NULL Outerjoins

2/16/2016Jinze University of Kentucky17 Summary of SQL features covered so far SELECT - FROM - WHERE statements Set and bag operations Ordering Aggregation and grouping Table expressions, subqueries NULL ’s and outerjoins  Next: data modification statements, constraints