rdbms algebra

49
The Relational Algebra

Upload: sarmad-ali

Post on 15-Jan-2015

257 views

Category:

Education


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: RDBMS Algebra

The Relational Algebra

Page 2: RDBMS Algebra

Mathematical Relations� For two sets D1 and D2, the Cartesian product, D1 X D2 ,

is the set of all ordered pairs in which the first element

is from D1 and the second is from D2

� Example

� D1 = {1,3} and D2 = {a,b,c}

� D1 X D2 = {(1,a), (1,b), (1,c), (3,a), (3,b), (3,c)}

� A relation is any subset of the Cartesian product� A relation is any subset of the Cartesian product

� Example

� R = {(x,y) | x ∈ D1, y ∈ D2, and y = a}

� R = {(1,a), (3,a)}

� Could form Cartesian product of 3 sets; relation is any

subset of the ordered triples so formed

� Could extend to n sets, using n-tuples

Page 3: RDBMS Algebra

Database Relations

• A relation schema, R, is a set of attributes A1, A2,…,An with their domains D1, D2,…Dn

• A relation r on relation schema R is a set of mappings from the attributes to their domains

• r is a set of n-tuples (A1:d1, A2:d2, …, An:dn) such that d1ε D1, d2 εD2 , …, dn εDn

– Mapping the data in domain Dn to the attribute An

• In a table to represent the relation, list the Ai as column headings, and let the (d1, d2, …dn) become the n-tuples, the rows of the table

• The name of the attribute becomes the name of the column

Page 4: RDBMS Algebra

Relational Algebra

• Theoretical language with operators that

apply to one or two relations to produce

another relation

• Both operands and results are tables• Both operands and results are tables

• Can assign name to resulting table (rename)

• SELECT, PROJECT, JOIN allow many data

retrieval operations

Page 5: RDBMS Algebra

SELECT Operation

• Applied to a single table, returns rows that meet a specified

predicate, copying them to new table

• Returns a horizontal subset of original table

SELECT tableName WHERE condition [GIVING newTableName]SELECT tableName WHERE condition [GIVING newTableName]

Symbolically, [newTableName = ] σ predicate (table-name)

• Predicate is called theta-condition, as in σθ(table-name)

• Result table is horizontal subset of operand

• Predicate can have operators <, <=, >, >=, =, <>, ∧(AND),

∨(OR), ¬ (NOT)

Page 6: RDBMS Algebra

SELECT Example

SELECT Student WHERE stuId = ‘S1013’ GIVING Result

Result = σ STDID=‘S1013’ (Student)

Student ResultStudentstuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

ResultstuId lastName firstName major credits

S1013 McCarthy Owen Math 0

Page 7: RDBMS Algebra

SELECT Example

SELECT Class WHERE room = ‘H225’ GIVING Answer

Answer= σ ROOM=‘H225’ (Class)

Class AnswerClassclassNumber facId schedule room

ART103A F101 MWF9 H221

CSC201A F105 TuThF10 M110

CSC203A F105 MThF12 M110

HST205A F115 MWF11 H221

MTH101B F110 MTuTh9 H225

MTH103C F110 MWF11 H225

AnswerclassNumber facId schedule room

MTH101B F110 MTuTh9 H225

MTH103C F110 MWF11 H225

Page 8: RDBMS Algebra

PROJECT Example

PROJECT Student OVER major GIVING Temp

Temp = Π major (Student)

StudentstuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

Tempmajor

History

MathS1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

Math

Art

CSC

Page 9: RDBMS Algebra

PROJECT Example

PROJECT Class OVER (facId, room)

Π facId, room (Class)

ClassclassNumber facId schedule room

ART103A F101 MWF9 H221

CSC201A F105 TuThF10 M110

facId room

F101 H221

F105 M110CSC201A F105 TuThF10 M110

CSC203A F105 MThF12 M110

HST205A F115 MWF11 H221

MTH101B F110 MTuTh9 H225

MTH103C F110 MWF11 H225

F105 M110

F115 H221

F110 H225

Page 10: RDBMS Algebra

PROJECT and SELECT Combination Example

Want the names and student IDs of History majors

SELECT Student WHERE major = ‘History’ GIVING Temp

PROJECT Temp OVER (lastName, firstName, stuId) GIVING Result

StudentstuId lastName firstName major credits

S1001 Smith Tom History 90

TempstuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

S1005 Lee Perry History 3

TempstuId lastName firstName major credits

S1001 Smith Tom History 90

S1005 Lee Perry History 3

ResultlastName firstName stuId

Smith Tom S1001

Lee Perry S1005

Page 11: RDBMS Algebra

Product and Theta-join

• Binary operations – apply to two tables

• Product: Cartesian product – cross-product of

A and B – A TIMES B, written A x B

– all combinations of rows of A with rows of B– all combinations of rows of A with rows of B

– Degree of result is deg of A + deg of B

– Cardinality of result is (card of A) * (card of B)

• THETA join: TIMES followed by SELECT

A |x|θ

B = σθ(A x B)

Page 12: RDBMS Algebra

PRODUCT Example

Student X Enroll (Produces 63 tuples)

StudentstuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

EnrollstuId classNumber grade

S1001 ART103A A

S1001 HST205A C

S1002 ART103A D

S1002 CSC201A F

S1002 MTH103C B

S1010 ART103A

S1010 MTH103C

X

S1020 Rivera Jane CSC 15 S1010 MTH103C

S1020 CSC201A B

S1020 MTH101B A

Student.stuId lastName firstName major credits Enroll.stuId classNumber grade

S1001 Smith Tom History 90 S1001 ART103A A

S1001 Smith Tom History 90 S1001 HST205A C

S1001 Smith Tom History 90 S1002 ART103A D

S1001 Smith Tom History 90 S1002 CSC201A F

S1001 Smith Tom History 90 S1002 MTH103C B

S1001 Smith Tom History 90 S1010 ART103A

S1001 Smith Tom History 90 S1010 MTH103C

S1001 Smith Tom History 90 S1020 CSC201A B

S1001 Smith Tom History 90 S1020 MTH101B A

S1002 Chin Ann Math 36S1001 ART103A A

… … … … …… … …

Page 13: RDBMS Algebra

JOIN Example

Faculty JOIN Class or Faculty |X| Class

FacultyfacId name department rank

F101 Adams Art Professor

F105 Tanaka CSC Instructor

F110 Byrne Math Assistant

F115 Smith History Associate

F221 Smith CSC Professor

ClassclassNumber facId schedule room

ART103A F101 MWF9 H221

CSC201A F105 TuThF10 M110

CSC203A F105 MThF12 M110

HST205A F115 MWF11 H221

MTH101B F110 MTuTh9 H225

|X|

MTH103C F110 MWF11 H225

facId name department rank class No schedule roomF101 Adams Art Professor ART103A MWF9 H221

F105 Tanaka CSC Instructor CSC203A MThF12 M110

F105 Tanaka CSC Instructor CSC201A TuThF10 M110

F110 Byrne Math Assistant MTH103C MWF11 H225

F110 Byrne Math Assistant MTH101B MTuTh9 H225

F115 Smith History Associate HST205A MWF11 H221

Page 14: RDBMS Algebra

NATURAL JOIN Operation

• Requires two tables with common column(s)

(at least with same domain)

• combines the matching rows-rows of the two

tables that have the same values in the tables that have the same values in the

common column(s)

• symbolized by |x| as in

– [newTableName = ] Student |x| Enroll, or

– Student JOIN Enroll [GIVING newTableName]

Page 15: RDBMS Algebra

Equi-join and Natural Join

• EQUIJOIN formed when θ is equality on column common to both tables (or comparable columns)

• The common column appears twice in the • The common column appears twice in the result

• Eliminating the repeated column in the result gives the NATURAL JOIN, usually called just JOIN

A |x| B

Page 16: RDBMS Algebra

NATURAL JOIN ExampleStudent JOIN Enroll or Student |X| Enroll

Equivalent to:

Student TIMES Enroll GIVING Temp3

SELECT Temp3 WHERE Student.stuId = Enroll.stuId GIVING Temp4

PROJECT Temp4 OVER (Student.stuId, lastName, firstName, major, credits, classNumber,

grade)

StudentstuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

EnrollstuId classNumber grade

S1001 ART103A A

S1001 HST205A C

S1002 ART103A D

S1002 CSC201A F

JOINS1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

S1002 CSC201A F

S1002 MTH103C B

S1010 ART103A

S1010 MTH103C

S1020 CSC201A B

S1020 MTH101B A

JOIN

stuId lastName firstName major credits classNumber grade

S1001 Smith Tom History 90 HST205A C

S1001 Smith Tom History 90 ART103A A

S1002 Chin Ann Math 36 MTH103C B

S1002 Chin Ann Math 36 CSC201A F

S1002 Chin Ann Math 36 ART103A D

S1010 Burns Edward Art 63 MTH103C

S1010 Burns Edward Art 63 ART103A

S1020 Rivera Jane CSC 15 MTH101B A

S1020 Rivera Jane CSC 15 CSC201A B

Page 17: RDBMS Algebra

EQUIJOIN ExampleStudent EQUIJOIN Enroll (Produce 9 tuples)

Equivalent to:

Student TIMES Enroll GIVING Temp3

SELECT Temp3 WHERE Student.stuId = Enroll.stuId

StudentstuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

EnrollstuId classNumber grade

S1001 ART103A A

S1001 HST205A C

S1002 ART103A D

S1002 CSC201A F

EQUIJOIN

Student X Enroll

produces 63 tuples

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

S1002 CSC201A F

S1002 MTH103C B

S1010 ART103A

S1010 MTH103C

S1020 CSC201A B

S1020 MTH101B A

EQUIJOIN

stuId lastName firstName major credits Enroll.stuId classNumber grade

S1001 Smith Tom History 90 S1001 HST205A C

S1001 Smith Tom History 90 S1001 ART103A A

S1002 Chin Ann Math 36 S1002 MTH103C B

S1002 Chin Ann Math 36 S1002 CSC201A F

S1002 Chin Ann Math 36 S1002 ART103A D

S1010 Burns Edward Art 63 S1010 MTH103C

S1010 Burns Edward Art 63 S1010 ART103A

S1020 Rivera Jane CSC 15 S1020 MTH101B A

S1020 Rivera Jane CSC 15 S1020 CSC201A B

Final result

Just 9 tuples

Page 18: RDBMS Algebra

More Complicated Queries

Find classes and grades of student Ann Chin

SELECT Student WHERE lastName=‘Chin’ AND

firstName=‘Ann’ GIVING Temp1

Temp1 JOIN Enroll GIVING Temp2

PROJECT Temp2 OVER (classNo, grade) GIVING Answer

ΠclassNo,grade((σlastName=‘Chen’ ^ firstName=‘Ann’(Student))|X|Enroll)

Page 19: RDBMS Algebra

More Complicated Queries (cont.)SELECT Student WHERE lastName=‘Chin’ AND firstName=‘Ann’ GIVING Temp1

StudentstuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

Temp1stuId lastName firstName major credits

S1002 Chin Ann Math 36

Temp1 JOIN Enroll GIVING Temp2

EnrollstuId classNumber grade

S1001 ART103A A

S1001 HST205A CS1001 HST205A C

S1002 ART103A D

S1002 CSC201A F

S1002 MTH103C B

S1010 ART103A

S1010 MTH103C

S1020 CSC201A B

S1020 MTH101B A

Temp2

stuId lastName firstName major credits classNumber gradeS1002 Chin Ann Math 36 ART103A D

S1002 Chin Ann Math 36 CSC201A F

S1002 Chin Ann Math 36 MTH103C B

PROJECT Temp2 OVER (classNo, grade) GIVING

Answer

Answer

classNumber gradeART103A D

CSC201A F

MTH103C B

Page 20: RDBMS Algebra

More Complicated Queries (cont.)

Since we only need the stuId from Temp1, we can do a

PROJECT on Temp1 before we make the JOIN

ΠclassNo,grade(ΠstuId(σlastName=‘Chen’ ^ firstName=‘Ann’(Student))|X|Enroll)

A third way is:A third way is:

JOIN Student, Enroll GIVING Tempa

SELECT Tempa WHERE lastName=‘Chin’ AND firstName = ‘Ann’

GIVING Tempb

PROJECT Tempb Over (classNo, grade)

But it requires more work by the JOIN (produces 54 tubles), so is

less efficient

Page 21: RDBMS Algebra

Semijoins and Outerjoins

• Left semijoin A|x B is formed by finding

A|x|B and projecting result onto attributes of A

• Result is tuples of A that participate in the join

• Right semijoin A x| B defined similarly; tuples of B that participate in join

• Outerjoin is formed by adding to the join those tuples that • Outerjoin is formed by adding to the join those tuples that have no match, adding null values for the attributes from the other table e.g.

A OUTER- EQUIJOIN B

consists of the equijoin of A and B, supplemented by the unmatched tuples of A with null values for attributes of B and the unmatched tuples of B with null values for attributes of A

• Can also form left outerjoin or right outerjoin

Page 22: RDBMS Algebra

Semijoins ExampleStudent LEFT-SEMIJOIN Enroll or Student |X Enroll

StudentstuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

EnrollstuId classNumber grade

S1001 ART103A A

S1001 HST205A C

S1002 ART103A D

S1002 CSC201A F

S1002 MTH103C B

S1010 ART103A

S1010 MTH103C

S1020 CSC201A B

S1020 MTH101B A

|X

stuId lastName firstName major

S1001 Smith Tom History

S1002 Chin Ann Math

S1010 Burns Edward Art

S1020 Rivera Jane CSC

Page 23: RDBMS Algebra

Outerjoin ExampleStudent OUTER-EQUIJOIN Faculty

Compare Student.lastName with Faculty.name

StudentstuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

FacultyfacId name department rank

F101 Adams Art Professor

F105 Tanaka CSC Instructor

F110 Byrne Math Assistant

F115 Smith History Associate

F221 Smith CSC Professor

OUTERJOIN

S1020 Rivera Jane CSC 15

stuId lastName firstName major credits facId name department rank

S1001 Smith Tom History 90 F221 Smith CSC Professor

S1001 Smith Tom History 90 F115 Smith History Associate

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

F101 Adams Art Professor

F105 Tanaka CSC Instructor

F110 Byrne Math Assistant

Page 24: RDBMS Algebra

Left-Outer-Equijoin Example

Student LEFT-OUTER-EQUIJOIN FacultyStudent

stuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

FacultyfacId name department rank

F101 Adams Art Professor

F105 Tanaka CSC Instructor

F110 Byrne Math Assistant

F115 Smith History Associate

F221 Smith CSC Professor

LEFT-OUTER-EQUIJOIN

stuId lastName firstName major credits facId name department rank

S1001 Smith Tom History 90 F221 Smith CSC Professor

S1001 Smith Tom History 90 F115 Smith History Associate

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

Page 25: RDBMS Algebra

Right-Outer-Equijoin Example

Student RIGHT-OUTER-EQUIJOIN FacultyStudent

stuId lastName firstName major credits

S1001 Smith Tom History 90

S1002 Chin Ann Math 36

S1005 Lee Perry History 3

S1010 Burns Edward Art 63

S1013 McCarthy Owen Math 0

S1015 Jones Mary Math 42

S1020 Rivera Jane CSC 15

FacultyfacId name department rank

F101 Adams Art Professor

F105 Tanaka CSC Instructor

F110 Byrne Math Assistant

F115 Smith History Associate

F221 Smith CSC Professor

LEFT-OUTER-EQUIJOIN

stuId lastName firstName major credits facId name department rank

S1001 Smith Tom History 90 F221 Smith CSC Professor

S1001 Smith Tom History 90 F115 Smith History Associate

F101 Adams Art Professor

F105 Tanaka CSC Instructor

F110 Byrne Math Assistant

Page 26: RDBMS Algebra

Division

• Binary operator where entire structure of one table (divisor) is a portion of structure of the other (dividend)

• Result is projection onto those attributes of • Result is projection onto those attributes of the dividend that are not in the divisor of the tuples of dividend that appear with all the rows of the divisor

• It tells which values of those attributes appear with all the values of the divisor

Page 27: RDBMS Algebra

Divide Example

Club DIVIDED BY Stu or Club ÷÷÷÷ Stu

ClubClubName StuNumb StuLastName

Computing S1001 Smith

Computing S1002 Chin

StuStuNumb StuLastName

S1001 Smith

S1002 Chin

S1005 LeeComputing S1002 Chin

Drama S1001 Smith

Drama S1002 Chin

Drama S1005 Lee

Karate S1001 Smith

Karate S1002 Chin

Karate S1005 LeeClub

DIVIDED

BY StuClubName

Drama

Karate

Page 28: RDBMS Algebra

Set Operations

• Tables must be union compatible – have same

basic structure

– Have the same degree

– Attributes in corresponding position have same – Attributes in corresponding position have same

domain

• Third column in first relation must have same domain

as third column in second relation

Page 29: RDBMS Algebra

Set Operations

• A UNION B: set of tuples in either or both of A

and B, written A ∪ B

• A INTERSECTION B: set of tuples in both A and

B simultaneously, wriSen A ∩ BB simultaneously, wriSen A ∩ B

• Difference or A MINUS B: set of tuples in A but

not in B, written A - B

Page 30: RDBMS Algebra

Set Operations (cont.)

• A UNION B: set of tuples in either or both of A and B,

written A ∪ B

– Remove duplicates

MainFac

FacID name department rank

F101 Adams Art Processor

F105 Tanaka CSC Instructor

F221 Smith CSC Processor

BarnchFac

FacID name department rank

F101 Adams Art Processor

F110 Byre Math Assistant

F115 Smith History Associate

F221 Smith CSC ProcessorF115 Smith History Associate

F221 Smith CSC Processor

MainFac UNION BarnchFac

FacID name department rank

F101 Adams Art Processor

F105 Tanaka CSC Instructor

F110 Byre Math Assistant

F115 Smith History Associate

F221 Smith CSC Processor

Page 31: RDBMS Algebra

Set Operations (cont.)

• A INTERSECTION B: set of tuples in both A and B

simultaneously, wriSen A ∩ B

MainFac

FacID name department rank

F101 Adams Art Processor

F105 Tanaka CSC Instructor

F221 Smith CSC Processor

BranchFac

FacID name department rank

F101 Adams Art Processor

F110 Byre Math Assistant

F115 Smith History Associate

F221 Smith CSC ProcessorF115 Smith History Associate

F221 Smith CSC Processor

MainFac INTERSECTION BranchFac

FacID name department rank

F101 Adams Art Processor

F221 Smith CSC Processor

Page 32: RDBMS Algebra

Set Operations (cont.)• Difference or A MINUS B: set of tuples in A but not in B, written A – B

• MainFac - BranchFac

MainFac

FacID name department rank

F101 Adams Art Processor

F105 Tanaka CSC Instructor

F221 Smith CSC Processor

BranchFac

FacID name department rank

F101 Adams Art Processor

F110 Byre Math Assistant

F115 Smith History Associate

F221 Smith CSC Processor

MainFac MINUS BranchFac

FacID name department rank

F105 Tanaka CSC Instructor

-

F105 Tanaka CSC Instructor

� BranchFac - MainFac

MainFac

FacID name department rank

F101 Adams Art Processor

F105 Tanaka CSC Instructor

F221 Smith CSC Processor

BranchFac

FacID name department rank

F101 Adams Art Processor

F110 Byre Math Assistant

F115 Smith History Associate

F221 Smith CSC Processor

MainFac MINUS BranchFac

FacID name department rank

F110 Byre Math Assistant

F115 Smith History Associate

-

Page 33: RDBMS Algebra

Relational Calculus

• Formal non-procedural language

• Two forms: tuple-oriented and domain-

oriented

• Based on predicate calculus• Based on predicate calculus

Page 34: RDBMS Algebra

Tuple-oriented predicate calculus

• Uses tuple variables, which take tuples of relations as values

• Query has form {S P(S)}

– S is the tuple variable, stands for tuples of relation– S is the tuple variable, stands for tuples of relation

– P(S) is a formula that describes S

– Means “Find the set of all tuples, s, such that P(S) is true when S=s.”

• Limit queries to safe expressions – test only finite number of possibilities

Page 35: RDBMS Algebra

Example Tuple-oriented Relational Calculus

Example 1: Find the names of all faculty members who are Professors in CSC

Department

{F.name | F ∈ Faculty ^ F.department = ‘CSC’ ^ F.rank = ‘Professor’}

Example 2: Find the last names of all students enrolled in CSC201A

{S.lastName | S ∈ Student ^ EXISTS E (E ∈ Enroll ^ E.stuId = S.stuId ^

E.classNo = ‘CSC201A)}E.classNo = ‘CSC201A)}

Example 3: Find the first and last names of all students who are enrolled in at

least one class that meets in room H221.

{S.firstName, S.lastName | S ∈ Student ^ EXISTS E(E ∈ Enroll ^ E.stuId =

S.stuId ^ EXISTS C (C ∈ Class ^ C.classNo = E.classNo ^ C.room = ‘H221’))}

Page 36: RDBMS Algebra

Domain-oriented predicate calculus

• Uses variables that take their values from domains

• Query has form

{<x1,x2,...,xn> P(x1,x2,...,xm )}

– x1,x2,...,xn are domain variables

– P(x1,x2,...,xm) is a predicate– P(x1,x2,...,xm) is a predicate

– n<=m

– Means set of all domain variables x1,x2,...,xn for which predicate P(x1,x2,...,xm) is true

• Predicate must be a formula

• Often test for membership condition, <x,y,z >∈ X

Page 37: RDBMS Algebra

Example Domain-oriented Relational Calculus

• Example 1: Find the name of all faculty members who are professors in

CSC Department

– {LN | ∃ FI, DP, RK (<F1, LN, DP, RK> ∈ Faculty ^ RK = ‘Professor’ ^ DP = ‘CSC’)}

• Example 2: Find the last names of all students enrolled in CSC201A.

– {LN | ∃ SI, FN, MJ, CR (<SI, LN, FN, MJ, CR> ∈ Student ^ ∃ CN, GR (<SI, CN, GR>

∈ Enroll ^ CN = ‘CSC201A’))}

• Example 3: Find the first and last names of all students who are enrolled in

at least one class that meets in room H221.at least one class that meets in room H221.

– {FN, LN | ∃ SI, MJ, CR (<SI, LN, FN, MJ, CR> ∈ Student ^ ∃ CN, GR (<SI, CN, GR>

∈ Enroll ^ ∃ FI, SH, RM (<CN, FI, SH, RM> ∈ Class ^ RM = ‘H221’)))}

SI: stuId, LN: lastName, FN: firstName, MJ: major, CR: credits, CN: calssNo, FI: facId,

SH: schedule, RM: room, DP: department, RK: rank, GR: grade

Page 38: RDBMS Algebra

Views• External models in 3-level architecture are called

external views

• Relational views are slightly different

• Relational view is constructed from existing (base)

tablestables

• View can be a window into a base table (subset)

• View can contain data from more than one table

• View can contain calculated data

• Views hide portions of database from users

• External model may have views and base tables

Page 39: RDBMS Algebra

Views (cont.)• Original table

– Student(stuId, lastName, firstName, ssn, major, credits)

• Organize into two tables

– PersonalStu(stuId, lastName, firstName, ssn)

– AcademicStu(stuId, major, credits)

• Create the original table as a view using a natural join • Create the original table as a view using a natural join

of PersonalStu and AcademicStu

• Restrict updates to views

– View must contain primary key to be updatable

– Views constructed from summary data are not updatable

Page 40: RDBMS Algebra

Mapping ER to Relational Model

• Each strong entity set becomes a table

• Non-composite, single-valued attributes become attributes of table

• Composite attributes: either make the composite a single attribute or use individual attributes for components, ignoring the composite

• Multi-valued attributes: remove them to a new table along with the primary key of the original table; also keep key in original table

• Weak entity sets become tables by adding primary key of owner entity• Weak entity sets become tables by adding primary key of owner entity

• Binary Relationships:– 1:M-place primary key of 1 side in table of M side as foreign key

– 1:1- make sure they are not the same entity. If not, use either key as foreign key in the other table

– M:M-create a relationship table with primary keys of related entities, along with any relationship attributes

• Ternary or higher degree relationships: construct relationship table of keys, along with any relationship attributes

Page 41: RDBMS Algebra

ER Diagram

Example Department

deptCodeoffice

deptName

EmploysChairsHasMajor

Offers

Textbook

isbn

titlestuId

lastName

rankFaculty-

Class-

Textbook

Teaches

publisher

Textbook

author

zip

credit

Student

stuId

major

lastName

firstName

address

number

city

state

street

Faculty

facId

firstName

phone

Page 42: RDBMS Algebra

ER to RelationsEach strong entity set becomes a table

• Table name same as entity name

• Non-composite, single-valued attributes,

represented by single oval, become attributes

of the relationof the relation

• Department(deptName, office, deptCode)

Department

deptCodeoffice

deptName

Page 43: RDBMS Algebra

ER to RelationsComposite attributes

• An attribute is atomic, it cannot be composite

• Two approaches

– Make each ER attribute an attribute in the relation

– Represent the total address with just one attribute in the

relation

• Cannot search of all students in particular zip• Cannot search of all students in particular zip

• Student(stuId, lastName, firstName, major,

credits, number, street, city, state, zip)

• Student(stuId, lastName, firstName, major,

credits, address)credit

Student

stuId

major

lastName

firstName

address

number

city

state

street

zip

Page 44: RDBMS Algebra

ER to RelationsMulti-valued attributes

• Remove them to a new table along with the

primary key of the original table

• Keep key in original table

• Textbook(isbn, title, publisher)

• Author(isbn, lastName, firstName)

isbn

• Student(stuId, lastName, firstName, major1,

major2, credits, number, street, city, state, zip

Author(isbn, lastName, firstName)

Textbook

isbn

author

title

publisher

zip

credit

Student

stuId

major

lastName

firstName

address

number

city

state

street

• Student(stuId, lastName, firstName,

credits, number, street, city, state, zip

Author(isbn, lastName, firstName)

• StuMajor(stuId, major)

Page 45: RDBMS Algebra

ER to RelationsMulti-valued attributes

• If multiple phone numbers for faculty

• First approach, multiple attributes in one relation

Faculty(facId, lastName, firstName, rank, phone, alternatePhone, street, city,

state, zip)

• Second approach, create a PhoneNumber relation

• Can have primary number, alternate number, cell number, spouse number,

home number, etc.

• Faculty(facId, lastName, firstName, rank, street, city, state, zip)

• PhoneNumber(facId, phoneNumber, typePhone)

Page 46: RDBMS Algebra

ER to RelationsWeak entity

• Weak entity sets become tables by adding primary key of owner entity

Faculty

• Faculty(facId, lastName, firstName, rank, phone,

alternatePhone, street, city, state, zip)

Evaluation

IsRated• Evaluation(facId, date, rater, rating)

Page 47: RDBMS Algebra

ER to RelationsBinary Relationships

– 1:M-place primary key of 1 side in table of M side as foreign key– 1:1- make sure they are not the same entity. If not, use either key as foreign key in the other table– M:M-create a relationship table with primary keys of related entities, along with any relationship attributes

Faculty• Faculty(facId, lastName, firstName, rank, phone,

alternatePhone, street, city, state, zip)

Class

Teaches

• Teaches(calssNo, facId)

• Class(classNo, facId, schedule, room)

• Don’t need Teaches relation

1

M

Page 48: RDBMS Algebra

ER to RelationsTernary or higher degree relationships

– Construct relationship table of keys, along with any relationship attributes

Faculty

• Faculty(facId, lastName, firstName, rank,

phone, alternatePhone, street, city, state, zip)

• Faculty-Class-Textbook(classNo, isbn, facId)

• Choose classNo, isbn as primary key, because for

each combination of classNo and isbn there is

Class

Faculty-

Class-

Textbook

each combination of classNo and isbn there is

only one faculty member1

M

Textbook

M

Page 49: RDBMS Algebra

Convert ER to Relational Tables

• Department(deptName, office, deptCode)

• Student(stuId, lastName, firstName, major, credits, number,

street, city, state, zip)

• Student(stuId, lastName, firstName, major, credits, address)

//cannot search of all students in particular zip//cannot search of all students in particular zip

• Textbook(isbn, title, publisher)

• Author(isbn, lastName, firstName)

• Faculty(facId, office, deptCode, cellPhone, homePhone,

officePhone) //may have other phone, eg, spousePhone

• Faculty(facId, office, deptCode)

• PhoneNumber(facId, number, typePhone)