SlideShare a Scribd company logo
1 of 49
Download to read offline
The Relational 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 productA 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
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
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
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)
SELECT Example
SELECT Student WHERE stuId = ‘S1013’ GIVING Result
Result = σ STDID=‘S1013’ (Student)
Student ResultStudent
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
Result
stuId lastName firstName major credits
S1013 McCarthy Owen Math 0
SELECT Example
SELECT Class WHERE room = ‘H225’ GIVING Answer
Answer= σ ROOM=‘H225’ (Class)
Class AnswerClass
classNumber 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
Answer
classNumber facId schedule room
MTH101B F110 MTuTh9 H225
MTH103C F110 MWF11 H225
PROJECT Example
PROJECT Student OVER major GIVING Temp
Temp = Π major (Student)
Student
stuId lastName firstName major credits
S1001 Smith Tom History 90
S1002 Chin Ann Math 36
Temp
major
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
PROJECT Example
PROJECT Class OVER (facId, room)
Π facId, room (Class)
Class
classNumber 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
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
Student
stuId lastName firstName major credits
S1001 Smith Tom History 90
Temp
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
S1005 Lee Perry History 3
Temp
stuId lastName firstName major credits
S1001 Smith Tom History 90
S1005 Lee Perry History 3
Result
lastName firstName stuId
Smith Tom S1001
Lee Perry S1005
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)
PRODUCT Example
Student X Enroll (Produces 63 tuples)
Student
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
Enroll
stuId 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 90S1001 ART103A A
S1001 Smith Tom History 90S1001 HST205A C
S1001 Smith Tom History 90S1002 ART103A D
S1001 Smith Tom History 90S1002 CSC201A F
S1001 Smith Tom History 90S1002 MTH103C B
S1001 Smith Tom History 90S1010 ART103A
S1001 Smith Tom History 90S1010 MTH103C
S1001 Smith Tom History 90S1020 CSC201A B
S1001 Smith Tom History 90S1020 MTH101B A
S1002 Chin Ann Math 36S1001 ART103A A
… … … … …… … …
JOIN Example
Faculty JOIN Class or Faculty |X| Class
Faculty
facId name department rank
F101 Adams Art Professor
F105 Tanaka CSC Instructor
F110 Byrne Math Assistant
F115 Smith History Associate
F221 Smith CSC Professor
Class
classNumber 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 room
F101 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
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 thetables 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]
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
NATURAL JOIN Example
Student 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)
Student
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
Enroll
stuId 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 90HST205A C
S1001 Smith Tom History 90ART103A A
S1002 Chin Ann Math 36MTH103C B
S1002 Chin Ann Math 36CSC201A F
S1002 Chin Ann Math 36ART103A D
S1010 Burns Edward Art 63MTH103C
S1010 Burns Edward Art 63ART103A
S1020 Rivera Jane CSC 15MTH101B A
S1020 Rivera Jane CSC 15CSC201A B
EQUIJOIN Example
Student EQUIJOIN Enroll (Produce 9 tuples)
Equivalent to:
Student TIMES Enroll GIVING Temp3
SELECT Temp3 WHERE Student.stuId = Enroll.stuId
Student
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
Enroll
stuId 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 90S1001 HST205A C
S1001 Smith Tom History 90S1001 ART103A A
S1002 Chin Ann Math 36S1002 MTH103C B
S1002 Chin Ann Math 36S1002 CSC201A F
S1002 Chin Ann Math 36S1002 ART103A D
S1010 Burns Edward Art 63S1010 MTH103C
S1010 Burns Edward Art 63S1010 ART103A
S1020 Rivera Jane CSC 15S1020 MTH101B A
S1020 Rivera Jane CSC 15S1020 CSC201A B
Final result
Just 9 tuples
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)
More Complicated Queries (cont.)
SELECT Student WHERE lastName=‘Chin’ AND firstName=‘Ann’ GIVING Temp1
Student
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
Temp1
stuId lastName firstName major credits
S1002 Chin Ann Math 36
Temp1 JOIN Enroll GIVING Temp2
Enroll
stuId 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 grade
S1002 Chin Ann Math 36ART103A D
S1002 Chin Ann Math 36CSC201A F
S1002 Chin Ann Math 36MTH103C B
PROJECT Temp2 OVER (classNo, grade) GIVING
Answer
Answer
classNumber grade
ART103A D
CSC201A F
MTH103C B
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
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
Semijoins Example
Student LEFT-SEMIJOIN Enroll or Student |X Enroll
Student
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
Enroll
stuId 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
Outerjoin Example
Student OUTER-EQUIJOIN Faculty
Compare Student.lastName with Faculty.name
Student
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
Faculty
facId 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
Left-Outer-Equijoin Example
Student LEFT-OUTER-EQUIJOIN Faculty
Student
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
Faculty
facId 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
Right-Outer-Equijoin Example
Student RIGHT-OUTER-EQUIJOIN Faculty
Student
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
Faculty
facId 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
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
Divide Example
Club DIVIDED BY Stu or Club ÷÷÷÷ Stu
Club
ClubName StuNumb StuLastName
Computing S1001 Smith
Computing S1002 Chin
Stu
StuNumb StuLastName
S1001 Smith
S1002 Chin
S1005 Lee
Computing S1002 Chin
Drama S1001 Smith
Drama S1002 Chin
Drama S1005 Lee
Karate S1001 Smith
Karate S1002 Chin
Karate S1005 Lee
Club
DIVIDED
BY Stu
ClubName
Drama
Karate
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
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, wriƩen A ∩ BB simultaneously, wriƩen A ∩ B
• Difference or A MINUS B: set of tuples in A but
not in B, written A - B
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 Processor F115 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
Set Operations (cont.)
• A INTERSECTION B: set of tuples in both A and B
simultaneously, wriƩen 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 Processor F115 Smith History Associate
F221 Smith CSC Processor
MainFac INTERSECTION BranchFac
FacID name department rank
F101 Adams Art Processor
F221 Smith CSC Processor
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
-
Relational Calculus
• Formal non-procedural language
• Two forms: tuple-oriented and domain-
oriented
• Based on predicate calculus• Based on predicate calculus
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
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’))}
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
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
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
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
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
ER Diagram
Example Department
deptCode
office
deptName
EmploysChairsHasMajor
Offers
Textbook
isbn
title
stuId
lastName
rank
Faculty-
Class-
Textbook
Teaches
publisher
Textbook
author
zip
credit
Student
stuId
major
lastName
firstName
address
number
city
state
street
Faculty
facId
firstName
phone
ER to Relations
Each 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
deptCode
office
deptName
ER to Relations
Composite 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
ER to Relations
Multi-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)
ER to Relations
Multi-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)
ER to Relations
Weak 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)
ER to Relations
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
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
ER to Relations
Ternary 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
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)

More Related Content

What's hot

Sequence and series
Sequence and series Sequence and series
Sequence and series Sukhtej Sethi
 
Sequences and Series (Mathematics)
Sequences and Series (Mathematics) Sequences and Series (Mathematics)
Sequences and Series (Mathematics) Dhrumil Maniar
 
Sequences and series
Sequences and seriesSequences and series
Sequences and seriesrey castro
 
CENTRAL TENDENCY AND MEAN AND TYPES IN STATISTICS
CENTRAL TENDENCY AND MEAN AND TYPES  IN STATISTICSCENTRAL TENDENCY AND MEAN AND TYPES  IN STATISTICS
CENTRAL TENDENCY AND MEAN AND TYPES IN STATISTICSBALUPRAKASHVITHANALA
 
21 monotone sequences x
21 monotone sequences x21 monotone sequences x
21 monotone sequences xmath266
 
Arithmetic sequences and series
Arithmetic sequences and seriesArithmetic sequences and series
Arithmetic sequences and seriesJJkedst
 
Infinite series &amp; sequence
Infinite series &amp; sequenceInfinite series &amp; sequence
Infinite series &amp; sequenceVikshit Ganjoo
 
Sequences &amp; series
Sequences &amp; seriesSequences &amp; series
Sequences &amp; seriesThabani Masoka
 
Algebra 2 unit 12.3.12.5
Algebra 2 unit 12.3.12.5Algebra 2 unit 12.3.12.5
Algebra 2 unit 12.3.12.5Mark Ryder
 
Calculas sequence and series
Calculas sequence and seriesCalculas sequence and series
Calculas sequence and seriesVrushang Sangani
 
Analysis sequences and bounded sequences
Analysis sequences and bounded sequencesAnalysis sequences and bounded sequences
Analysis sequences and bounded sequencesSANDEEP VISHANG DAGAR
 
Algebra 2 unit 12.2.12.4
Algebra 2 unit 12.2.12.4Algebra 2 unit 12.2.12.4
Algebra 2 unit 12.2.12.4Mark Ryder
 
Algebra 2 Section 1-5
Algebra 2 Section 1-5Algebra 2 Section 1-5
Algebra 2 Section 1-5Jimbo Lamb
 
Arithmetic Sequence and Arithmetic Series
Arithmetic Sequence and Arithmetic SeriesArithmetic Sequence and Arithmetic Series
Arithmetic Sequence and Arithmetic SeriesJoey Valdriz
 

What's hot (20)

Sequence and series
Sequence and series Sequence and series
Sequence and series
 
Sequences and Series (Mathematics)
Sequences and Series (Mathematics) Sequences and Series (Mathematics)
Sequences and Series (Mathematics)
 
Sequences and series
Sequences and seriesSequences and series
Sequences and series
 
CENTRAL TENDENCY AND MEAN AND TYPES IN STATISTICS
CENTRAL TENDENCY AND MEAN AND TYPES  IN STATISTICSCENTRAL TENDENCY AND MEAN AND TYPES  IN STATISTICS
CENTRAL TENDENCY AND MEAN AND TYPES IN STATISTICS
 
21 monotone sequences x
21 monotone sequences x21 monotone sequences x
21 monotone sequences x
 
Chapter 1 sequences and series
Chapter 1 sequences and seriesChapter 1 sequences and series
Chapter 1 sequences and series
 
Sequence and series
Sequence and seriesSequence and series
Sequence and series
 
Arithmetic sequences and series
Arithmetic sequences and seriesArithmetic sequences and series
Arithmetic sequences and series
 
Advance algebra
Advance algebraAdvance algebra
Advance algebra
 
Infinite series &amp; sequence
Infinite series &amp; sequenceInfinite series &amp; sequence
Infinite series &amp; sequence
 
Plano cartesiano
Plano cartesianoPlano cartesiano
Plano cartesiano
 
Sequences &amp; series
Sequences &amp; seriesSequences &amp; series
Sequences &amp; series
 
Algebra 2 unit 12.3.12.5
Algebra 2 unit 12.3.12.5Algebra 2 unit 12.3.12.5
Algebra 2 unit 12.3.12.5
 
Calculas sequence and series
Calculas sequence and seriesCalculas sequence and series
Calculas sequence and series
 
Analysis sequences and bounded sequences
Analysis sequences and bounded sequencesAnalysis sequences and bounded sequences
Analysis sequences and bounded sequences
 
Slope intercept
Slope interceptSlope intercept
Slope intercept
 
Algebra 2 unit 12.2.12.4
Algebra 2 unit 12.2.12.4Algebra 2 unit 12.2.12.4
Algebra 2 unit 12.2.12.4
 
Sequences and series
Sequences and seriesSequences and series
Sequences and series
 
Algebra 2 Section 1-5
Algebra 2 Section 1-5Algebra 2 Section 1-5
Algebra 2 Section 1-5
 
Arithmetic Sequence and Arithmetic Series
Arithmetic Sequence and Arithmetic SeriesArithmetic Sequence and Arithmetic Series
Arithmetic Sequence and Arithmetic Series
 

Viewers also liked

RDBMS ER2 Relational
RDBMS ER2 RelationalRDBMS ER2 Relational
RDBMS ER2 RelationalSarmad Ali
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionAhmed Raza
 
Artificial Intelligence Course- Introduction
Artificial Intelligence Course- IntroductionArtificial Intelligence Course- Introduction
Artificial Intelligence Course- IntroductionMuhammad Sanaullah
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introductionRana Ehtisham Ul Haq
 
Compiler Construction Course - Introduction
Compiler Construction Course - IntroductionCompiler Construction Course - Introduction
Compiler Construction Course - IntroductionMuhammad Sanaullah
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionSarmad Ali
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture NotesFellowBuddy.com
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction Sarmad Ali
 
Network Engineer Interview Questions with Answers
Network Engineer Interview Questions with Answers Network Engineer Interview Questions with Answers
Network Engineer Interview Questions with Answers Sarmad Ali
 

Viewers also liked (11)

RDBMS ER2 Relational
RDBMS ER2 RelationalRDBMS ER2 Relational
RDBMS ER2 Relational
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
RDBMS ERD
RDBMS ERDRDBMS ERD
RDBMS ERD
 
Artificial Intelligence Course- Introduction
Artificial Intelligence Course- IntroductionArtificial Intelligence Course- Introduction
Artificial Intelligence Course- Introduction
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
Compiler Construction Course - Introduction
Compiler Construction Course - IntroductionCompiler Construction Course - Introduction
Compiler Construction Course - Introduction
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Compiler Design Lecture Notes
Compiler Design Lecture NotesCompiler Design Lecture Notes
Compiler Design Lecture Notes
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Network Engineer Interview Questions with Answers
Network Engineer Interview Questions with Answers Network Engineer Interview Questions with Answers
Network Engineer Interview Questions with Answers
 

Similar to RDBMS Algebra

Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentationNITISH KUMAR
 
Assessment of Learning 2 (Overview)
Assessment of Learning 2 (Overview)Assessment of Learning 2 (Overview)
Assessment of Learning 2 (Overview)Caroline Lace
 
Class 10 Cbse Maths 2010 Sample Paper Model 3
Class 10 Cbse Maths 2010 Sample Paper Model 3 Class 10 Cbse Maths 2010 Sample Paper Model 3
Class 10 Cbse Maths 2010 Sample Paper Model 3 Sunaina Rawat
 
Notes and formulae mathematics
Notes and formulae mathematicsNotes and formulae mathematics
Notes and formulae mathematicsZainonie Ma'arof
 
11.2 Arithmetic Sequences and Series
11.2 Arithmetic Sequences and Series11.2 Arithmetic Sequences and Series
11.2 Arithmetic Sequences and Seriessmiller5
 
QUARTILE AND DECILE OF GROUPED DATA
QUARTILE AND DECILE OF GROUPED DATAQUARTILE AND DECILE OF GROUPED DATA
QUARTILE AND DECILE OF GROUPED DATAJENNYROSEDALIGDIG1
 
Pre-Assessment-Activity.pdf
Pre-Assessment-Activity.pdfPre-Assessment-Activity.pdf
Pre-Assessment-Activity.pdfByronSmith46
 
Chapter 3: Linear Systems and Matrices - Part 3/Slides
Chapter 3: Linear Systems and Matrices - Part 3/SlidesChapter 3: Linear Systems and Matrices - Part 3/Slides
Chapter 3: Linear Systems and Matrices - Part 3/SlidesChaimae Baroudi
 
Sequences and Series (S&S GAME) - Barisan dan Deret.pdf
Sequences and Series (S&S GAME) - Barisan dan Deret.pdfSequences and Series (S&S GAME) - Barisan dan Deret.pdf
Sequences and Series (S&S GAME) - Barisan dan Deret.pdfDiah Lutfiana Dewi
 
NTSE Ap.pptx and I am studying aakash and
NTSE Ap.pptx and I am studying aakash andNTSE Ap.pptx and I am studying aakash and
NTSE Ap.pptx and I am studying aakash andAyushSaxena838963
 
Class 10 Cbse Maths 2010 Sample Paper Model 2
Class 10 Cbse Maths 2010 Sample Paper Model 2Class 10 Cbse Maths 2010 Sample Paper Model 2
Class 10 Cbse Maths 2010 Sample Paper Model 2Sunaina Rawat
 
Form 5 Additional Maths Note
Form 5 Additional Maths NoteForm 5 Additional Maths Note
Form 5 Additional Maths NoteChek Wei Tan
 
9.4 Series and Their Notations
9.4 Series and Their Notations9.4 Series and Their Notations
9.4 Series and Their Notationssmiller5
 
Kem akademik sept 16
Kem akademik  sept 16Kem akademik  sept 16
Kem akademik sept 16SMK Mukah
 
Presentation of Arithmatic sequence Series Created by Ambreen koondhar.pptx
Presentation of Arithmatic sequence Series Created by Ambreen koondhar.pptxPresentation of Arithmatic sequence Series Created by Ambreen koondhar.pptx
Presentation of Arithmatic sequence Series Created by Ambreen koondhar.pptxsadafkoondhar
 
Barisan dan deret .ingg
Barisan dan deret .inggBarisan dan deret .ingg
Barisan dan deret .inggFendik Bagoez
 
Barisan dan deret .ingg
Barisan dan deret .inggBarisan dan deret .ingg
Barisan dan deret .inggFendik Bagoez
 
Arithmetic progression
Arithmetic progressionArithmetic progression
Arithmetic progressionAnkitGupta1471
 
11.3 Geometric Sequences and Series
11.3 Geometric Sequences and Series11.3 Geometric Sequences and Series
11.3 Geometric Sequences and Seriessmiller5
 

Similar to RDBMS Algebra (20)

Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Assessment of Learning 2 (Overview)
Assessment of Learning 2 (Overview)Assessment of Learning 2 (Overview)
Assessment of Learning 2 (Overview)
 
Class 10 Cbse Maths 2010 Sample Paper Model 3
Class 10 Cbse Maths 2010 Sample Paper Model 3 Class 10 Cbse Maths 2010 Sample Paper Model 3
Class 10 Cbse Maths 2010 Sample Paper Model 3
 
Notes and formulae mathematics
Notes and formulae mathematicsNotes and formulae mathematics
Notes and formulae mathematics
 
11.2 Arithmetic Sequences and Series
11.2 Arithmetic Sequences and Series11.2 Arithmetic Sequences and Series
11.2 Arithmetic Sequences and Series
 
QUARTILE AND DECILE OF GROUPED DATA
QUARTILE AND DECILE OF GROUPED DATAQUARTILE AND DECILE OF GROUPED DATA
QUARTILE AND DECILE OF GROUPED DATA
 
Pre-Assessment-Activity.pdf
Pre-Assessment-Activity.pdfPre-Assessment-Activity.pdf
Pre-Assessment-Activity.pdf
 
Chapter 3: Linear Systems and Matrices - Part 3/Slides
Chapter 3: Linear Systems and Matrices - Part 3/SlidesChapter 3: Linear Systems and Matrices - Part 3/Slides
Chapter 3: Linear Systems and Matrices - Part 3/Slides
 
Sequences and Series (S&S GAME) - Barisan dan Deret.pdf
Sequences and Series (S&S GAME) - Barisan dan Deret.pdfSequences and Series (S&S GAME) - Barisan dan Deret.pdf
Sequences and Series (S&S GAME) - Barisan dan Deret.pdf
 
NTSE Ap.pptx and I am studying aakash and
NTSE Ap.pptx and I am studying aakash andNTSE Ap.pptx and I am studying aakash and
NTSE Ap.pptx and I am studying aakash and
 
Class 10 Cbse Maths 2010 Sample Paper Model 2
Class 10 Cbse Maths 2010 Sample Paper Model 2Class 10 Cbse Maths 2010 Sample Paper Model 2
Class 10 Cbse Maths 2010 Sample Paper Model 2
 
Form 5 Additional Maths Note
Form 5 Additional Maths NoteForm 5 Additional Maths Note
Form 5 Additional Maths Note
 
9.4 Series and Their Notations
9.4 Series and Their Notations9.4 Series and Their Notations
9.4 Series and Their Notations
 
Kem akademik sept 16
Kem akademik  sept 16Kem akademik  sept 16
Kem akademik sept 16
 
Presentation of Arithmatic sequence Series Created by Ambreen koondhar.pptx
Presentation of Arithmatic sequence Series Created by Ambreen koondhar.pptxPresentation of Arithmatic sequence Series Created by Ambreen koondhar.pptx
Presentation of Arithmatic sequence Series Created by Ambreen koondhar.pptx
 
Barisan dan deret .ingg
Barisan dan deret .inggBarisan dan deret .ingg
Barisan dan deret .ingg
 
Barisan dan deret .ingg
Barisan dan deret .inggBarisan dan deret .ingg
Barisan dan deret .ingg
 
Arithmetic progression
Arithmetic progressionArithmetic progression
Arithmetic progression
 
Rumus matematik examonline spa
Rumus matematik examonline spaRumus matematik examonline spa
Rumus matematik examonline spa
 
11.3 Geometric Sequences and Series
11.3 Geometric Sequences and Series11.3 Geometric Sequences and Series
11.3 Geometric Sequences and Series
 

More from Sarmad Ali

RDBMS Arch & Models
RDBMS Arch & ModelsRDBMS Arch & Models
RDBMS Arch & ModelsSarmad Ali
 
RDBMS ERD Examples
RDBMS ERD ExamplesRDBMS ERD Examples
RDBMS ERD ExamplesSarmad Ali
 
Management Information System-MIS
Management Information System-MISManagement Information System-MIS
Management Information System-MISSarmad Ali
 
Classification of Compilers
Classification of CompilersClassification of Compilers
Classification of CompilersSarmad Ali
 
Introduction to RDBMS
Introduction to RDBMSIntroduction to RDBMS
Introduction to RDBMSSarmad Ali
 
About Data Base
About Data BaseAbout Data Base
About Data BaseSarmad Ali
 
Management information system-MIS
Management information system-MISManagement information system-MIS
Management information system-MISSarmad Ali
 

More from Sarmad Ali (8)

RDBMS Model
RDBMS ModelRDBMS Model
RDBMS Model
 
RDBMS Arch & Models
RDBMS Arch & ModelsRDBMS Arch & Models
RDBMS Arch & Models
 
RDBMS ERD Examples
RDBMS ERD ExamplesRDBMS ERD Examples
RDBMS ERD Examples
 
Management Information System-MIS
Management Information System-MISManagement Information System-MIS
Management Information System-MIS
 
Classification of Compilers
Classification of CompilersClassification of Compilers
Classification of Compilers
 
Introduction to RDBMS
Introduction to RDBMSIntroduction to RDBMS
Introduction to RDBMS
 
About Data Base
About Data BaseAbout Data Base
About Data Base
 
Management information system-MIS
Management information system-MISManagement information system-MIS
Management information system-MIS
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

RDBMS Algebra

  • 2. 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 productA 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
  • 3. 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
  • 4. 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
  • 5. 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)
  • 6. SELECT Example SELECT Student WHERE stuId = ‘S1013’ GIVING Result Result = σ STDID=‘S1013’ (Student) Student ResultStudent 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 Result stuId lastName firstName major credits S1013 McCarthy Owen Math 0
  • 7. SELECT Example SELECT Class WHERE room = ‘H225’ GIVING Answer Answer= σ ROOM=‘H225’ (Class) Class AnswerClass classNumber 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 Answer classNumber facId schedule room MTH101B F110 MTuTh9 H225 MTH103C F110 MWF11 H225
  • 8. PROJECT Example PROJECT Student OVER major GIVING Temp Temp = Π major (Student) Student stuId lastName firstName major credits S1001 Smith Tom History 90 S1002 Chin Ann Math 36 Temp major 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
  • 9. PROJECT Example PROJECT Class OVER (facId, room) Π facId, room (Class) Class classNumber 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
  • 10. 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 Student stuId lastName firstName major credits S1001 Smith Tom History 90 Temp 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 S1005 Lee Perry History 3 Temp stuId lastName firstName major credits S1001 Smith Tom History 90 S1005 Lee Perry History 3 Result lastName firstName stuId Smith Tom S1001 Lee Perry S1005
  • 11. 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)
  • 12. PRODUCT Example Student X Enroll (Produces 63 tuples) Student 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 Enroll stuId 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 90S1001 ART103A A S1001 Smith Tom History 90S1001 HST205A C S1001 Smith Tom History 90S1002 ART103A D S1001 Smith Tom History 90S1002 CSC201A F S1001 Smith Tom History 90S1002 MTH103C B S1001 Smith Tom History 90S1010 ART103A S1001 Smith Tom History 90S1010 MTH103C S1001 Smith Tom History 90S1020 CSC201A B S1001 Smith Tom History 90S1020 MTH101B A S1002 Chin Ann Math 36S1001 ART103A A … … … … …… … …
  • 13. JOIN Example Faculty JOIN Class or Faculty |X| Class Faculty facId name department rank F101 Adams Art Professor F105 Tanaka CSC Instructor F110 Byrne Math Assistant F115 Smith History Associate F221 Smith CSC Professor Class classNumber 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 room F101 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
  • 14. 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 thetables 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]
  • 15. 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
  • 16. NATURAL JOIN Example Student 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) Student 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 Enroll stuId 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 90HST205A C S1001 Smith Tom History 90ART103A A S1002 Chin Ann Math 36MTH103C B S1002 Chin Ann Math 36CSC201A F S1002 Chin Ann Math 36ART103A D S1010 Burns Edward Art 63MTH103C S1010 Burns Edward Art 63ART103A S1020 Rivera Jane CSC 15MTH101B A S1020 Rivera Jane CSC 15CSC201A B
  • 17. EQUIJOIN Example Student EQUIJOIN Enroll (Produce 9 tuples) Equivalent to: Student TIMES Enroll GIVING Temp3 SELECT Temp3 WHERE Student.stuId = Enroll.stuId Student 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 Enroll stuId 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 90S1001 HST205A C S1001 Smith Tom History 90S1001 ART103A A S1002 Chin Ann Math 36S1002 MTH103C B S1002 Chin Ann Math 36S1002 CSC201A F S1002 Chin Ann Math 36S1002 ART103A D S1010 Burns Edward Art 63S1010 MTH103C S1010 Burns Edward Art 63S1010 ART103A S1020 Rivera Jane CSC 15S1020 MTH101B A S1020 Rivera Jane CSC 15S1020 CSC201A B Final result Just 9 tuples
  • 18. 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)
  • 19. More Complicated Queries (cont.) SELECT Student WHERE lastName=‘Chin’ AND firstName=‘Ann’ GIVING Temp1 Student 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 Temp1 stuId lastName firstName major credits S1002 Chin Ann Math 36 Temp1 JOIN Enroll GIVING Temp2 Enroll stuId 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 grade S1002 Chin Ann Math 36ART103A D S1002 Chin Ann Math 36CSC201A F S1002 Chin Ann Math 36MTH103C B PROJECT Temp2 OVER (classNo, grade) GIVING Answer Answer classNumber grade ART103A D CSC201A F MTH103C B
  • 20. 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
  • 21. 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
  • 22. Semijoins Example Student LEFT-SEMIJOIN Enroll or Student |X Enroll Student 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 Enroll stuId 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
  • 23. Outerjoin Example Student OUTER-EQUIJOIN Faculty Compare Student.lastName with Faculty.name Student 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 Faculty facId 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
  • 24. Left-Outer-Equijoin Example Student LEFT-OUTER-EQUIJOIN Faculty Student 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 Faculty facId 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
  • 25. Right-Outer-Equijoin Example Student RIGHT-OUTER-EQUIJOIN Faculty Student 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 Faculty facId 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
  • 26. 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
  • 27. Divide Example Club DIVIDED BY Stu or Club ÷÷÷÷ Stu Club ClubName StuNumb StuLastName Computing S1001 Smith Computing S1002 Chin Stu StuNumb StuLastName S1001 Smith S1002 Chin S1005 Lee Computing S1002 Chin Drama S1001 Smith Drama S1002 Chin Drama S1005 Lee Karate S1001 Smith Karate S1002 Chin Karate S1005 Lee Club DIVIDED BY Stu ClubName Drama Karate
  • 28. 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
  • 29. 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, wriƩen A ∩ BB simultaneously, wriƩen A ∩ B • Difference or A MINUS B: set of tuples in A but not in B, written A - B
  • 30. 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 Processor F115 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
  • 31. Set Operations (cont.) • A INTERSECTION B: set of tuples in both A and B simultaneously, wriƩen 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 Processor F115 Smith History Associate F221 Smith CSC Processor MainFac INTERSECTION BranchFac FacID name department rank F101 Adams Art Processor F221 Smith CSC Processor
  • 32. 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 -
  • 33. Relational Calculus • Formal non-procedural language • Two forms: tuple-oriented and domain- oriented • Based on predicate calculus• Based on predicate calculus
  • 34. 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
  • 35. 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’))}
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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
  • 40. 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
  • 42. ER to Relations Each 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 deptCode office deptName
  • 43. ER to Relations Composite 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
  • 44. ER to Relations Multi-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)
  • 45. ER to Relations Multi-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)
  • 46. ER to Relations Weak 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)
  • 47. ER to Relations 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 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
  • 48. ER to Relations Ternary 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
  • 49. 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)