1 Introduction to Database Systems CSE 444 Lecture #1 January 4, 2006.
CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture...
Transcript of CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture...
![Page 1: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/1.jpg)
CSE 444: Database Internals
Lecture 2Review of the Relational Model
1CSE 444 - Spring 2016
![Page 2: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/2.jpg)
Announcements
• Lab 1 part 1 is due on Friday– Lab 1 is due next week on Friday– “git commit –a” and “git push” often!
• HW1 is due on Wednesday, April 6th
• 544M first paper review is due on Monday
CSE 444 - Spring 2016 2
![Page 3: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/3.jpg)
Agenda
• Review Relational Model
• Review Queries (will skip most slides)– Relational Algebra– SQL
• Review translation SQL à RA– Needed for HW1
CSE 444 - Spring 2016 3
![Page 4: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/4.jpg)
Database/Relation/Tuple• A Database is collection of relations
• A Relation R is subset of S1 x S2 x … x Sn– Where Si is the domain of attribute i– n is number of attributes of the relation– A relation is a set of tuples
• A Tuple t is an element of S1 x S2 x … x Sn
Other names: relation = table; tuple = rowCSE 444 - Spring 2016 4
![Page 5: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/5.jpg)
Discussion• Rows in a relation:
– Ordering immaterial (a relation is a set)– All rows are distinct – set semantics– Query answers may have duplicates – bag semantics
• Columns in a tuple:– Ordering is significant– Applications refer to columns by their names
• Domain of each column is a primitive type
Data independence!
CSE 444 - Spring 2016 5
![Page 6: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/6.jpg)
CSE 444 - Spring 2016
Schema
• Relation schema: describes column heads– Relation name– Name of each field (or column, or attribute)– Domain of each field
• Degree (or arity) of relation: # attributes
• Database schema: set of all relation schemas
6
![Page 7: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/7.jpg)
CSE 444 - Spring 2016
Instance
• Relation instance: concrete table content– Set of tuples (also called records) matching the
schema
• Cardinality of relation instance: # tuples
• Database instance: set of all relation instances
7
![Page 8: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/8.jpg)
What is the schema?What is the instance?
CSE 444 - Spring 2016 8
sno sname scity sstate1 s1 city 1 WA2 s2 city 1 WA3 s3 city 2 MA4 s4 city 2 MA
Supplier
![Page 9: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/9.jpg)
What is the schema?What is the instance?
CSE 444 - Spring 2016 9
sno sname scity sstate1 s1 city 1 WA2 s2 city 1 WA3 s3 city 2 MA4 s4 city 2 MA
Relation schemaSupplier(sno: integer, sname: string, scity: string, sstate: string)Supplier
instance
![Page 10: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/10.jpg)
CSE 444 - Spring 2016
Integrity Constraints
• Condition specified on a database schema• Restricts data that can be stored in db instance
• DBMS enforces integrity constraints – Ensures only legal database instances exist
• Simplest form of constraint is domain constraint– Attribute values must come from attribute domain
10
![Page 11: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/11.jpg)
CSE 444 - Spring 2016
Key Constraints• Super Key: “set of attributes that functionally
determines all attributes”
• Key: Minimal super-key; a.k.a. “candidate key”
• Primary key: One minimal key can be selected as primary key
11
![Page 12: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/12.jpg)
CSE 444 - Spring 2016
Foreign Key Constraints• A relation can refer to a tuple in another relation
• Foreign key– Field that refers to tuples in another relation– Typically, this field refers to the primary key of other
relation– Can pick another field as well
12
![Page 13: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/13.jpg)
Key Constraint SQL Examples
CSE 444 - Spring 2016 13
CREATE TABLE Part (pno integer,pname varchar(20),psize integer,pcolor varchar(20),PRIMARY KEY (pno)
);
![Page 14: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/14.jpg)
CSE 444 - Spring 2016
Key Constraint SQL Examples
CREATE TABLE Supply(
sno integer,
pno integer,
qty integer,
price integer
);
14
CREATE TABLE Part (pno integer,pname varchar(20),psize integer,pcolor varchar(20),PRIMARY KEY (pno)
);
![Page 15: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/15.jpg)
CSE 444 - Spring 2016
Key Constraint SQL Examples
CREATE TABLE Supply(
sno integer,
pno integer,
qty integer,
price integer,
PRIMARY KEY (sno,pno));
15
CREATE TABLE Part (pno integer,pname varchar(20),psize integer,pcolor varchar(20),PRIMARY KEY (pno)
);
![Page 16: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/16.jpg)
CSE 444 - Spring 2016
Key Constraint SQL Examples
CREATE TABLE Supply(
sno integer,
pno integer,
qty integer,
price integer,
PRIMARY KEY (sno,pno),FOREIGN KEY (sno) REFERENCES Supplier,FOREIGN KEY (pno) REFERENCES Part
);
16
CREATE TABLE Part (pno integer,pname varchar(20),psize integer,pcolor varchar(20),PRIMARY KEY (pno)
);
![Page 17: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/17.jpg)
CSE 444 - Spring 2016
Key Constraint SQL Examples
CREATE TABLE Supply(
sno integer,
pno integer,
qty integer,
price integer,
PRIMARY KEY (sno,pno),FOREIGN KEY (sno) REFERENCES Supplier
ON DELETE NO ACTION,FOREIGN KEY (pno) REFERENCES Part
ON DELETE CASCADE);
17
CREATE TABLE Part (pno integer,pname varchar(20),psize integer,pcolor varchar(20),PRIMARY KEY (pno)
);
![Page 18: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/18.jpg)
CSE 444 - Spring 2016
General Constraints• Table constraints serve to express complex
constraints over a single table
CREATE TABLE Part (pno integer,pname varchar(20),psize integer,pcolor varchar(20),PRIMARY KEY (pno),CHECK ( psize > 0 )
);
Note: Also possible to create constraints over many tables18
![Page 19: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/19.jpg)
CSE 444 - Spring 2016 19
Relational Query Languages
![Page 20: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/20.jpg)
Relational Query Language
• Set-at-a-time:– Query inputs and outputs are relations
• Two variants of the query language:– Relational algebra: specifies order of operations– Relational calculus / SQL: declarative
CSE 444 - Spring 2016 20
![Page 21: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/21.jpg)
Note
• We will go very quickly in class over the Relational Algebra and SQL
• Please review at home:– Read the slides that we skipped in class– Review material from 344 as needed
CSE 444 - Spring 2016 21
![Page 22: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/22.jpg)
CSE 444 - Spring 2016
Relational Algebra• Queries specified in an operational manner
– A query gives a step-by-step procedure
• Relational operators– Take one or two relation instances as argument– Return one relation instance as result– Easy to compose into relational algebra expressions
22
![Page 23: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/23.jpg)
Five Basic Relational Operators• Selection: 𝜎condition(S)
– Condition is Boolean combination (∧,∨) of atomic predicates (<, <=, =, ≠, >=, >)
• Projection: πlist-of-attributes(S)• Union (∪)• Set difference (–), • Cross-product/cartesian product (⨯),
Join: R ⋈𝛉S = 𝜎𝛉(R⨯S)Other operators: anti-semijoin (read about it!), renaming
CSE 444 - Spring 2016 23
![Page 24: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/24.jpg)
Logical Query Plans
Supplier(sno,sname,scity,sstate)Supply(sno,pno,qty,price)Part(pno,pname,psize,pcolor)
CSE 444 - Spring 2016 24
![Page 25: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/25.jpg)
Logical Query Plans
Supplier Supply
pno=pno
Part
Π sname,scity
σ psize > 10sno=sno
What doesthis querycompute?
Supplier(sno,sname,scity,sstate)Supply(sno,pno,qty,price)Part(pno,pname,psize,pcolor)
CSE 444 - Spring 2016 25
![Page 26: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/26.jpg)
CSE 444 - Spring 2016
Selection & Projection Examples
no name zip disease1 p1 98125 flu2 p2 98125 heart3 p3 98120 lung4 p4 98120 heart
Patient
𝜎disease=‘heart’(Patient)no name zip disease2 p2 98125 heart4 p4 98120 heart
zip disease98125 flu98125 heart98120 lung98120 heart
𝛑zip,disease(Patient)
𝛑zip (𝜎disease=‘heart’(Patient))zip9812098125
26
![Page 27: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/27.jpg)
CSE 444 - Spring 2016
Cross-Product Example
age zip disease54 98125 heart20 98120 flu
AnonPatient P Voters V
P.age P.zip disease54 98125 heart54 98125 heart20 98120 flu20 98120 flu
name V.age V.zipp1 54 98125p2 20 98120p1 54 98125p2 20 98120
P ⨯ V
name age zipp1 54 98125p2 20 98120
27
![Page 28: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/28.jpg)
CSE 444 - Spring 2016
Different Types of Join• Theta-join: R 𝜽S = 𝜎𝜽(R x S)
– Join of R and S with a join condition q– Cross-product followed by selection q
• Equijoin: R 𝜽S = 𝛑A(𝜎𝜽 (R x S))– Join condition 𝜽 consists only of equalities– Projection 𝛑A drops all redundant attributes
• Natural join: R S = 𝛑A (𝜎𝜽(R x S))– Equijoin– Equality on all fields with same name in R and in S
28
![Page 29: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/29.jpg)
CSE 444 - Spring 2016
Theta-Join Example
29
age zip disease50 98125 heart19 98120 flu
AnonPatient P Voters V
P.age P.zip disease19 98120 flu
name V.age V.zipp2 20 98120
P P.zip = V.zip and P.age <= V.age + 1 and P.age >= V.age - 1 V
name age zipp1 54 98125p2 20 98120
![Page 30: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/30.jpg)
CSE 444 - Spring 2016
Equijoin Example
age zip disease54 98125 heart20 98120 flu
AnonPatient P Voters V
P P.age=V.age V
name age zipp1 54 98125p2 20 98120
age P.zip disease name V.zip
54 98125 heart p1 98125
20 98120 flu p2 98120
30
![Page 31: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/31.jpg)
CSE 444 - Spring 2016
Natural Join Example
age zip disease54 98125 heart20 98120 flu
AnonPatient P Voters V
P V
name age zipp1 54 98125p2 20 98120
age zip disease name
54 98125 heart p1
20 98120 flu p2
31
![Page 32: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/32.jpg)
CSE 444 - Spring 2016
More Joins
• Outer join– Include tuples with no matches in the output– Use NULL values for missing attributes
• Variants– Left outer join– Right outer join– Full outer join
32
![Page 33: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/33.jpg)
CSE 444 - Spring 2016
Outer Join Example
age zip disease54 98125 heart20 98120 flu33 98120 lung
AnonPatient P Voters V
P o V
name age zipp1 54 98125p2 20 98120
age zip disease name
54 98125 heart p1
20 98120 flu p2
33 98120 lung null
33
![Page 34: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/34.jpg)
CSE 444 - Spring 2016
Example of Algebra Queries
Q1: Names of patients who have heart disease𝛑name(Voter (𝜎disease=‘heart’ (AnonPatient))
34
![Page 35: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/35.jpg)
CSE 444 - Spring 2016
More Examples
RelationsSupplier(sno,sname,scity,sstate)Part(pno,pname,psize,pcolor)Supply(sno,pno,qty,price)
Q2: Name of supplier of parts with size greater than 10𝛑sname(Supplier Supply (𝜎psize>10 (Part))
Q3: Name of supplier of red parts or parts with size greater than 10𝛑sname(Supplier Supply (𝜎psize>10 (Part) ∪ 𝜎pcolor=‘red’ (Part) ) )
(Many more examples in the book)35
![Page 36: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/36.jpg)
Logical Query Plans
Supplier Supply
pno=pno
Part
Π sname,scity
σ psize > 10sno=sno
Supplier(sno,sname,scity,sstate)Part(pno,pname,psize,pcolor)Supply(sno,pno,qty,price)
CSE 444 - Spring 2016 36
![Page 37: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/37.jpg)
CSE 444 - Spring 2016
Extended Operatorsof Relational Algebra
• Duplicate elimination (𝛿)– Since commercial DBMSs operate on multisets
not sets• Aggregate operators (ɣ)
– Min, max, sum, average, count• Grouping operators (ɣ)
– Partitions tuples of a relation into “groups”– Aggregates can then be applied to groups
• Sort operator (𝜏)
37
![Page 38: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/38.jpg)
CSE 444 - Spring 2016
Structured Query Language: SQL
• Declarative query language, based on the relational calculus (see 344)
• Data definition language– Statements to create, modify tables and views
• Data manipulation language– Statements to issue queries, insert, delete data
38
![Page 39: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/39.jpg)
CSE 444 - Spring 2016
SQL Query
SELECT <attributes>FROM <one or more relations>WHERE <conditions>
Basic form: (plus many many more bells and whistles)
39
![Page 40: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/40.jpg)
Quick Review of SQL
Supplier(sno,sname,scity,sstate)Supply(sno,pno,qty,price)Part(pno,pname,psize,pcolor)
CSE 444 - Spring 2016 40
![Page 41: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/41.jpg)
Quick Review of SQL
What doesthis querycompute?
Supplier(sno,sname,scity,sstate)Supply(sno,pno,qty,price)Part(pno,pname,psize,pcolor)
SELECT DISTINCT z.pno, z.pnameFROM Supplier x, Supply y, Part z WHERE x.sno = y.sno and y.pno = z.pno
and x.scity = ‘Seattle’ and y.price < 100
CSE 444 - Spring 2016 41
![Page 42: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/42.jpg)
Quick Review of SQLWhat aboutthis one?
Supplier(sno,sname,scity,sstate)Supply(sno,pno,qty,price)Part(pno,pname,psize,pcolor)
SELECT z.pname, count(*) as cnt, min(y.price)FROM Supplier x, Supply y, Part z WHERE x.sno = y.sno and y.pno = z.pnoGROUP BY z.pname
CSE 444 - Spring 2016 42
![Page 43: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/43.jpg)
CSE 444 - Spring 2016
Simple SQL QueryPName Price Category ManufacturerGizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorksSingleTouch $149.99 Photography CanonMultiTouch $203.99 Household Hitachi
SELECT *FROM ProductWHERE category=‘Gadgets’
Product
PName Price Category ManufacturerGizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorks“selection”
43
![Page 44: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/44.jpg)
CSE 444 - Spring 2016
Simple SQL QueryPName Price Category ManufacturerGizmo $19.99 Gadgets GizmoWorks
Powergizmo $29.99 Gadgets GizmoWorksSingleTouch $149.99 Photography CanonMultiTouch $203.99 Household Hitachi
SELECT PName, Price, ManufacturerFROM ProductWHERE Price > 100
Product
PName Price ManufacturerSingleTouch $149.99 CanonMultiTouch $203.99 Hitachi
“selection” and“projection”
44
![Page 45: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/45.jpg)
CSE 444 - Spring 2016
Details
• Case insensitive:– Same: SELECT Select select– Same: Product product– Different: ‘Seattle’ ‘seattle’
• Constants:– ‘abc’ - yes– “abc” - no
45
![Page 46: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/46.jpg)
CSE 444 - Spring 2016
Eliminating Duplicates
SELECT DISTINCT categoryFROM Product
Compare to:
SELECT categoryFROM Product
CategoryGadgetsGadgets
PhotographyHousehold
CategoryGadgets
PhotographyHousehold
46
![Page 47: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/47.jpg)
CSE 444 - Spring 2016
Ordering the Results
SELECT pname, price, manufacturerFROM ProductWHERE category=‘gizmo’ AND price > 50ORDER BY price, pname
Ties are broken by the second attribute on the ORDER BY list, etc.
Ordering is ascending, unless you specify the DESC keyword.
47
![Page 48: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/48.jpg)
CSE 444 - Spring 2016
JoinsProduct (pname, price, category, manufacturer)Company (cname, stockPrice, country)
Find all products under $200 manufactured in Japan;return their names and prices.
SELECT PName, PriceFROM Product, CompanyWHERE Manufacturer=CName AND Country=‘Japan’
AND Price <= 200
48
![Page 49: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/49.jpg)
CSE 444 - Spring 2016
Tuple Variables
SELECT DISTINCT pname, addressFROM Person, CompanyWHERE worksfor = cname
Whichaddress ?
Person(pname, address, worksfor)Company(cname, address)
SELECT DISTINCT Person.pname, Company.addressFROM Person, CompanyWHERE Person.worksfor = Company.cname
SELECT DISTINCT x.pname, y.addressFROM Person AS x, Company AS yWHERE x.worksfor = y.cname 49
![Page 50: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/50.jpg)
CSE 444 - Spring 2016
Nested Queries• Nested query
– Query that has another query embedded within it– The embedded query is called a subquery
• Why do we need them?– Enables to refer to a table that must itself be computed
• Subqueries can appear in– WHERE clause (common)– FROM clause (less common)– HAVING clause (less common)
50
![Page 51: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/51.jpg)
CSE 444 - Spring 2016
Subqueries Returning Relations
Return cities where one can find companies that manufacture products bought by Joe Blow
Company(name, city)Product(pname, maker)Purchase(id, product, buyer)
51
SELECT Company.cityFROM CompanyWHERE Company.name IN
(SELECT Product.makerFROM Purchase , ProductWHERE Product.pname=Purchase.product
AND Purchase .buyer = ‘Joe Blow‘);
![Page 52: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/52.jpg)
CSE 444 - Spring 2016
Subqueries Returning Relations
Product ( pname, price, category, maker)Find products that are more expensive than all those producedBy “Gizmo-Works”
You can also use: s > ALL Rs > ANY REXISTS R
52
SELECT nameFROM ProductWHERE price > ALL (SELECT price
FROM PurchaseWHERE maker=‘Gizmo-Works’)
![Page 53: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/53.jpg)
CSE 444 - Spring 2016
Correlated Queries
SELECT DISTINCT titleFROM Movie AS xWHERE year <> ANY
(SELECT yearFROM MovieWHERE title = x.title);
Movie (title, year, director, length)Find movies whose title appears more than once.
Note (1) scope of variables (2) this can still be expressed as single SFW
correlation
53
![Page 54: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/54.jpg)
CSE 444 - Spring 2016
Aggregation
SELECT count(*)FROM ProductWHERE year > 1995
Except count, all aggregations apply to a single attribute
SELECT avg(price)FROM ProductWHERE maker=“Toyota”
SQL supports several aggregation operations:sum, count, min, max, avg
54
![Page 55: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/55.jpg)
CSE 444 - Spring 2016
Grouping and Aggregation
Conceptual evaluation steps:1. Evaluate FROM-WHERE, apply condition C1
2. Group by the attributes a1,…,ak
3. Apply condition C2 to each group (may have aggregates)4. Compute aggregates in S and return the resultRead more about it in the book...
SELECT SFROM R1,…,Rn
WHERE C1GROUP BY a1,…,akHAVING C2
55
![Page 56: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/56.jpg)
CSE 444 - Spring 2016 56
From SQL to RA
![Page 57: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/57.jpg)
57
From SQL to RA
SELECT DISTINCT x.name, z.nameFROM Product x, Purchase y, Customer zWHERE x.pid = y.pid and y.cid = y.cid and
x.price > 100 and z.city = ‘Seattle’
CSE 444 - Spring 2016
Product(pid, name, price)Purchase(pid, cid, store)Customer(cid, name, city)
![Page 58: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/58.jpg)
58
From SQL to RA
Product Purchase
pid=pid
price>100 and city=‘Seattle’
x.name,z.name
δ
cid=cid
Customer
Π
σ
Product(pid, name, price)Purchase(pid, cid, store)Customer(cid, name, city)
CSE 444 - Spring 2016
![Page 59: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/59.jpg)
59
An Equivalent Expression
Product Purchase
pid=pid
city=‘Seattle’
x.name,z.name
δ
cid=cid
Customer
Π
σ price>100
σ
Query optimization = finding cheaper,equivalent expressions
CSE 444 - Spring 2016
![Page 60: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/60.jpg)
Extended RA: Operators on Bags
• Duplicate elimination δ• Grouping γ• Sorting τ
CSE 444 - Spring 2016 60
![Page 61: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/61.jpg)
61
Logical Query Plan
SELECT city, count(*)FROM salesGROUP BY cityHAVING sum(price) > 100
sales(product, city, price)
γ city, sum(price)→p, count(*) → c
σ p > 100
Π city, c
T1(city,p,c)
T2(city,p,c)
T3(city, c)
T1, T2, T3 = temporary tables
CSE 444 - Spring 2016
![Page 62: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/62.jpg)
CSE 444 - Spring 2016
Typical Plan for Block (1/2)
R S
join condition
𝜎 selection condition
𝛑 fields
join condition
…
SELECT-PROJECT-JOINQuery
…
62
![Page 63: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/63.jpg)
CSE 444 - Spring 2016
Typical Plan For Block (2/2)
𝛑fields
Ɣfields, sum/count/min/max(fields)
havingcondition
𝜎selection condition
join condition
… …63
![Page 64: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/64.jpg)
Benefits of Relational Model
• Physical data independence– Can change how data is organized on disk without
affecting applications
• Logical data independence– Can change the logical schema without affecting
applications (not 100%... consider updates)
CSE 444 - Spring 2016 64
![Page 65: CSE 444: Database Internals - courses.cs.washington.edu€¦ · CSE 444: Database Internals Lecture 2 Review of the Relational Model CSE 444 - Spring 2016 1. Announcements • Lab](https://reader034.fdocuments.us/reader034/viewer/2022042400/5f0f31f77e708231d442f531/html5/thumbnails/65.jpg)
Query Evaluation Steps Preview
Parse & Rewrite Query
Select Logical Plan
Select Physical Plan
Query Execution
Disk
SQL query
Queryoptimization
Logicalplan
Physicalplan
65