ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.

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.
SQL Review.
ICS 624 Spring 2011 Overview of DB & IR Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/12/20111Lipyeow.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
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.
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.
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 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 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.
ICS 321 Spring 2011 The Database Language SQL (iii) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 3/14/20111Lipyeow.
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 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 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
CS 405G: Introduction to Database Systems
SQL: Queries, Constraints, Triggers
SQL: The Query Language Part 1
SQL: Queries, Programming, Triggers
SQL: Queries, Constraints, Triggers
SQL: Queries, Programming, Triggers
Presentation transcript:

ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow Lim -- University of Hawaii at Manoa

Example Relations Sailors( sid: integer, sname: string, rating: integer, age: real) Boats( bid: integer, bname: string, color: string) Reserves( sid: integer, bid: string, day: date) sidbidday /10/ /12/96 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa2 sidsnameratingage 22Dustin Lubber Rusty R1 S1 bidbnamecolor 101InterlakeBlue 102InterlakeRed 103Clippergreen 104MarineRed B1

Basic SQL Query relation-list A list of relation names (possibly with a range-variable after each name). target-list A list of attributes of relations in relation-list qualification Comparisons (Attr op const or Attr1 op Attr2, where op is one of, ≤, ≥, =, ≠) combined using AND, OR and NOT. DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Default is that duplicates are not eliminated! 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa3 SELECT [ DISTINCT ] target-list FROM relation-list WHERE qualification

Example Q1 Range variables really needed only if the same relation appears twice in the FROM clause. Good style to always use range variables 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa4 SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND bid=103 SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103 Without range variables

Conceptual Evaluation Strategy Semantics of an SQL query defined in terms of the following conceptual evaluation strategy: 1. Compute the cross-product of relation-list. 2. Discard resulting tuples if they fail qualifications. 3. Delete attributes that are not in target-list. 4. If DISTINCT is specified, eliminate duplicate rows. This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers. 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa5

Cross-Product Consider the cross product of S1 with R1 Each row of S1 is paired with each row of R1. Result schema has one field per field of S1 and R1, with field names `inherited’ if possible. – Conflict: Both S1 and R1 have a field called sid. – Rename to sid1 and sid2 sidsnameratingagesidbidday 22Dustin /10/96 22Dustin /12/96 31Lubber /10/96 31Lubber /12/96 58Rusty /10/96 58Rusty /12/96 S1 × R1 sidbidday /10/ /12/96 sidsnameratingage 22Dustin Lubber Rusty R1 S1

Example Q1: conceptual evaluation Conceptual Evaluation Steps: 1. Compute cross-product 2. Discard disqualified tuples 3. Delete unwanted attributes 4. If DISTINCT is specified, eliminate duplicate rows. 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa7 S.sidsnameratingageR.sidbidday 22Dustin /10/96 22Dustin /12/96 31Lubber /10/96 31Lubber /12/96 58Rusty /10/96 58Rusty /12/96 SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND bid=103 S.sidsnameratingageR.sidbidday 58Rusty /12/96 sname Rusty

Q2: Find sailors who’ve reserved at least one boat Would adding DISTINCT to this query make a difference? What is the effect of replacing S.sid by S.sname in the SELECT clause? Would adding DISTINCT to this variant of the query make a difference? 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa8 sidbidday /10/ /12/96 sidsnameratingage 22Dustin Lubber Rusty R1 S1 SELECT S1.sid FROM Sailors S1, Reserves R1 WHERE S1.sid=R1.sid

Q3: Find the colors of boats reserved by Lubber 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa9 sidbidday /10/ /12/96 sidsnameratingage 22Dustin Lubber Rusty R1 S1 SELECT B1.color FROM Sailors S1, Reserves R1, Boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND S1.sname=‘Lubber’ bidbnamecolor 101InterlakeBlue 102InterlakeRed 103Clippergreen 104MarineRed B1

Expressions WHERE-qualification can contain expressions SELECT-list can also contain arithmetic or string expressions over the column names Example: compute a new ``age adjusted’’ rating for each sailor whose rating satisfies a special formula 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa10 SELECT S1.sname, S1.rating * S1.age / 100 AS NewRating FROM Sailors S1 WHERE S1.rating – 5.0 > S1.age / 12.0 sidsnameratingage 22Dustin Lubber Rusty S1

Strings & Pattern Matching String comparisons via the comparisons operators (, =, etc), but take note of collations – i.e. determines the ordering. Lexicographic, languages etc SQL supports pattern matching via the LIKE operator and wildcards – ``%’’ : zero or more arbitrary chars – ``_’’ : any one char 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa11 SELECT S1.sname, S1.rating FROM Sailors S1 WHERE S1.sname LIKE `L_%’ sidsnameratingage 22Dustin Lubber Rusty S1

UNION, INTERSECT & EXCEPT Set-manipulation constructs for result sets of SQL queries that are union-compatible Can simplify some complicated SQL queries Consider Q5: Find the names of sailors who have reserved a red or a green boat 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa12 SELECT S1.sname FROM Sailors S1, Reserves R1, Boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND ( B1.color=`red’ OR B1.color=`green’)

Q6: Find the names of sailors who have reserved both a red and a green boat 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa13 SELECT S1.sname FROM Sailors S1, Reserves R1, Boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND ( B1.color=`red’ OR AND B1.color=`green’) SELECT S1.sname FROM Sailors S1, Reserves R1, Boats B1, Reserves R2, Boats B2 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND S1.sid=R2.sid AND R2.bid=B2.bid AND B1.color=`red’ AND B2.color=`green’

Q6 with INTERSECT : Find the names of sailors who have reserved both a red and a green boat 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa14 SELECT S1.sname FROM Sailors S1, Reserves R1, Boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND B1.color=`red’ INTERSECT SELECT S2.sname FROM Sailors S2, Reserves R2, Boats B2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=`green’

Q6 Nested: Find the names of sailors who have reserved both a red and a green boat 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa15 SELECT S3.sname FROM Sailors S3 WHERE S3.sid IN ( SELECT S1.sid FROM Sailors S1, Reserves R1, Boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND B1.color=`red’ INTERSECT SELECT S2.sid FROM Sailors S2, Reserves R2, Boats B2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=`green’ )

Q5 with UNION : Find the names of sailors who have reserved a red or a green boat 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa16 SELECT S1.sname FROM Sailors S1, Reserves R1, Boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND B1.color=`red’ UNION SELECT S2.sname FROM Sailors S2, Reserves R2, Boats B2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=`green’

Q19: Find the sids of sailors who have reserved red boats but not green boats 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa17 SELECT S1.sid FROM Sailors S1, Reserves R1, Boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND B1.color=`red’ EXCEPT SELECT S2.sid FROM Sailors S2, Reserves R2, Boats B2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=`green’

Summary Basic structure of an SQL query Joins over multiple tables Expressions in SELECT and WHERE clauses String collation and pattern matching Union, intersect, except set-manipulation operators Many ways to write the same queries, many subtleties 9/8/2009Lipyeow Lim -- University of Hawaii at Manoa18