0% found this document useful (0 votes)
22 views

Chapter - 5 Algebra

This document discusses relational algebra and formal query languages. It begins with a brief history of relational databases and introduces fundamental concepts like relations, tuples, and attributes. Examples are provided to illustrate relations and their schemas. The key formal query languages covered are relational algebra, relational tuple calculus, and relational domain calculus. Relational algebra uses set operators like union, difference, intersection and selection to retrieve information from relations. Examples demonstrate how these operators work and their properties.

Uploaded by

swa7xqzz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Chapter - 5 Algebra

This document discusses relational algebra and formal query languages. It begins with a brief history of relational databases and introduces fundamental concepts like relations, tuples, and attributes. Examples are provided to illustrate relations and their schemas. The key formal query languages covered are relational algebra, relational tuple calculus, and relational domain calculus. Relational algebra uses set operators like union, difference, intersection and selection to retrieve information from relations. Examples demonstrate how these operators work and their properties.

Uploaded by

swa7xqzz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Overview
Carnegie Mellon Univ.
School of Computer Science • history
15-415/615 - DB Applications • concepts
• Formal query languages
– relational algebra
C. Faloutsos & A. Pavlo
– rel. tuple calculus
Lecture #4: Relational Algebra – rel. domain calculus

Faloutsos - Pavlo CMU SCS 15-415/615 #2

CMU SCS CMU SCS

History Concepts - reminder


• before: records, pointers, sets etc • Database: a set of relations (= tables)
• introduced by E.F. Codd in 1970 • rows: tuples
• revolutionary! • columns: attributes (or keys)
• first systems: 1977-8 (System R; Ingres) • superkey, candidate key, primary key
• Turing award in 1981

Faloutsos - Pavlo CMU SCS 15-415/615 #3 Faloutsos - Pavlo CMU SCS 15-415/615 #4

1
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Example Example: cont’d


k-th attribute
Database: Database:
(Dk domain)

rel. schema (attr+domains)


tuple

Faloutsos - Pavlo CMU SCS 15-415/615 #5 Faloutsos - Pavlo CMU SCS 15-415/615 #6

CMU SCS CMU SCS

Example: cont’d Example: cont’d


• Di: the domain of the i-th attribute (eg., char(10)

rel. schema (attr+domains) rel. schema (attr+domains)

instance instance

Faloutsos - Pavlo CMU SCS 15-415/615 #7 Faloutsos - Pavlo CMU SCS 15-415/615 #8

2
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Overview Formal query languages


• history • How do we collect information?
• concepts • Eg., find ssn’s of people in 415
• Formal query languages • (recall: everything is a set!)
– relational algebra • One solution: Rel. algebra, ie., set operators
– rel. tuple calculus • Q1: Which ones??
– rel. domain calculus
• Q2: what is a minimal set of operators?

Faloutsos - Pavlo CMU SCS 15-415/615 #9 Faloutsos - Pavlo CMU SCS 15-415/615 #10

CMU SCS CMU SCS

Relational operators Example:


• . • Q: find all students (part or full time)
• . • A: PT-STUDENT union FT-STUDENT
• .
• set union U
• set difference ‘-’

Faloutsos - Pavlo CMU SCS 15-415/615 #11 Faloutsos - Pavlo CMU SCS 15-415/615 #12

3
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Observations: Observations:
• two tables are ‘union compatible’ if they • A: redundant:
have the same attributes (‘domains’) • STUDENT intersection STAFF =
U
• Q: how about intersection

STUDENT STAFF

Faloutsos - Pavlo CMU SCS 15-415/615 #13 Faloutsos - Pavlo CMU SCS 15-415/615 #14

CMU SCS CMU SCS

Observations: Observations:
• A: redundant: • A: redundant:
• STUDENT intersection STAFF = • STUDENT intersection STAFF =
STUDENT - (STUDENT - STAFF)

STUDENT STAFF STUDENT STAFF

Faloutsos - Pavlo CMU SCS 15-415/615 #15 Faloutsos - Pavlo CMU SCS 15-415/615 #16

4
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Observations: Relational operators


• A: redundant: • .
• STUDENT intersection STAFF = • .
STUDENT - (STUDENT - STAFF) • .
• set union U

Double negation: • set difference ‘-’


We’ll see it again, later…

Faloutsos - Pavlo CMU SCS 15-415/615 #17 Faloutsos - Pavlo CMU SCS 15-415/615 #18

CMU SCS CMU SCS

Other operators? Other operators?


• eg, find all students on ‘Main street’ • Notice: selection (and rest of operators)
• A: ‘selection’ expect tables, and produce tables (-> can be
cascaded!!)
• For selection, in general:

Faloutsos - Pavlo CMU SCS 15-415/615 #19 Faloutsos - Pavlo CMU SCS 15-415/615 #20

5
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Selection - examples Relational operators


• Find all ‘Smiths’ on ‘Forbes Ave’ • selection
• .
• .
• set union RUS
‘condition’ can be any boolean combination of ‘=‘,
• set difference R-S
‘>’, ‘>=‘, ...

Faloutsos - Pavlo CMU SCS 15-415/615 #21 Faloutsos - Pavlo CMU SCS 15-415/615 #22

CMU SCS CMU SCS

Relational operators Relational operators


• selection picks rows - how about columns? Cascading: ‘find ssn of students on ‘forbes
• A: ‘projection’ - eg.: ave’

finds all the ‘ssn’ - removing duplicates

Faloutsos - Pavlo CMU SCS 15-415/615 #23 Faloutsos - Pavlo CMU SCS 15-415/615 #24

6
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Relational operators Relational operators


• selection Are we done yet?
• projection Q: Give a query we can not answer yet!
• .
• set union RUS
• set difference R- S

Faloutsos - Pavlo CMU SCS 15-415/615 #25 Faloutsos - Pavlo CMU SCS 15-415/615 #26

CMU SCS CMU SCS

Relational operators Relational operators


A: any query across two or more tables, A: any query across two or more tables,
eg., ‘find names of students in 15-415’ eg., ‘find names of students in 15-415’
Q: what extra operator do we need?? Q: what extra operator do we need??
A: surprisingly, cartesian product is enough!

Faloutsos - Pavlo CMU SCS 15-415/615 #27 Faloutsos - Pavlo CMU SCS 15-415/615 #28

7
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Cartesian product so what?


• eg., dog-breeding: MALE x FEMALE
• gives all possible couples • Eg., how do we find names of students taking
415?

x =

Faloutsos - Pavlo CMU SCS 15-415/615 #29 Faloutsos - Pavlo CMU SCS 15-415/615 #30

CMU SCS CMU SCS

Cartesian product Cartesian product


• A:

Faloutsos - Pavlo CMU SCS 15-415/615 #31 Faloutsos - Pavlo CMU SCS 15-415/615 #32

8
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

FUNDAMENTAL
Relational operators
• selection
• projection
• cartesian product MALE x FEMALE
• set union RUS
• set difference R- S

Faloutsos - Pavlo CMU SCS 15-415/615 #33 Faloutsos - Pavlo CMU SCS 15-415/615 #34

CMU SCS CMU SCS

Relational ops Joins


• Surprisingly, they are enough, to help us • Equijoin:
answer almost any query we want!!
• derived/convenience operators:
– set intersection
– join (theta join, equi-join, natural join)
– ‘rename’ operator
– division

Faloutsos - Pavlo CMU SCS 15-415/615 #35 Faloutsos - Pavlo CMU SCS 15-415/615 #36

9
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Cartesian product Joins


• A: • Equijoin:
• theta-joins:
generalization of equi-join - any condition

Faloutsos - Pavlo CMU SCS 15-415/615 #37 Faloutsos - Pavlo CMU SCS 15-415/615 #38

CMU SCS CMU SCS

Joins Joins
• very popular: natural join: R S • nat. join has 5 attributes
• like equi-join, but it drops duplicate
columns:
STUDENT (ssn, name, address)
TAKES (ssn, cid, grade)

equi-join: 6
Faloutsos - Pavlo CMU SCS 15-415/615 #39 Faloutsos - Pavlo CMU SCS 15-415/615 #40

10
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Natural Joins - nit-picking Overview - rel. algebra


• if no attributes in common between R, S: • fundamental operators
nat. join -> cartesian product • derived operators
– joins etc
– rename
– division
• examples

Faloutsos - Pavlo CMU SCS 15-415/615 #41 Faloutsos - Pavlo CMU SCS 15-415/615 #42

CMU SCS CMU SCS

Rename op. Rename op.


• Q: why? • PC (parent-id, child-id)
• A: shorthand; self-joins; …
• for example, find the grand-parents of
‘Tom’, given PC (parent-id, child-id)

Faloutsos - Pavlo CMU SCS 15-415/615 #43 Faloutsos - Pavlo CMU SCS 15-415/615 #44

11
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Rename op. Rename op.


• first, WRONG attempt: • we clearly need two different names for the
same table - hence, the ‘rename’ op.
• (why? how many columns?)
• Second WRONG attempt:

Faloutsos - Pavlo CMU SCS 15-415/615 #45 Faloutsos - Pavlo CMU SCS 15-415/615 #46

CMU SCS CMU SCS

Overview - rel. algebra Division


• fundamental operators • Rarely used, but powerful.
• derived operators • Example: find suspicious suppliers, ie.,
– joins etc suppliers that supplied all the parts in
– rename A_BOMB
– division
• examples

Faloutsos - Pavlo CMU SCS 15-415/615 #47 Faloutsos - Pavlo CMU SCS 15-415/615 #48

12
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Division Division
• Observations: ~reverse of cartesian product
• It can be derived from the 5 fundamental
operators (!!)
• How?

Faloutsos - Pavlo CMU SCS 15-415/615 #49 Faloutsos - Pavlo CMU SCS 15-415/615 #50

CMU SCS CMU SCS

Division Division
• Answer: • Answer:

• Observation: find ‘good’ suppliers, and • Observation: find ‘good’ suppliers, and
subtract! (double negation) subtract! (double negation)
Faloutsos - Pavlo CMU SCS 15-415/615 #51 Faloutsos - Pavlo CMU SCS 15-415/615 #52

13
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Division Division
• Answer: • Answer:

All suppliers
all possible
All bad parts suspicious shipments

Faloutsos - Pavlo CMU SCS 15-415/615 #53 Faloutsos - Pavlo CMU SCS 15-415/615 #54

CMU SCS CMU SCS

Division Division
• Answer: • Answer:

all possible
all suppliers who missed
suspicious shipments
at least one suspicious shipment,
that didn’t happen
i.e.: ‘good’ suppliers
Faloutsos - Pavlo CMU SCS 15-415/615 #55 Faloutsos - Pavlo CMU SCS 15-415/615 #56

14
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Overview - rel. algebra Sample schema


find names of students that take 15-415
• fundamental operators
• derived operators
– joins etc
– rename
– division
• examples

Faloutsos - Pavlo CMU SCS 15-415/615 #57 Faloutsos - Pavlo CMU SCS 15-415/615 #58

CMU SCS CMU SCS

Examples Examples
• find names of students that take 15-415 • find names of students that take 15-415

Faloutsos - Pavlo CMU SCS 15-415/615 #59 Faloutsos - Pavlo CMU SCS 15-415/615 #60

15
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Sample schema Examples


find course names of ‘smith’
• find course names of ‘smith’

Faloutsos - Pavlo CMU SCS 15-415/615 #61 Faloutsos - Pavlo CMU SCS 15-415/615 #62

CMU SCS CMU SCS

Examples Examples
• find ssn of ‘overworked’ students, ie., that • find ssn of ‘overworked’ students, ie., that
take 412, 413, 415 take 412, 413, 415: almost correct answer:

Faloutsos - Pavlo CMU SCS 15-415/615 #63 Faloutsos - Pavlo CMU SCS 15-415/615 #64

16
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS CMU SCS

Examples Examples
• find ssn of ‘overworked’ students, ie., that • find ssn of students that work at least as
take 412, 413, 415 - Correct answer: hard as ssn=123, ie., they take all the
courses of ssn=123, and maybe more

c-name=413

c-name=415

Faloutsos - Pavlo CMU SCS 15-415/615 #65 Faloutsos - Pavlo CMU SCS 15-415/615 #66

CMU SCS CMU SCS

Sample schema Examples


• find ssn of students that work at least as
hard as ssn=123 (ie., they take all the
courses of ssn=123, and maybe more

Faloutsos - Pavlo CMU SCS 15-415/615 #67 Faloutsos - Pavlo CMU SCS 15-415/615 #68

17
Faloutsos - Pavlo CMU SCS 15-415/615

CMU SCS

Conclusions
• Relational model: only tables (‘relations’)
• relational algebra: powerful, minimal: 5
operators can handle almost any query!

Faloutsos - Pavlo CMU SCS 15-415/615 #69

18

You might also like