CS 405G: Introduction to Database Systems

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 1 R &G - Chapter 5 Life is just a bowl of queries. -Anon (not Forrest Gump)
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
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: 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 Review.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
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.
SQL: The Query Language CS 186, Spring 2006, Lectures 11&12 R &G - Chapter 5 Life is just a bowl of queries. -Anon.
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.
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.
CS 370 Database Systems Lecture 12 Introduction to SQL.
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,
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.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2007.
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 CS 186, Spring 2007, Lecture 7 R&G, Chapter 5 Mary Roth   The important thing is not to stop questioning. Albert Einstein Life is just a bowl of.
SQL: The Query Language Part 1 R&G - Chapter 5 1.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
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.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
SQL: The Query Language Part 1 R&G - Chapter 5 Lecture 7 The important thing is not to stop questioning. Albert Einstein.
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: The Query Language
SQL: Queries, Programming, Triggers
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:

CS 405G: Introduction to Database Systems SQL I Instructor: Jinze Liu

DDL Sailors Boats Reserves sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 CREATE TABLE Sailors (sid INTEGER, sname CHAR(20), rating INTEGER, age REAL, PRIMARY KEY sid) CREATE TABLE Boats (bid INTEGER, bname CHAR (20), color CHAR(10) PRIMARY KEY bid) CREATE TABLE Reserves bid INTEGER, day DATE, PRIMARY KEY (sid, bid, day), FOREIGN KEY sid REFERENCES Sailors, FOREIGN KEY bid REFERENCES Boats) NOT NULL, Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria NOT NULL, NOT NULL, Reserves NOT NULL, sid bid day 1 102 9/12 2 9/13 NOT NULL,

SQL DML DML includes 4 main statements: SELECT (query), INSERT, UPDATE and DELETE e.g: To find the names of all 19 year old students: We’ll spend a lot of time on this one PROJECT sid name age gpa 53666 Jones 18 3.4 53688 Smith 3.2 53650 login jones@cs smith@ee smith@math 19 3.8 SELECT S.name FROM Students S WHERE S.age=19 SELECT

Querying Multiple Relations Can specify a join over two tables as follows: SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=‘B' PROJECT SELECT JOIN S.name E.cid Jones History105 result =

Basic SQL Query DISTINCT: optional keyword indicating answer should not contain duplicates. In SQL, default is that duplicates are not eliminated! (Result is called a “multiset”) target-list : A list of attributes of tables in relation-list SELECT [DISTINCT] target-list FROM relation-list WHERE qualification qualification : Comparisons combined using AND, OR and NOT. Comparisons are Attr op const or Attr1 op Attr2, where op is one of ,,,, etc. relation-list : A list of relation names, possibly with a range-variable after each name

Query Semantics Semantics of an SQL query are defined in terms of the following conceptual evaluation strategy: FROM clause: compute cross-product of all tables WHERE clause: Check conditions, discard tuples that fail. (called “selection”). 3. SELECT clause: Delete unwanted fields. (called “projection”). 4. If DISTINCT specified, eliminate duplicate rows. Probably the least efficient way to compute a query! An optimizer will find more efficient strategies to get the same answer.

Query Semantics Example SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria X Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 Reserves sid bid day 1 102 9/12 2 103 9/13

Step 1: Compute the cross product Sailors Reserves sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 sid bid day 1 102 9/12 2 103 9/13 ... SailorsXReserves sid sname rating age bid day 1 Frodo 7 22 102 9/12 2 103 9/13 Bilbo 39 3 Sam 8 27

Step 1: How big? Question: If |S| is cardinality of Sailors, and Reserves sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 sid bid day 1 102 9/12 2 103 9/13 Question: If |S| is cardinality of Sailors, and |R| is cardinality of Reserves, What is the cardinality of Sailors X Reserves? Answer: |S| * |R| |Sailors X Reserves| = 3X2 = 6

Step 2: Check conditions in where clause SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 SailorsXReserves sid sname rating age bid day 1 Frodo 7 22 102 9/12 2 103 9/13 Bilbo 39 3 Sam 8 27

Step 3: Delete unwanted fields SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 SailorsXReserves sid sname rating age bid day 1 Frodo 7 22 102 9/12 2 103 9/13 Bilbo 39 3 Sam 8 27

Range Variables Used for short hand Needed when ambiguity could arise e.g two tables with the same column name: SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND Reserves.bid=103 FROM Sailors S, Reserves R WHERE S.sid=R.sid AND R.bid=103 Question: do range variables remind you of anything? Variables in relational calculus

Sometimes you need a range variable e.g a Self-join: SELECT R1.bid, R1.date FROM Reserves R1, Reserves R2 WHERE R1.bid = R2.bid and R1.date = R2.date and R1.sid != R2.sid bid day 103 9/12 bid day 103 9/12 Reserves Reserves sid bid day 1 102 9/12 3 103 4 9/13 2 sid bid day 1 102 9/12 3 103 4 9/13 2 R1 R2 R1 R2 R2 R2 R1

Sometimes you need a range variable SELECT R1.bid, R1.day FROM Reserves R1, Reserves R2 WHERE R1.bid = R2.bid and R1.day = R2.day and R1.sid != R2.sid bid day 103 9/12 bid day 103 9/12 What are we computing? Boats reserved on the same day by different sailors

SELECT Clause Expressions Can use “*” if you want all columns: SELECT * FROM Sailors x WHERE x.age > 20 Can use arithmetic expressions (add other operations we’ll discuss later) SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 FROM Sailors S WHERE S.sname = ‘Dustin’ Can use AS to provide column names SELECT S1.sname AS name1, S2.sname AS name2 FROM Sailors S1, Sailors S2 WHERE 2*S1.rating = S2.rating - 1

WHERE Clause Expressions Can also have expressions in WHERE clause: SELECT S1.sname AS name1, S2.sname AS name2 FROM Sailors S1, Sailors S2 WHERE 2*S1.rating = S2.rating - 1 “LIKE” is used for string matching. SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 FROM Sailors S WHERE S.sname LIKE ‘B_l%o’ `_’ stands for any one character and `%’ stands for 0 or more arbitrary characters.

Set versus bag semantics No duplicates Relational model and algebra use set semantics Bag Duplicates allowed Number of duplicates is significant SQL uses bag semantics by default

Set versus bag example πSID Enroll Enroll SELECT SID FROM Enroll; sid 1234 1124 1123 Enroll sid cid grade 1234 647 A 108 B 1124 1123 SELECT SID FROM Enroll; sid 1234 1124 1123

A case for bag semantics Efficiency Saves time of eliminating duplicates Which one is more useful? πGPA Student SELECT GPA FROM Student; The first query just returns all possible GPA’s The second query returns the actual GPA distribution Besides, SQL provides the option of set semantics with DISTINCT keyword

SELECT DISTINCT SELECT DISTINCT S.sid FROM Sailors S, Reserves R bid day 1 102 9/12 2 103 9/13 sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 SELECT DISTINCT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid sid 1 2 Find sailors that have reserved at least one boat

SELECT DISTINCT How about: SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid sid 1 2

SELECT DISTINCT How about: SELECT S.sname FROM Sailors S, Reserves R sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 4 5 32 How about: sname Frodo Bilbo SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid Reserves sid bid day 1 102 9/12 2 103 9/13 4 105 vs: SELECT DISTINCT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid sname Frodo Bilbo Do we find all sailors that reserved at least one boat?

Find sids of sailors who’ve reserved a red or a green boat ANDs, ORs, UNIONs and INTERSECTs Find sids of sailors who’ve reserved a red or a green boat Boats SELECT R.sid FROM Boats B,Reserves R WHERE(B.color=‘red’ OR B.color=‘green’) AND R.bid=B.bid bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria 105 Titanic green sid 2 4 Sailors Reserves sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 X sid bid day 1 102 9/12 2 103 9/13 4 105

Find sids of sailors who’ve reserved a red and a green boat ANDs and ORs Find sids of sailors who’ve reserved a red and a green boat Boats SELECT R.sid FROM Boats B,Reserves R WHERE(B.color=‘red’ AND B.color=‘green’) AND R.bid=B.bid X bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria 105 Titanic green Sailors Reserves sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 sid bid day 1 101 9/12 2 103 9/13 105

 = Use INTERSECT instead of AND SELECT R.sid FROM Boats B,Reserves R Exercise: try to rewrite this query using a self join instead of INTERSECT! SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid INTERSECT WHERE B.color = ‘green’ AND R.bid=B.bid Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria 105 Titanic green Reserves sid bid day 1 101 9/12 2 103 9/13 105 sid 1 2 sid 1 sid 1  =

=  Could also use UNION for the OR query SELECT R.sid FROM Boats B, Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid UNION SELECT R.sid FROM Boats B, Reserves R WHERE B.color = ‘green’ Boats bid bname color 101 Nina red 102 Pinta blue 103 Santa Maria 105 Titanic green Reserves sid bid day 1 102 9/12 2 103 9/13 4 105 sid 2 4 sid 2 sid 4 = 

EXCEPT: Set Difference Sailors sid sname rating age 1 Frodo 7 22 2 Bilbo 39 3 Sam 8 27 Find sids of sailors who have not reserved a boat SELECT S.sid FROM Sailors S EXCEPT FROM Sailors S, Reserves R WHERE S.sid=R.sid Reserves sid bid day 1 102 9/12 2 103 9/13 105 First find the set of sailors who have reserved a boat… sid 3 and then compare it with the rest of the sailors