Lecture 9 09/14/15 Instructor: Yu-San Lin yusan@psu.edu - - PowerPoint PPT Presentation

lecture 9 09 14 15
SMART_READER_LITE
LIVE PREVIEW

Lecture 9 09/14/15 Instructor: Yu-San Lin yusan@psu.edu - - PowerPoint PPT Presentation

CMPSC431W: Database Management Systems Lecture 9 09/14/15 Instructor: Yu-San Lin yusan@psu.edu Course Website: hCp://www.cse.psu.edu/~yul189/cmpsc431w Slides based


slide-1
SLIDE 1

CMPSC431W: ¡Database ¡Management ¡Systems ¡

Instructor: ¡Yu-­‑San ¡Lin ¡ ¡ yusan@psu.edu ¡

¡ Course ¡Website: ¡hCp://www.cse.psu.edu/~yul189/cmpsc431w ¡ Slides ¡based ¡on ¡McGraw-­‑Hill ¡& ¡Dr. ¡Wang-­‑Chien ¡Lee ¡

¡ ¡

Lecture ¡9 ¡ 09/14/15 ¡

1 ¡

slide-2
SLIDE 2

Outline ¡

  • Chapter ¡3 ¡

– Review ¡exercise ¡from ¡last ¡Tme ¡ – View ¡ – Quiz ¡3 ¡

  • Chapter ¡4 ¡

– RelaTonal ¡algebra ¡

  • SelecTon ¡& ¡projecTon ¡
  • Set ¡operaTons ¡

2 ¡

slide-3
SLIDE 3

How ¡would ¡you ¡implement ¡this? ¡

3 ¡

Employees ¡

ssn ¡ name ¡ lot ¡

Dependents ¡

pname ¡ age ¡

Policies ¡

policyi d ¡ cost ¡

Purchas er ¡ Benefici ary ¡

CREATE ¡TABLE ¡Policies ¡( ¡ ¡policyid ¡INTEGER, ¡ ¡cost ¡REAL, ¡ ¡ssn ¡CHAR ¡(11) ¡________, ¡ ¡PRIMARY ¡KEY ¡(policyid), ¡ ¡FOREIGN ¡KEY ¡(ssn) ¡ ¡ ¡REFERENCES ¡Employees, ¡ ¡ ¡_________________); ¡ CREATE ¡TABLE ¡Dependents ¡( ¡ ¡pname ¡CHAR(20), ¡ ¡age ¡INTEGER, ¡ ¡policyid ¡INTEGER, ¡ ¡PRIMARY ¡KEY ¡(________________), ¡ ¡FOREIGN ¡KEY ¡(________) ¡ ¡ ¡REFERENCES ¡Policies, ¡ ¡ ¡_________________); ¡

slide-4
SLIDE 4

Views ¡

  • View: ¡a ¡table ¡whose ¡rows ¡are ¡not ¡explicitly ¡stored ¡

in ¡the ¡database ¡but ¡are ¡computed ¡as ¡needed ¡ from ¡a ¡view ¡definiTon ¡

¡ ¡ ¡CREATE ¡VIEW ¡B-­‑Students ¡(name, ¡sid, ¡course) ¡ ¡ ¡AS ¡SELECT ¡S.sname, ¡S.sid, ¡Ecid ¡ ¡ ¡FROM ¡Students ¡S, ¡Enrolled ¡E ¡ ¡ ¡WHERE ¡S.sid ¡= ¡E.studid ¡AND ¡E.grade ¡= ¡‘B’; ¡ ¡

  • Can ¡be ¡used ¡to ¡present ¡necessary ¡informaTon, ¡

while ¡hiding ¡details ¡in ¡underlying ¡relaTon(s). ¡

4 ¡

slide-5
SLIDE 5

CHAPTER ¡4: ¡RELATIONAL ¡ALGEBRA ¡

5 ¡

slide-6
SLIDE 6

Formal ¡RelaTonal ¡Languages ¡

  • Two ¡mathemaTcal ¡Query ¡Language ¡form ¡the ¡

basis ¡for ¡“real” ¡languages ¡(e.g., ¡SQL), ¡and ¡for ¡ implementaTon: ¡

– RelaTonal ¡Algebra: ¡_________, ¡very ¡useful ¡for ¡ represenTng ¡execuTon ¡plans ¡ – RelaTonal ¡Calculus: ¡_________, ¡lets ¡users ¡ describe ¡what ¡they ¡want, ¡rather ¡than ¡how ¡to ¡ compute ¡it ¡

6 ¡

slide-7
SLIDE 7

Preliminaries ¡

  • A ¡query ¡is ¡applied ¡to ¡_____________ ¡
  • The ¡result ¡of ¡a ¡query ¡is ¡a ¡_____________ ¡
  • PosiTonal ¡v.s. ¡name-­‑field ¡notaTon: ¡

– PosiTonal ¡notaTon: ¡easier ¡for ¡formal ¡definiTons ¡ – Named-­‑field ¡notaTon: ¡more ¡readable ¡

7 ¡

slide-8
SLIDE 8

Example ¡

  • Schema ¡example: ¡

Sailors(sid: ¡integer, ¡sname: ¡string, ¡ra)ng: ¡integer, ¡age: ¡real) ¡ Boats(bid: ¡integer, ¡bname: ¡string, ¡color: ¡string) ¡ Reserves(sid: ¡integer, ¡bid: ¡integer, ¡day: ¡date) ¡

8 ¡

sid ¡ sname ¡ ra9ng ¡ age ¡ 22 ¡ DusTn ¡ 7 ¡ 45.0 ¡ 31 ¡ Lubber ¡ 8 ¡ 55.5 ¡ 58 ¡ Rusty ¡ 10 ¡ 35.0 ¡ sid ¡ sname ¡ ra9ng ¡ age ¡ 28 ¡ yuppy ¡ 9 ¡ 35.0 ¡ 31 ¡ Lubber ¡ 8 ¡ 55.5 ¡ 44 ¡ guppy ¡ 5 ¡ 35.0 ¡ 58 ¡ Rusty ¡ 10 ¡ 35.0 ¡ sid ¡ bid ¡ day ¡ 22 ¡ 101 ¡ 10/10/96 ¡ 58 ¡ 103 ¡ 11/12/96 ¡

Instance ¡S1 ¡of ¡Sailors ¡ Instance ¡S2 ¡of ¡Sailors ¡ Instance ¡R1 ¡of ¡Reserves ¡

slide-9
SLIDE 9

RelaTonal ¡Algebra ¡

  • Queries ¡in ¡algebra ¡are ¡composed ¡using ¡a ¡

collecTon ¡of ¡__________ ¡

– Operators ¡can ¡be ¡unary ¡or ¡binary ¡

  • Every ¡operator ¡in ¡the ¡algebra ¡accepts ¡(one ¡or ¡

two) ¡relaTon ¡instances ¡as ¡arguments, ¡and ¡ returns ¡a ¡relaTon ¡instance ¡as ¡the ¡result ¡

9 ¡

slide-10
SLIDE 10

SelecTon ¡

  • SelecTon ¡(σ) ¡selects ¡subset ¡of ¡rows ¡from ¡

relaTon ¡

  • Expression: ¡

σraTng>8(S2) ¡ Return: ¡ ¡ ¡

  • Schema ¡of ¡the ¡result ¡of ¡a ¡selecTon ¡ ¡

= ¡schema ¡of ¡the ¡input ¡relaTon ¡instance ¡

10 ¡

sid ¡ sname ¡ ra9ng ¡ age ¡ 28 ¡ yuppy ¡ 9 ¡ 35.0 ¡ 58 ¡ Rusty ¡ 10 ¡ 35.0 ¡ SelecTon ¡condiTon ¡

slide-11
SLIDE 11

SelecTon ¡(cont.) ¡

  • SelecTon ¡condiTon ¡is ¡a ¡Boolean ¡combinaTon ¡
  • f ¡terms ¡that ¡have ¡the ¡forms: ¡

¡ ¡ ¡ ¡1) ¡a4ribute ¡op ¡constant ¡ ¡ ¡ ¡ ¡2) ¡a4ribute1 ¡op ¡a4ribute2 ¡

* ¡Comparison ¡operators: ¡<, ¡<=, ¡=, ¡≠, ¡>=, ¡> ¡ * ¡Terms ¡are ¡combined ¡with: ¡________ ¡

  • The ¡reference ¡to ¡an ¡aCribute ¡can ¡be ¡by ¡either ¡

– ________ ¡(.i ¡or ¡i) ¡ – ______ ¡(.name ¡or ¡name) ¡

11 ¡

slide-12
SLIDE 12

ProjecTon ¡

  • ProjecTon ¡(π) ¡deletes ¡unwanted ¡columns ¡from ¡

relaTon ¡

  • Example ¡

πsname, ¡raTng(S2) ¡

Return: ¡

12 ¡

sname ¡ ra9ng ¡ yuppy ¡ 9 ¡ Lubber ¡ 8 ¡ guppy ¡ 5 ¡ Rusty ¡ 10 ¡ Fields ¡to ¡be ¡retained ¡

  • The ¡other ¡fields ¡are ¡projected ¡
  • ut ¡
  • The ¡schema ¡of ¡the ¡result ¡of ¡a ¡

projecTon ¡is ¡determined ¡by ¡ the ¡fields ¡that ¡are ¡projected ¡

slide-13
SLIDE 13

ProjecTon ¡(cont.) ¡

  • Duplicates ¡are ¡eliminated ¡

– Example: ¡ πage(S2) ¡ – Return: ¡

  • Follows ¡definiTon ¡of ¡relaTon: ¡set ¡of ¡tuples ¡
  • Real ¡systems ¡oren ¡omit ¡this ¡step ¡

13 ¡

slide-14
SLIDE 14

SelecTon ¡& ¡ProjecTon ¡

  • We ¡can ¡combine ¡σ ¡and ¡π ¡together ¡

– Example: ¡

πsname, ¡raTng(σraTng>8(S2)) ¡

– Return: ¡

  • The ¡order ¡of ¡applying ¡the ¡operators ¡is: ¡

__________ ¡à ¡__________ ¡

14 ¡

slide-15
SLIDE 15

Set ¡OperaTon: ¡Union ¡

  • Union ¡(⋃) ¡

R ¡⋃ ¡S ¡

– Returns ¡a ¡relaTon ¡instance ¡containing ¡all ¡tuples ¡ that ¡occur ¡in ¡either ¡relaTon ¡instance ¡R ¡or ¡relaTon ¡ instance ¡S ¡(or ¡both) ¡ – R ¡and ¡S ¡must ¡be ¡union-­‑compa)ble: ¡

  • Same ¡number ¡of ¡fields ¡
  • Corresponding ¡fields ¡have ¡the ¡same ¡________ ¡

15 ¡

slide-16
SLIDE 16

Set ¡OperaTon: ¡Union ¡(cont.) ¡

  • Example: ¡S1 ¡⋃ ¡S2 ¡ ¡

16 ¡

sid ¡ sname ¡ ra9ng ¡ age ¡ 22 ¡ DusTn ¡ 7 ¡ 45.0 ¡ 31 ¡ Lubber ¡ 8 ¡ 55.5 ¡ 58 ¡ Rusty ¡ 10 ¡ 35.0 ¡ 28 ¡ yuppy ¡ 9 ¡ 35.0 ¡ 44 ¡ guppy ¡ 5 ¡ 35.0 ¡

slide-17
SLIDE 17

Set ¡OperaTon: ¡IntersecTon ¡

  • IntersecTon ¡(⋂): ¡

R ¡⋂ ¡S ¡

– Returns ¡a ¡relaTon ¡instance ¡containing ¡all ¡tuples ¡ that ¡occur ¡in ¡both ¡R ¡and ¡S ¡ – The ¡relaTons ¡must ¡be ¡union-­‑compaTble ¡too ¡

  • Example: ¡S1 ¡⋂ ¡S2 ¡

17 ¡

sid ¡ sname ¡ ra9ng ¡ age ¡ 31 ¡ Lubber ¡ 8 ¡ 55.5 ¡ 58 ¡ Rusty ¡ 10 ¡ 35.0 ¡

slide-18
SLIDE 18

Set ¡OperaTon: ¡Set-­‑difference ¡

  • Set-­‑difference ¡(−): ¡

R ¡− ¡S ¡

– Returns ¡a ¡relaTon ¡instance ¡containing ¡all ¡tuples ¡ that ¡occur ¡______________ ¡ – The ¡relaTons ¡must ¡be ¡union-­‑compaTble ¡too ¡

  • Example: ¡S1 ¡− ¡S2 ¡

18 ¡

sid ¡ sname ¡ ra9ng ¡ age ¡ 22 ¡ DusTn ¡ 7 ¡ 45.0 ¡

slide-19
SLIDE 19

Set ¡OperaTon: ¡Cross-­‑product ¡

  • Cross-­‑product ¡(×): ¡

R ¡× ¡S ¡

– Returns ¡a ¡relaTon ¡instance ¡whose ¡schema ¡ contains ¡all ¡the ¡fields ¡of ¡R ¡followed ¡by ¡all ¡the ¡fields ¡

  • f ¡S ¡

– Result ¡of ¡R ¡× ¡S: ¡ – Naming ¡conflict ¡might ¡appear ¡

19 ¡

slide-20
SLIDE 20

Set ¡OperaTon: ¡Cross-­‑product ¡(cont.) ¡

  • Example: ¡S1 ¡× ¡R1 ¡ ¡

20 ¡

(sid) ¡ sname ¡ ra9ng ¡ age ¡ (sid) ¡ bid ¡ day ¡ 22 ¡ DusTn ¡ 7 ¡ 45.0 ¡ 22 ¡ 101 ¡ 10/10/96 ¡ 22 ¡ DusTn ¡ 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 ¡

slide-21
SLIDE 21

QuesTon ¡Time ¡

  • Find ¡the ¡sailors’ ¡names ¡who ¡reserved ¡boat ¡

with ¡bid=101 ¡

21 ¡

sid ¡ sname ¡ ra9ng ¡ age ¡ 22 ¡ DusTn ¡ 7 ¡ 45.0 ¡ 31 ¡ Lubber ¡ 8 ¡ 55.5 ¡ 58 ¡ Rusty ¡ 10 ¡ 35.0 ¡ sid ¡ sname ¡ ra9ng ¡ age ¡ 28 ¡ yuppy ¡ 9 ¡ 35.0 ¡ 31 ¡ Lubber ¡ 8 ¡ 55.5 ¡ 44 ¡ guppy ¡ 5 ¡ 35.0 ¡ 58 ¡ Rusty ¡ 10 ¡ 35.0 ¡ sid ¡ bid ¡ day ¡ 22 ¡ 101 ¡ 10/10/96 ¡ 58 ¡ 103 ¡ 11/12/96 ¡

S1 ¡ S2 ¡ R1 ¡

slide-22
SLIDE 22

Don’t ¡Forget ¡

  • Project ¡Phase ¡1 ¡this ¡Friday ¡(9/18) ¡

– Submit ¡presentaTon ¡slides ¡11.59pm ¡on ¡9/17 ¡ – Submit ¡project ¡report ¡in ¡hard ¡copies ¡and ¡upload ¡ electronic ¡version ¡before ¡8am ¡on ¡9/18 ¡ – Each ¡team ¡presents ¡4 ¡minutes ¡

  • Reading: ¡4.1 ¡~ ¡4.2 ¡

22 ¡