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.

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.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
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.
Relational Calculus CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth   We will occasionally use this arrow notation unless there is danger of.
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.
Relational Calculus CS 186, Fall 2005 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion. Ronald Graham.
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.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
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.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
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.
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.
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
Basic SQL Lecture 6 Fall
SQL: Queries, Programming, Triggers
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
SQL: Queries, Programming, Triggers
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
SQL: Queries, Programming, Triggers
Presentation transcript:

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 queries. -Anon (not Forrest Gump)

Administrivia Homework 1 due Thursday, Feb 8 10 p.m. Source code for diskmgr and global are available on class web site Coming up: –Homework 2 handed out Feb 13 –Midterm 1: in class February 22 Questions?

Review Query languages provide 2 key advantages: –Less work for user asking query –More opportunities for optimization Algebra and safe calculus are simple and powerful models for query languages for relational model –Have same expressive power –Algebra is more operational; calculus is more declarative SQL can express every query that is expressible in relational algebra/calculus. (and more)

Review: Where have we been? Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB PracticeTheory Lecture 2 Relational Algebra Relational Model Lecture 5 Lectures 3 &4 Relational Calculus Lecture 6

Where are we going next? Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Practice SQL This week Next week After the midterm

{S1 |  S  Sailors  S.rating > 7   R(R  Reserves  R.bid = 103  R.sid = S.sid)  (S1.sname = S.name  S1.age = S.age  S1.day = R.day)} Review: Relational Calculus Example Find names, ages and reservation dates of sailors rated > 7 who’ve reserved boat #103 S S S R R snameageday S1 rusty /12/96 3 quantifiers, but only 1 is free. The free quantifier defines the shape of the result.

Review: The SQL Query Language The most widely used relational query language. Standardized (although most systems add their own “special sauce” -- including PostgreSQL) We will study SQL92 -- a basic subset

Review: SQL Two sublanguages: –DDL – Data Definition Language Define and modify schema (at all 3 levels) –DML – Data Manipulation Language Queries and IUD (insert update delete) DBMS is responsible for efficient evaluation. –Relational completeness means we can define precise semantics for relational queries. –Optimizer can re-order operations, without affecting query answer. –Choices driven by “cost model”

Review: DDL sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors sidbidday 11029/ /13 Reserves bidbnamecolor 101Ninared 102Pintablue 103Santa Mariared Boats 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 (sid INTEGER, bid INTEGER, day DATE, PRIMARY KEY (sid, bid, day), FOREIGN KEY sid REFERENCES Sailors, FOREIGN KEY bid REFERENCES Boats) NOT NULL,

Integrity Constraints (ICs) A foreign key constraint is an Integrity Constraint: –a condition that must be true for any instance of the database; –Specified when schema is defined. –Checked when relations are modified. Primary/foreign key constraints; but databases support more general constraints as well. –e.g. domain constraints like: Rating must be between 1 and 10 ALTER TABLE SAILORS ADD CONSTRAINT RATING CHECK (RATING >= 1 AND RATING < 10) Or even more complex (and potentially nonsensical): ALTER TABLE SAILORS ADD CONSTRAINT RATING CHECK (RATING*AGE/4 <= SID)

DBMSs have fairly sophisticated support for constraints! Specify them on CREATE or ALTER TABLE statements Column Constraints: expressions for column constraint must produce boolean results and reference the related column’s value only. NOT NULL | NULL | UNIQUE | PRIMARY KEY | CHECK (expression) FOREIGN KEY (column) referenced_table [ ON DELETE action ] [ ON UPDATE action ] } action is one of: NO ACTION, CASCADE, SET NULL, SET DEFAULT

Table Constraints: UNIQUE ( column_name [,... ] ) PRIMARY KEY ( column_name [,... ] ) | CHECK ( expression ) | FOREIGN KEY ( column_name [,... ] ) REFERENCES reftable [ ON DELETE action ] [ ON UPDATE action ] } Here, expressions, keys, etc can include multiple columns DBMSs have fairly sophisticated support for constraints!

Integrity Constraints can help prevent data consistency errors …but they have drawbacks: –Expensive –Can’t always return a meaningful error back to the application. e.g: What if you saw this error when you enrolled in a course online? “A violation of the constraint imposed by a unique index or a unique constraint occurred”. –Can be inconvenient e.g. What if the ‘Sailing Class’ application wants to register new (unrated) sailors with rating 0? So they aren’t widely used –Software developers often prefer to keep the integrity logic in applications instead

Intermission

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: SELECT S.name FROM Students S WHERE S.age=19 We’ll spend a lot of time on this one SELECT PROJECT

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

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

Semantics of an SQL query are defined in terms of the following conceptual evaluation strategy: 1.FROM clause: compute cross-product of all tables 2.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

Query Semantics Example sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors sidbidday 11029/ /13 Reserves bidbnamecolor 101Ninared 102Pintablue 103Santa Mariared Boats SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 X

Step 1: Compute the cross product sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors sidbidday 11029/ /13 Reserves sidsnameratingagesidbidday 1Frodo /12 1Frodo /13 2Bilbo /12 2Bilbo /13 3Sam /12 3Sam /13 SailorsXReserves...

Step 1: How big? sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors sidbidday 11029/ /13 Reserves 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 sidsnameratingagesidbidday 1Frodo /12 1Frodo /13 2Bilbo /12 2Bilbo /13 3Sam /12 3Sam /13 SailorsXReserves SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103

Step 3: Delete unwanted fields sidsnameratingagesidbidday 1Frodo /12 1Frodo /13 2Bilbo /12 2Bilbo /13 3Sam /12 3Sam /13 SailorsXReserves SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103

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 SELECT sname 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 sidbidday 11029/ / / /12 R1R2 sidbidday 11029/ / / /12 Reserves R2 R1 bidday 1039/12 bidday 1039/12

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 bidday 1039/12 bidday 1039/12 What are we computing? Boats reserved on the same day by different sailors

SELECT Clause Expressions 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’ SELECT S1.sname AS name1, S2.sname AS name2 FROM Sailors S1, Sailors S2 WHERE 2*S1.rating = S2.rating - 1 Can use AS to provide column names Can use “*” if you want all columns: SELECT * FROM Sailors x WHERE x.age > 20

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 `_’ stands for any one character and `%’ stands for 0 or more arbitrary characters. SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 FROM Sailors S WHERE S.sname LIKE ‘B_l%o’ “ LIKE” is used for string matching.

SELECT DISTINCT SELECT DISTINCT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors sidbidday 11029/ / /13 Reserves 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 2

SELECT DISTINCT How about: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 4Bilbo532 Sailors sidbidday 11029/ / /13 Reserves sname Frodo Bilbo SELECT DISTINCT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid sname Frodo Bilbo vs: 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 sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors Reserves bidbnamecolor 101Ninared 102Pintablue 103Santa Mariared 105Titanicgreen Boats sidbidday 11029/ / /13 X sid 2 4 SELECT R.sid FROM Boats B,Reserves R WHERE(B.color=‘red’ OR B.color=‘green’) AND R.bid=B.bid

SELECT R.sid FROM Boats B,Reserves R WHERE(B.color=‘red’ AND B.color=‘green’) AND R.bid=B.bid Find sids of sailors who’ve reserved a red and a green boat ANDs and ORs sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Sailors Reserves sidbidday 11019/ / /13 X bidbnamecolor 101Ninared 102Pintablue 103Santa Mariared 105Titanicgreen Boats

SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘red’ AND R.bid=B.bid INTERSECT SELECT R.sid FROM Boats B,Reserves R WHERE B.color = ‘green’ AND R.bid=B.bid Use INTERSECT instead of AND Reserves sidbidday 11019/ / /13 bidbnamecolor 101Ninared 102Pintablue 103Santa Mariared 105Titanicgreen Boats sid  = 1 Exercise: try to rewrite this query using a self join instead of INTERSECT!

Could also use UNION for the OR query Reserves sidbidday 11029/ / /13 bidbnamecolor 101Ninared 102Pintablue 103Santa Mariared 105Titanicgreen Boats sid 2 4  = 2 4 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’ AND R.bid=B.bid

EXCEPT: Set Difference SELECT S.sid FROM Sailors S EXCEPT SELECT S.sid FROM Sailors S, Reserves R WHERE S.sid=R.sid Find sids of sailors who have not reserved a boat sidsnameratingage 1Frodo722 2Bilbo239 3Sam827 Reserves sidbidday 11029/ / /13 Sailors First find the set of sailors who have reserved a boat… and then compare it with the rest of the sailors sid 3