Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and...

24
1 Database Systems CSE 414 Section 6: Midterm Review CSE 414 ‐ Autumn 2017

Transcript of Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and...

Page 1: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

1

Database SystemsCSE 414

Section 6: Midterm Review

CSE 414 ‐ Autumn 2017

Page 2: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Consider a schema for a picture tagging website:Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 2

Page 3: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the names of all members that were tagged in both 2011 and 2014 sorted in alphabetic order

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 3

Page 4: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the names of all members that were tagged in both 2011 and 2014 sorted in alphabetic order.

Find a partner and try it out!

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 4

Page 5: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the names of all members that were tagged in both 2011 and 2014 sorted in alphabetic order

select M.namefrom Member M, Tagged T, Picture P1, Picture P2 where M.mid = T.midand P1.pid = T.pid and P2.pid = T.pidand P1.year = 2011 and P2.year = 2014 order by M.name;

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 5

Page 6: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

select M.namefrom Member M, Tagged T, Picture P1, Picture P2 where M.mid = T.midand P1.pid = T.pid and P2.pid = T.pidand P1.year = 2011 and P2.year = 2014 order by M.name;

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 6

Page 7: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

select M.namefrom Member M, Tagged T, Picture P1, Picture P2 where M.mid = T.midand P1.pid = T.pid and P2.pid = T.pidand P1.year = 2011 and P2.year = 2014 order by M.name;

T.pid = P1.pid and T.pid = P2.pid => P1.pid = P2.pid

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 7

Page 8: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

select M.namefrom Member M, Tagged T, Picture P1, Picture P2 where M.mid = T.midand P1.pid = T.pid and P2.pid = T.pidand P1.year = 2011 and P2.year = 2014 order by M.name;

T.pid = P1.pid and T.pid = P2.pid => P1.pid = P2.pid => P1.year = P2.year

Since pid is the primary key of Picture

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 8

Page 9: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the names of all members that were tagged in both 2011 and 2014 sorted in alphabetic order

select x.namefrom Member x, Tagged y1, Tagged y2, Picture z1, Picture z2 where x.mid = y1.mid and y1.pid = z1.pid and z1.year = 2011 and x.mid = y2.mid and y2.pid = z2.pid and z2.year = 2014 order by x.name

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 9

Page 10: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 10

Page 11: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Find a partner and try it yourself!

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 11

Page 12: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Q1 = select distinct x.namefrom Member x, Tagged y where x.mid = y.midand not exists

(select * from Picture z where y.pid = z.pidand z.year = 2015);

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 12

Page 13: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Q1 = select distinct x.namefrom Member x, Tagged y where x.mid = y.midand not exists

(select * from Picture z where y.pid = z.pidand z.year = 2015);

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 13

Page 14: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Q2 = select distinct x.namefrom Member x where not exists

(select * from Tagged y, Picture z where x.mid = y.midand y.pid = z.pid and z.year = 2015);

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 14

Page 15: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Q3 = select distinct x.namefrom Member x where not exists

(select * from Tagged y where x.mid = y.midand not exists

(select * from Picture z where y.pid = z.pidand z.year = 2015));

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 15

Page 16: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Q3 = select distinct x.namefrom Member x where not exists

(select * from Tagged y where x.mid = y.midand exists

(select * from Picture z where y.pid = z.pidand z.year = 2015));

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 16

Page 17: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Q4 = select distinct x.namefrom Member x, Tagged y, Picture z where x.mid = y.mid and y.pid = z.pidand z.year = 2015 group by x.namehaving count(z.pid) = 0;

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 17

Page 18: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Q4 = select distinct x.namefrom Member x, Tagged y, Picture z where x.mid = y.mid and y.pid = z.pidand z.year = 2015 group by x.namehaving count(z.pid) = 0;

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 18

Page 19: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Return the name of all users who were never tagged in 2015.

Q4 = select distinct x.namefrom Member x left outer join Tagged y on x.mid = y.midleft outer join Picture z on y.pid = z.pidand z.year = 2015 group by x.namehaving count(z.pid) = 0;

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 19

Page 20: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Write a Relational Algebra Expression (draw a tree) for the following query:

select w.year, max(w.c) as m from

(select x.name, z.year, count(*) as c from Member x, Tagged y, Picture z where x.mid = y.mid and y.pid = z.pid and age < 20 group by x.name, z.year) w

group by w.yearhaving sum(w.c) > 100;

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 20

Page 21: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

select w.year, max(w.c) as m from

(select x.name, z.year, count(*) as c from Member x, Tagged y, Picture z where x.mid = y.midand y.pid = z.pidand age < 20 group by x.name, z.year) w

group by w.yearhaving sum(w.c) > 100;

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 21

Page 22: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Write a query in datalog with negation that returns the mids and names of all members that were tagged only in pictures where Alice was also tagged.

Try it out!

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 22

Page 23: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Write a query in datalog with negation that returns the mids and names of all members that were tagged only in pictures were Alice was also tagged.

Try it out!

Hint:aliceTagged(pid) :-nonAnswer(mid) :-answer(mid,name) :-

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 23

Page 24: Database Systems CSE 414 - courses.cs.washington.edu...and P1.pid = T.pid and P2.pid = T.pid and P1.year = 2011 and P2.year = 2014 order by M.name; T.pid = P1.pid and T.pid = P2.pid

Write a query in datalog with negation that returns the mids and names of all members that were tagged only in pictures were Alice was also tagged.

aliceTagged(pid) :- Member(mid, ’Alice’, -),Tagged(mid, pid) nonAnswer(mid) :- Tagged(mid, pid), not aliceTagged(pid) answer(mid, name) :- Member(mid, name,-), not nonAnswer(mid)

Member(mid, name, age) Picture(pid, year) Tagged(mid, pid)

CSE 414 ‐ Autumn 2017 24