15
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction to Data Management Lecture #14 (Relational Languages IV) Instructor: Mike Carey [email protected] SQL

The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

*** The “Online” Edition ***

Introduction to Data Management

Lecture #14(Relational Languages IV)

Instructor: Mike Carey [email protected]

SQL

Page 2: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Announcements

v Be sure to watch the “SQL Pre-Lecture”§ Stop this video and watch that first (if you haven’t already)!

v HW#4 is in flight...!§ Due Monday (so you can RelaX this weekend J)§ Now also finished with the relational calculus!§ Our SQL adventure has finally begun...

v HW#5 due out Monday (“Monday mode” for now)§ First of a series of SQL-based HW assignments§ It is critical that you resolve any lingering MySQL issues! (Ask in

discussion, post Q’s on Piazza, do whatever it takes – otherwise you won’t survive…!)

Page 3: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3

On to SQL...!

v relation-list A list of relation names (possibly with a range-variable after each name).

v target-list A list of attributes of relations in relation-listv qualification Comparisons (Attr op const or Attr1 op

Attr2, where op is one of <, <=, =, >, >=, <>) combined using AND, OR and NOT.

v DISTINCT is an optional keyword indicating that the answer should not contain duplicates. Default is that duplicates are not eliminated! (Bags, not sets.)

SELECT [DISTINCT] target-listFROM relation-listWHERE qualification

SQL “SPJ” Query:

Page 4: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Many SQL-Based DBMSsv Commercial RDBMS choices include

§ DB2 (IBM)§ Oracle§ SQL Server (Microsoft)§ Teradata

v Open source RDBMS options include§ MySQL§ PostgreSQL

v And for so-called “Big Data”, we also have§ Apache Hive (on Hadoop) + newer wannabees

Page 5: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5

Example Instances

sid sname rating age22 dustin 7 45.031 lubber 8 55.558 rusty 10 35.0sid sname rating age28 yuppy 9 35.031 lubber 8 55.544 guppy 5 35.058 rusty 10 35.0

sid bid day22 101 10/10/9658 103 11/12/96

R1

S1

S2

v We’ll use these instances of our usual Sailors and Reserves relations in our examples.

Page 6: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

Conceptual Evaluation Strategy

v Semantics of an SQL query defined in terms of the following conceptual evaluation strategy:§ Compute the cross-product of relation-list. (✕)§ Discard resulting tuples if they fail qualifications. (σ)§ Project out attributes that are not in target-list. (π)§ If DISTINCT is specified, eliminate duplicate rows. (δ)

v This strategy is probably the least efficient way to compute a query! An optimizer will find more efficient strategies to compute the same answers.

Page 7: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7

Example of Conceptual EvaluationSELECT S.snameFROM Sailors S, Reserves R ß using S1WHERE S.sid=R.sid AND R.bid=103

(sid) sname rating age (sid) bid day 22 dustin 7 45.0 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 58 rusty 10 35.0 22 101 10/10/96

58 rusty 10 35.0 58 103 11/12/96

Page 8: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

A Note on Range Variables

v Named variables “needed” when the same relation appears twice (or more) in the FROMclause. Previous query can be written lazily:SELECT snameFROM Sailors S, Reserves RWHERE S.sid=R.sid AND bid=103

SELECT snameFROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid

AND bid=103

It’s better style,though, to userange variables

always!OR

Sailors(sid, sname, rating, age)Reserves(sid, bid, day)Boats(bid, bname, color)

Page 9: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9

Find sailors who’ve reserved at least one boat

v Would adding DISTINCT to this query make a difference? (With our example data? And what about other possible data?)

v 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?

SELECT S.sidFROM Sailors S, Reserves RWHERE S.sid=R.sid

Sailors(sid, sname, rating, age)Reserves(sid, bid, day)Boats(bid, bname, color)

Let’s go check it out on MySQL...!

Page 10: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

Expressions and Strings

v Illustrates use of arithmetic expressions and string pattern matching: Find triples (names and ages of sailors plus a field defined by an expression) for sailors whose names begin and end with B and contain at least three characters.

v AS provides a way to (re)name fields in result.v LIKE is used for string matching. `_� stands for any

one character and `%� stands for 0 or more arbitrary characters. (See MySQL docs for more info...)

SELECT S.sname, S.age, S.age/7.0 AS dogyearsFROM Sailors SWHERE S.sname LIKE 'B_%B'

Sailors(sid, sname, rating, age)

Page 11: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11

Example Data in MySQL

SailorsReserves

Boats

Page 12: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12

Find sid�s of sailors who�ve reserved a red or a green boat

v If we replace OR by AND in this first version, what do we get?

v UNION: Can be used to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries).

v Also available: EXCEPT(What would we get if we replaced UNION by EXCEPT?)

[Note: MySQL vs. RelaX – and why?]

SELECT DISTINCT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid

AND (B.color=‘red’ OR B.color=‘green’)

(SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid

AND B.color=‘red’)UNION(SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid

AND B.color=‘green’)

Sailors(sid, sname, rating, age)Reserves(sid, bid, day)Boats(bid, bname, color)

Page 13: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13

{ t(sid) | ∃s ∈ Sailors(t.sid =s.sid ∧∃r ∈ Reserves(r.sid =s.sid ∧∃b ∈ Boats(b.bid =r.bid ∧

(b.color =‘red’∨ b.color =‘green’))))}

SQL vs. TRCFind sid’s of sailors who’ve reserved a red or a green boat

Sailors(sid, sname, rating, age)Reserves(sid, bid, day)Boats(bid, bname, color) SELECT S.sid

FROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid

AND (B.color=�red�OR B.color=�green�)

Page 14: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

Find sid�s of sailors who’ve reserved a red and a green boat

v INTERSECT: Can be used to compute the intersection of any two union-compatible sets of tuples.

v Included in the SQL/92 standard, but not in all systems (incl. MySQL).

v Contrast symmetry of the UNION and INTERSECT queries with how much the other versions differ.

SELECT S.sidFROM Sailors S, Boats B1, Reserves R1,

Boats B2, Reserves R2WHERE S.sid=R1.sid AND R1.bid=B1.bid

AND S.sid=R2.sid AND R2.bid=B2.bidAND (B1.color=�red�AND B2.color=�green�)

Key field!SELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid

AND B.color=�red�INTERSECTSELECT S.sidFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid

AND B.color=�green�

Sailors(sid, sname, rating, age)Reserves(sid, bid, day)Boats(bid, bname, color)

Page 15: The “Online” Edition *** Lecture #14 (Relational Languages IV)€¦ · Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 *** The “Online” Edition *** Introduction

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15

To Be Continued...