Top Banner
COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Je Get clickers today!
44

COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Jan 18, 2018

Download

Documents

Mary Wood

Using multiple tables Product p_namepricemanufacturer Gizmo19.99GizmoWorks Powergizmo39.99GizmoWorks Widget19.99WidgetsRUs HyperWidget203.99Hyper c_nameaddresscitystate GizmoWorks123 Gizmo St.HoustonTX WidgetsRUs20 Main St.New YorkNY Hyper1 Mission Dr.San FranciscoCA Company Product (p_name, price, manufacturer)Company (c_name, address, city, state) Later in course: Deciding what fields belong in what tables.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

COMP 430Intro. to Database

SystemsMulti-table SQL

Slides use ideas from Chris Ré and Chris Jermaine.

Get clickers today!

Page 2: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

The need for multiple tablesUsing a single table leads to repeating data• Provides the opportunity for inconsistency• Requires more storage space & I/O time

Productp_name price manufacturer address city state

Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX

Powergizmo 39.99 GizmoWorks 123 Gizmo St. Houston TX

Widget 19.99 WidgetsRUs 20 Main St. New York NY

HyperWidget 203.99 Hyper 1 Mission Dr. San Francisco CA

Product (p_name, price, manufacturer, address, city, state)

Page 3: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Using multiple tablesProduct

p_name price manufacturer

Gizmo 19.99 GizmoWorks

Powergizmo 39.99 GizmoWorks

Widget 19.99 WidgetsRUs

HyperWidget 203.99 Hyper

c_name address city state

GizmoWorks 123 Gizmo St. Houston TX

WidgetsRUs 20 Main St. New York NY

Hyper 1 Mission Dr. San Francisco CA

Company

Product (p_name, price, manufacturer) Company (c_name, address, city, state)

Later in course: Deciding what fields belong in what tables.

Page 4: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Foreign keys & referential integrityProduct

p_name price manufacturer

Gizmo 19.99 GizmoWorks

Powergizmo 39.99 GizmoWorks

Widget 19.99 WidgetsRUs

HyperWidget 203.99 Hyper

c_name address city state

GizmoWorks 123 Gizmo St. Houston TX

WidgetsRUs 20 Main St. New York NY

Hyper 1 Mission Dr. San Francisco CA

Company

Product’s Manufacturer is a foreign key.• Foreign keys always refer to primary keys.• We want to enforce referential integrity – each Manufacturer value is an existing c_name.

Page 5: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Creating a table with a foreign key

CREATE TABLE Product ( p_name VARCHAR(50), price DECIMAL(6,2), manufacturer VARCHAR(50), PRIMARY KEY (p_name), FOREIGN KEY (manufacturer) REFERENCES Company (c_name ));

CREATE TABLE Company ( c_name VARCHAR(50), address VARCHAR(50), city VARCHAR(50), state CHAR(2), PRIMARY KEY (c_name));

Page 6: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Foreign key represents a dependence

Productp_name price manufacturer

Gizmo 19.99 GizmoWorks

… … …

c_name address city state

GizmoWorks 123 Gizmo St. Houston TX

… … … …

Company

CREATE TABLE Product ( …. FOREIGN KEY (manufacturer) REFERENCES Company (c_name));

CREATE TABLE Company (…);

Product conceptually dependent on Company to elaborate details.Product data dependent on Company data being entered.

Product def’n dependent on Company def’n.

Page 7: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Foreign keys vs. pointersForeign keys relate tables, or equivalently, sets of attributes.• Repeats data to connect individual records.• One relationship between tables vs. many pointers between table records.

Avoid data pointers because• Difficult to maintain pointers when data moves, esp. with concurrency.• Want to relate with query results, in addition to tables.

Page 8: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

In what ways do we relate tables?Explore design issues & common patterns later.

Two basic building blocks:• One-to-many relationships

• Many-to-many relationships

Page 9: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

In what ways do we relate tables?Explore design issues & common patterns later.

Two basic building blocks:• One-to-many relationships

• Many-to-many relationships

Page 10: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

One-to-many & many-to-one relationships

Productp_name price manufacturer

Gizmo 19.99 GizmoWorks

Powergizmo 39.99 GizmoWorks

Widget 19.99 WidgetsRUs

HyperWidget 203.99 Hyper

c_name address city state

GizmoWorks 123 Gizmo St. Houston TX

WidgetsRUs 20 Main St. New York NY

Hyper 1 Mission Dr. San Francisco CA

Company

Product (p_name, price, manufacturer) Company (c_name, address, city, state)

Each Company can have many Products.

Each Product is made by exactly one Company.

Page 11: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Many-to-many relationshipsStudent

s_id first_name last_name

S01 John Smith

S02 Mary Wallace

S03 Sue Roper

S04 Mark Jones

crn dept number

01234 COMP 140

15134 COMP 160

46117 ELEC 220

Course

Each Student can be enrolled in many Courses.

Each Course has many Students.

s_id crn grade

S01 01234 A

S01 46117 C

S02 01234 B

Enrollment

Page 12: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

What is Student’s primary key? A. s_idB. s_id, first_name, last_nameC. first_name, Last_nameD. last_name

s_id

s_id, fi

rst_nam

e, las

t_name

first_n

ame, La

st_name

last_nam

e

25% 25%25%25%

Students_id first_name last_name

S01 John Smith

S02 Mary Wallace

S03 Sue Roper

S04 Mark Jones

Response

Counter

Page 13: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

What is Course’s primary key?A. crnB. crn, dept, numberC. dept, numberD. number

25% 25%25%25%

crn dept number

01234 COMP 140

15134 COMP 160

46117 ELEC 220

Course

Response

Counter

Page 14: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

What is Enrollment’s primary key?A. s_idB. s_id, crnC. s_id, crn, gradeD. crnE. grade

s_id

s_id, crn

s_id, crn, grade crn

grade

20% 20%20%20%20%

s_id crn grade

S01 01234 A

S01 46117 C

S02 01234 B

Enrollment

Response

Counter

Page 15: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

What is a foreign key in Student?A. s_idB. first_name, last_nameC. N/A

33%33%33%

Student (s_id, FirstName, last_name)

Enrollment (s_id, crn, grade)

Course (crn, dept, number)

Response

Counter

Page 16: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

What is a foreign key in Course?A. crnB. dept, numberC. N/A

33%33%33%

Student (s_id, first_name, last_name)

Enrollment (s_id, crn, grade)

Course (crn, dept, number)

Response

Counter

Page 17: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

What is a foreign key in Enrollment?A. s_idB. crnC. the pair sid, crn is a single

foreign keyD. s_id and crn are each foreign

keys

25% 25%25%25%

Student (s_id, first_name, last_name)

Enrollment (s_id, crn, grade)

Course (crn, dept, number)

Response

Counter

Page 18: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Creating a table with a foreign keyCREATE TABLE Student ( s_id CHAR(10), first_name VARCHAR(50), last_name VARCHAR(50), PRIMARY KEY (s_id));

CREATE TABLE Enrollment ( s_id CHAR(10), crn CHAR(10), grade CHAR(2), PRIMARY KEY (s_id, crn), FOREIGN KEY (s_id) REFERENCES Student (s_id), FOREIGN KEY (crn) REFERENCES Course (crn));

CREATE TABLE Course ( crn CHAR(10), dept CHAR(4), number CHAR(3), PRIMARY KEY (crn));

Page 19: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Another representation – ER diagramStyle: Crow’s footConceptual level: Physical

Page 20: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

A more abstract ER diagramStyle: ChenConceptual level: Logical

Student CourseTakes NN

s_id

first_name last_name

grade crn

dept number

Student CourseTakes NN

s_id

first_name last_name

grade crn

dept number

Page 21: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Joining tablesProduct

p_name price manufacturer

Gizmo 19.99 GizmoWorks

Powergizmo 39.99 GizmoWorks

Widget 19.99 WidgetsRUs

HyperWidget 203.99 Hyper

c_name address city state

GizmoWorks 123 Gizmo St. Houston TX

WidgetsRUs 20 Main St. New York NY

Hyper 1 Mission Dr. San Francisco CA

Company

SELECT *FROM Product, CompanyWHERE manufacturer = c_name;

p_name price manufacturer address city state

Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX

Powergizmo 39.99 GizmoWorks 123 Gizmo St. Houston TX

Widget 19.99 WidgetsRUs 20 Main St. New York NY

HyperWidget 203.99 Hyper 1 Mission Dr. San Francisco CA

Join condition

Page 22: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Joining tablesProduct

p_name price manufacturer

Gizmo 19.99 GizmoWorks

Powergizmo 39.99 GizmoWorks

Widget 19.99 WidgetsRUs

HyperWidget 203.99 Hyper

c_name address city state

GizmoWorks 123 Gizmo St. Houston TX

WidgetsRUs 20 Main St. New York NY

Hyper 1 Mission Dr. San Francisco CA

Company

p_name price manufacturer address city state

Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX

Powergizmo 39.99 GizmoWorks 123 Gizmo St. Houston TX

Widget 19.99 WidgetsRUs 20 Main St. New York NY

HyperWidget 203.99 Hyper 1 Mission Dr. San Francisco CA

This is simplest & most common kind of join – an inner join. See other kinds later.

Page 23: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Joining tablesProduct

p_name price manufacturer

Gizmo 19.99 GizmoWorks

Powergizmo 39.99 GizmoWorks

Widget 19.99 WidgetsRUs

HyperWidget 203.99 Hyper

c_name address city state

GizmoWorks 123 Gizmo St. Houston TX

WidgetsRUs 20 Main St. New York NY

Hyper 1 Mission Dr. San Francisco CA

Company

p_name price manufacturer address city state

Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX

Powergizmo 39.99 GizmoWorks 123 Gizmo St. Houston TX

Widget 19.99 WidgetsRUs 20 Main St. New York NY

HyperWidget 203.99 Hyper 1 Mission Dr. San Francisco CA

This is simplest & most common kind of join – an inner join. See other kinds later.

Page 24: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Joins – forgetting the join conditionSELECT *FROM Product, Company;

p_name price manufacturer c_name address city state

Gizmo 19.99 GizmoWorks GizmoWorks 123 Gizmo St. Houston TX

Gizmo 19.99 GizmoWorks WidgetsRUs 20 Main St. New York NY

Gizmo 19.99 GizmoWorks Hyper 1 Mission Dr. San Francisco CA

Powergizmo 39.99 GizmoWorks GizmoWorks 123 Gizmo St. Houston TX

Powergizmo 39.99 GizmoWorks WidgetsRUs 20 Main St. New York NY

Powergizmo 39.99 GizmoWorks Hyper 1 Mission Dr. San Francisco CA

Widget 19.99 WidgetsRUs GizmoWorks 123 Gizmo St. Houston TX

… … … … … … …

All combinations of records! – Cross-product of tables.

Page 25: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Selection & ProjectionSELECT p_nameFROM Product, CompanyWHERE manufacturer = c_name AND state = ‘TX’;

p_name price manufacturer address city state

Gizmo 19.99 GizmoWorks 123 Gizmo St. Houston TX

Powergizmo 39.99 GizmoWorks 123 Gizmo St. Houston TXSelection

p_name

Gizmo

PowergizmoProjection

Page 26: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Attribute name conflictsPerson (name, address, works_for)Company (name, address)

SELECT name, addressFROM Person, CompanyWHERE works_for = name;

SELECT Person.name, Person.addressFROM Person, CompanyWHERE Person.works_for = Company.name;

SELECT p.name, p.addressFROM Person p, Company cWHERE p.works_for = c.name;

Page 27: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Semantics, multisets, sets

Page 28: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Semantics – set notationSELECT [DISTINCT] T1.a1, T1.a2, …, Tn.am

FROM T1, T2, …, Tn

WHERE Conditions(T1.a’1, T1.a’2, …, Tn.a’p);

{(T1.a1, T1.a2, …, Tn.am) | Conditions(T1.a’1, T1.a’2, …, Tn.a’p)}

Multisets by default.Sets with DISTINCT.

SELECT [DISTINCT] T1.a1, T1.a2, …, Tn.am

FROM T1, T2, …, Tn

WHERE Conditions(T1.a’1, T1.a’2, …, Tn.a’p);

{(T1.a1, T1.a2, …, Tn.am) | Conditions(T1.a’1, T1.a’2, …, Tn.a’p)}

Multisets by default.Sets with DISTINCT.

Page 29: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Semantics – order of stepsSELECT [DISTINCT] T1.a1, T1.a2, …, Tn.am

FROM T1, T2, …, Tn

WHERE Conditions(T1.a’1, T1.a’2, …, Tn.a’p);Answer = {}for row1 in T1 do for row2 in T2 do … for rown in Tn do if Conditions(row1.a’1, row1.a’2, …, rown.a’p) then Answer = Answer {(row1.a1, row1.a2, …, rown.am)}return Answer

Multiset union by default.Set union with DISTINCT.

1. Cross-product

2. Selection

3. Projection

SELECT [DISTINCT] T1.a1, T1.a2, …, Tn.am

FROM T1, T2, …, Tn

WHERE Conditions(T1.a’1, T1.a’2, …, Tn.a’p);Answer = {}for row1 in T1 do for row2 in T2 do … for rown in Tn do if Conditions(row1.a’1, row1.a’2, …, rown.a’p) then Answer = Answer {(row1.a1, row1.a2, …, rown.am)}return Answer

Multiset union by default.Set union with DISTINCT.

1. Cross-product

2. Selection

3. Projection

Page 30: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Activity: Writing multi-table queries03a-queries.ipynb

Page 31: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

What is result of query?A. R × (S T)B. R S TC. R (S T)D. None of the above

25% 25%25%25%

SELECT DISTINCT R.aFROM R, S, TWHERE R.a = S.a OR R.a = T.a;

S T

R

Schemas: R(a) S(a) T(a)

Response

Counter

Page 32: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

What is result of query if S is empty?A. RB. TC. R TD.

25% 25%25%25%

SELECT DISTINCT R.aFROM R, S, TWHERE R.a = S.a OR R.a = T.a;

S T

R

Schemas: R(a) S(a) T(a)

Response

Counter

Page 33: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Query summary

SELECT DISTINCT R.aFROM R, S, TWHERE R.a = S.a OR R.a = T.a;

Schemas: R(a) S(a) T(a)

• If S = , then • If T = , then • Else R (S T)

Page 34: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Two ways to understand multisetsTuple

(1, a)

(1, a)

(1, b)

(2, c)

(2, c)

(2, c)

(1, d)

(1, d)

Tuple

(1, a) 2

(1, b) 1

(2, c) 3

(1, d) 2

Page 35: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Multiset union

Tuple

(1, a) 2

(1, b) 0

(2, c) 3

(1, d) 0

Tuple

(1, a) 5

(1, b) 1

(2, c) 2

(1, d) 2

Tuple

(1, a) 7

(1, b) 1

(2, c) 5

(1, d) 2

∪ ¿

𝝀 ( 𝒁 )=𝝀 (𝑿 )+𝝀 (𝒀 )

Page 36: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Multiset intersection

Tuple

(1, a) 2

(1, b) 0

(2, c) 3

(1, d) 0

Tuple

(1, a) 5

(1, b) 1

(2, c) 2

(1, d) 2

Tuple

(1, a) 2

(1, b) 0

(2, c) 2

(1, d) 0

∩ ¿

𝝀 ( 𝒁 )=𝑚𝑖𝑛 (𝝀 ( 𝑿 ) ,𝝀 (𝒀 ) )

Page 37: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Multiset difference

Tuple

(1, a) 2

(1, b) 0

(2, c) 3

(1, d) 0

Tuple

(1, a) 5

(1, b) 1

(2, c) 2

(1, d) 2

Tuple

(1, a) 0

(1, b) 0

(2, c) 1

(1, d) 0

− ¿

𝝀 ( 𝒁 )=𝝀 (𝑿 )−𝝀 (𝒀 )

Page 38: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Set & multiset union

SELECT R.aFROM R, SWHERE R.a = S.aUNIONSELECT R.aFROM R, TWHERE R.a = T.a;

SELECT R.aFROM R, SWHERE R.a = S.aUNION ALLSELECT R.aFROM R, TWHERE R.a = T.a;

Page 39: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Set & multiset intersection

SELECT R.aFROM R, SWHERE R.a = S.aINTERSECTIONSELECT R.aFROM R, TWHERE R.a = T.a;

SELECT R.aFROM R, SWHERE R.a = S.aINTERSECTION ALLSELECT R.aFROM R, TWHERE R.a = T.a;

Page 40: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Set & multiset difference

SELECT R.aFROM R, SWHERE R.a = S.aEXCEPTSELECT R.aFROM R, TWHERE R.a = T.a;

SELECT R.aFROM R, SWHERE R.a = S.aEXCEPT ALLSELECT R.aFROM R, TWHERE R.a = T.a;

Page 41: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Activity – Sets & multisets03b-sets-multisets.ipynb

Page 42: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

A final tricky exampleCompany (c_name, hq_loc)

Product (p_name, manufacturer, factory_loc)

Goal: Find HQs of companies that manufacture in both U.S. and China.

Suggested solution:

SELECT hq_locFROM Company, ProductWHERE manufacturer = c_name AND factory_loc = ‘U.S.’INTERSECTSELECT hq_locFROM Company, ProductWHERE manufacturer = c_name AND factory_loc = ‘China’;

Page 43: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

Exercise: Compute results

p_name manufacturer factory_locGizmo GizmoWorks U.S.WhatNot What China

SELECT hq_locFROM Company, ProductWHERE manufacturer = c_name AND factory_loc = ‘U.S.’INTERSECTSELECT hq_locFROM Company, ProductWHERE manufacturer = c_name AND factory_loc = ‘China’;

Product

c_name hq_loc

GizmoWorks U.S.

What U.S.

Company

Page 44: COMP 430 Intro. to Database Systems Multi-table SQL Slides use ideas from Chris Ré and Chris Jermaine. Get clickers today!

One solution: set membership + subquery

SELECT DISTINCT hq_locFROM Company, ProductWHERE manufacturer = c_name AND c_name IN ( SELECT manufacturer FROM Product WHERE factory_loc = ‘U.S.’) AND c_name IN ( SELECT manufacturer FROM Product WHERE factory_loc = ‘China’);

p_name manufacturer factory_loc

Gizmo GizmoWorks U.S.

WhatNot What China

Product

c_name hq_loc

GizmoWorks U.S.

What U.S.

Company

More subqueries later.