CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.

Slides:



Advertisements
Similar presentations
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
Advertisements

1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CS 166: Database Management Systems
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
SQL (2).
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
CSC056-Z1 – Database Management Systems – Vinnie Costa – Hofstra University1 Database Management Systems Session 5 Instructor: Vinnie Costa
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
SQL: The Query Language Part 1
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 The Oracle Database System Querying the Data Database Course The Hebrew University of Jerusalem.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Advanced SQL. 2 Consider the following relations: –pupil (pupil_name, address, class, birthyear) –subject (subject_name, class, teacher) –grades (pupil_name,
Exercises Find the names of sailors who’ve reserved boat #103 { N |  S  Sailors (S.name = N.name   R  Reserves(S.sid = R.sid  R.bid = 103)) }
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
Chapter 5.  Data Manipulation Language (DML): subset of SQL which allows users to create queries and to insert, delete and modify rows.  Data Definition.
Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Basics --- SQL in 45 Minutes Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
SQL Examples CS3754 Class Note 11 CS3754 Class Note 11, John Shieh,
SQL 2 Introduction Structured Query Language (SQL): the most widely used commercial relational database language Originally.
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
1 Database Systems ( 資料庫系統 ) October 24, 2005 Lecture #5.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
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.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5 Jianping Fan.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
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.
SQL: Queries, Constraints, Triggers
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
SQL The Query Language R & G - Chapter 5
Database Systems October 14, 2009 Lecture #5.
Database Systems (資料庫系統)
Basic SQL Lecture 6 Fall
SQL: Queries, Constraints, Triggers
SQL: Queries, Programming, Triggers
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
SQL: Structured Query Language
SQL: Queries, Programming, Triggers
SQL: Queries, Constraints, Triggers
SQL: Queries, Programming, Triggers
Presentation transcript:

CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian

Tutors Available! M 10-11am T 3-4pm W 12-1pm, 1-2pm R 2-3pm F 10-11am RLC 203

3 Agenda Introduction Basic SQL Query Union, Intersection and Except Nested Queries Aggregate Operations Complex Integrity Constraints in SQL Triggers

Examples Reserves Sailors Boats 4

Union (supported in MySQL) Intersect Except/Minus 5

Find sid’s of sailors who’ve reserved a red or a green boat SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ UNION SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘green’ supported in MySQL

Find sid’s of sailors who’ve reserved a red and a green boat SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘green’

Find sid’s of sailors who’ve reserved a red boat but not a green boat SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ EXCEPT SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘green’

Example SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid=103 Find names of sailors who’ve reserved boat #103: Reserves Sailors

Nested Queries Understand semantics of nested queries ▫For each Sailors tuple, check the qualification by computing the subquery. SELECT sname FROM Sailors WHERE sid IN (SELECT sid FROM Reserves WHERE bid=103) Find names of sailors who’ve reserved boat #103:

Nested Queries Find names of sailors who’ve NOT reserved boat #103: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid<>103 Reserves Sailors Is this correct?

Nested Queries “Find names of sailors whose ids do not appear among the ids that reserved boat #103” is different with “Find names of sailors who have reserved a boat that is not 103” Find names of sailors who’ve NOT reserved boat #103: Reserves Sailors

Nested Queries SELECT sname FROM Sailors WHERE sid NOT IN (SELECT sid FROM Reserves WHERE bid=103) Find names of sailors who’ve NOT reserved boat #103: A very powerful feature of SQL: a WHERE clause can itself contain an SQL query!

14 Q: Find the sids of sailors who have reserved a red boat SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid and B.color=‘red’ Reserves Boats Rewrite the query using IN?

15 Q: Find the names of sailors who have reserved a red boat

16 Q: Find the names of sailors who have not reserved a red boat

Rewriting INTERSECT Queries Using IN Q: Find sid’s of sailors who’ve reserved both a red and a green boat: Reserves Boats

Rewriting EXCEPT Queries Q: Find sid’s of sailors who’ve reserved a red but not a green boat :

19 Find the names of sailors who’ve reserved a red and a green boat Is this query correct? Why SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’

20 Find the names of sailors who’ve reserved a red and a green boat

Set-Comparison Operators Also available: op ANY, op ALL ▫OP is one of, <> Q: Find sailors whose rating is equal to that of some sailor called Horatio: Can you rewrite the query without using set-comparison operators? SELECT * FROM Sailors WHERE rating = ANY ( SELECT rating FROM Sailors WHERE sname=‘Horatio’)

Set-Comparison Operators IN is equivalent to =ANY NOT IN is equivalent to <>ANY SELECT * FROM Sailors WHERE rating <> ANY ( SELECT rating FROM Sailors WHERE sname=‘Horatio’)

23 Set-Comparison Operators Q: Find sailors whose rating is better than every sailor called Horatio. SELECT * FROM Sailors WHERE rating > ALL ( SELECT rating FROM Sailors WHERE sname=‘Horatio’)

24 Set-Comparison Operators Q: Find the sailors with the highest rating.

Summary ANY ALL >,>=,<,<= [ANY/ALL] IN is equivalent to =ANY NOT IN is equivalent to <>ANY 25

Nested Queries with Correlation SELECT S.sname FROM Sailors S WHERE EXISTS ( SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Q: Find names of sailors who’ve reserved boat #103: Reserves Sailors

Nested Queries with Correlation SELECT S.sname FROM Sailors S WHERE EXISTS ( SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Q: Find names of sailors who’ve reserved boat #103: SELECT S.sname FROM Sailors S WHERE S.sid IN ( SELECT R.sid FROM Reserves R WHERE R.bid=103 SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid =R.sid and R.bid=103

Nested Queries with Correlation EXISTS is another set comparison operator, like IN. NOT EXISTS SELECT S.sname FROM Sailors S WHERE NOT EXISTS ( SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Q: Find names of sailors who’ve not reserved boat #103: Reserves Sailors

Division in SQL Find names of sailors that there is no boat that has not been reserved by this sailor Q: Find names of sailors who’ve reserved all boats. Reserves Sailors Boats

Division in SQL SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT bid FROM Boats WHERE bid NOT IN (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid)) For each sailor we check that there is no boat that has not been reserved by this sailor Q: Find names of sailors who’ve reserved all boats.

IN NOT IN EXISTS NOT EXISTS 31

Readings Chapter 5