Temple University – CIS Dept. CIS616– Principles of Database Systems
Temple University – CIS Dept. CIS616– Principles of Database Systems
description
Transcript of Temple University – CIS Dept. CIS616– Principles of Database Systems
![Page 1: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/1.jpg)
Temple University – CIS Dept.CIS616– Principles of Database Systems
V. Megalooikonomou
Relational Model
(based on notes by Silberchatz,Korth, and Sudarshan and notes by C. Faloutsos at CMU)
![Page 2: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/2.jpg)
Overview
history concepts Formal query languages
relational algebra rel. tuple calculus rel. domain calculus
![Page 3: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/3.jpg)
History before: records, pointers, sets etc introduced by E.F. Codd (1923-2003) in 1970 revolutionary!!! first systems: 1977-8 (System R;
Ingres) Turing award in 1981
![Page 4: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/4.jpg)
Concepts
Database: a set of relations (= tables)
rows: tuples columns: attributes (or keys) superkey, candidate key, primary
key
![Page 5: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/5.jpg)
Example
Database:
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
SSN c-id grade123 15-413 A234 15-413 B
![Page 6: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/6.jpg)
Example: cont’d
Database:
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
rel. schema (attr+domains)
tuple
k-th attribute
(Dk domain)
![Page 7: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/7.jpg)
Example: cont’d
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
rel. schema (attr+domains)
instance
![Page 8: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/8.jpg)
Example: cont’d
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
rel. schema (attr+domains)
instance
•Di: the domain of the I-th attribute (eg., char(10)
•Formally: an instance is a subset of (D1 x D2 x …x Dn)
![Page 9: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/9.jpg)
Example: cont’d
superkey (eg., ‘ssn , name’): determines record
cand. key (eg., ‘ssn’, or ‘st#’): minimal superkey (no subset of
it is a superkey) primary key: one of the cand. keys
![Page 10: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/10.jpg)
Another example Example: if
Customer-name = {Jones, Smith, Curry, Lindsay}Customer-street = {Main, North, Park}Customer-city = {Harrison, Rye, Pittsfield}
Then r = { (Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)}
is a relation over Customer-name x Customer-street x Customer-
city
![Page 11: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/11.jpg)
Relations, tuples Relation Schema:
A1, A2, …, An are attributes R = (A1, A2, …, An ) is a relation schema
E.g. Customer-schema = (customer-name, customer-street, customer-city)
r(R) is a relation on the relation schema RE.g. customer (Customer-schema)
Relations are unordered Order of tuples is irrelevant (tuples may be stored in an
arbitrary order)
![Page 12: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/12.jpg)
Database A database consists of multiple relations Information about an enterprise is broken up into parts, with each
relation storing one part of the information
E.g.: account : stores information about accounts depositor : stores information about which customer owns which account customer : stores information about customers
Storing all information as a single relation such as bank(account-number, balance, customer-name, ..)results in
repetition of information (e.g. two customers own an account) the need for null values (e.g. represent a customer without an account)
Normalization theory (discuss later) deals with how to design relational schemas
![Page 13: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/13.jpg)
Overview
history concepts Formal query languages
relational algebra rel. tuple calculus rel. domain calculus
![Page 14: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/14.jpg)
Formal query languages How do we collect information? Eg., find ssn’s of people in cis331 (recall: everything is a set!) One solution: Relational algebra, i.e.,
set operators (procedural language) Q1: Which operators?? Q2: What is a minimal set of
operators?
![Page 15: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/15.jpg)
. . . set union U set difference ‘-’
Relational operators
![Page 16: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/16.jpg)
Example:
FT-STUDENTSsn Name
129 peters main str239 lee 5th ave
PT-STUDENTSsn Name Address
123 smith main str234 jones forbes ave
Q: find all students (part or full time)
A: PT-STUDENT union FT-STUDENT
![Page 17: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/17.jpg)
Observations:
two tables are ‘union compatible’ if they have the same attributes (i.e., same arity: number of attributes and same ‘domains’)
Q: how about intersection ?
U
![Page 18: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/18.jpg)
Observations:
A: redundant: STUDENT intersection STAFF = STUDENT - (STUDENT - STAFF)
STUDENT STAFF
![Page 19: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/19.jpg)
. . . set union set difference ‘-’
Relational operators
U
![Page 20: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/20.jpg)
Other operators?
E.g., find all students on ‘Main street’
A: ‘selection’)('' STUDENTstrmainaddress
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
![Page 21: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/21.jpg)
Other operators?
Notice: selection (and rest of operators) expect tables, and produce tables
--> can be cascaded!! For selection, in general:
)(RELATIONcondition
![Page 22: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/22.jpg)
Selection - examples
Find all ‘Smiths’ on ‘Forbes Ave’
)('''' STUDENTaveForbesaddressSmithname
‘condition’ can be any boolean combination of ‘=‘, ‘>’, ‘>=‘, ...
![Page 23: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/23.jpg)
selection . . set union set difference R - S
Relational operators
)(Rcondition
R U S
![Page 24: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/24.jpg)
selection picks rows - how about columns?
A: ‘projection’ - eg.: finds all the ‘ssn’ - removing
duplicates
Relational operators
)(STUDENTssn
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
![Page 25: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/25.jpg)
Cascading: ‘find ssn of students on ‘forbes ave’
Relational operators
))(( '' STUDENTaveforbesaddressssn
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
![Page 26: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/26.jpg)
selection projection . set union set difference R - S
Relational operators
)(Rcondition)(Rlistatt
R U S
![Page 27: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/27.jpg)
Are we done yet?Q: Give a query we can not answer
yet!
Relational operators
![Page 28: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/28.jpg)
A: any query across two or more tables,eg., ‘find names of students in cis351’
Q: what extra operator do we need??A: surprisingly, the cartesian product is
enough!
Relational operators
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
TAKESSSN c-id grade
123 cis331 A234 cis331 B
![Page 29: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/29.jpg)
Cartesian product
E.g., dog-breeding: MALE x FEMALE gives all possible couples
MALEnamespikespot
FEMALEnamelassieshiba
x =M.name F.namespike lassiespike shibaspot lassiespot shiba
![Page 30: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/30.jpg)
so what?
Eg., how do we find names of students taking cis351?
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
TAKESSSN c-id grade
123 cis331 A234 cis331 B
![Page 31: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/31.jpg)
Cartesian product
A:)(......... .. TAKESxSTUDENTssnTAKESssnSTUDENT
Ssn Name Address ssn cid grade123 smith main str 123cis331 A234jones forbes ave 123cis331 A123 smith main str 234cis331 B234jones forbes ave 234cis331 B
![Page 32: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/32.jpg)
Cartesian product
Ssn Name Address ssn cid grade123 smith main str 123cis331 A234jones forbes ave 123cis331 A123 smith main str 234cis331 B234jones forbes ave 234cis331 B
)))(((.. ..351 TAKESxSTUDENTssnTAKESssnSTUDENTciscid
![Page 33: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/33.jpg)
Ssn Name Address ssn cid grade123 smith main str 123cis331 A234jones forbes ave 123cis331 A123 smith main str 234cis331 B234jones forbes ave 234cis331 B
)
)))(((
(
..351 TAKESxSTUDENTssnTAKESssnSTUDENTciscid
name
![Page 34: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/34.jpg)
selection projection cartesian product MALE x
FEMALE set union set difference R - S
FUNDAMENTALRelational operators
)(Rcondition)(Rlistatt
R U S
![Page 35: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/35.jpg)
Relational ops
Surprisingly, they are enough, to help us answer almost any query we want!!
derived operators, for convenience set intersection join (theta join, equi-join, natural join) ‘rename’ operator division
)(' RRSR
![Page 36: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/36.jpg)
Joins
Equijoin: SR bSaR .. )(.. SRbSaR
![Page 37: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/37.jpg)
Cartesian product
A:
Ssn Name Address ssn cid grade123 smith main str 123cis331 A234jones forbes ave 123cis331 A123 smith main str 234cis331 B234jones forbes ave 234cis331 B
)(......... .. TAKESxSTUDENTssnTAKESssnSTUDENT
![Page 38: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/38.jpg)
Joins
Equijoin:
theta-joins: generalization of equi-join - any
condition
SR bSaR ..
SR
)(.. SRbSaR
![Page 39: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/39.jpg)
Joins
very popular: natural join: R S like equi-join, but it drops
duplicate columns: STUDENT(ssn, name, address) TAKES(ssn, cid, grade)
![Page 40: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/40.jpg)
Joins
nat. join has 5 attributes TAKESSTUDENT
TAKESSTUDENT ssnTAKESssnSTUDENT ..
Ssn Name Address ssn cid grade123 smith main str 123cis331 A234jones forbes ave 123cis331 A123 smith main str 234cis331 B234jones forbes ave 234cis331 B
equi-join: 6
![Page 41: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/41.jpg)
Natural Joins - nit-picking
if no attributes in common between R, S:
nat. join -> cartesian product:
![Page 42: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/42.jpg)
Overview - rel. algebra
fundamental operators derived operators
joins etc rename division
examples
![Page 43: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/43.jpg)
rename op.
Q: why? A:
Shorthand (BEFORE can be a relational algebra expression)
self-joins; … for example, find the grand-
parents of ‘Tom’, given PC(parent-id, child-id)
)(BEFOREAFTER
![Page 44: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/44.jpg)
rename op.
PC(parent-id, child-id) PCPC
PCp-id c-idMary TomPeter MaryJohn Tom
PCp-id c-idMary TomPeter MaryJohn Tom
![Page 45: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/45.jpg)
rename op.
first, WRONG attempt:
(why? how many columns?)
Second WRONG attempt:
PCPC
PCPC idpPCidcPC ..
![Page 46: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/46.jpg)
rename op.
we clearly need two different names for the same table - hence, the ‘rename’ op.
PCPC idpPCidcPCPC ..11 )(
![Page 47: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/47.jpg)
Overview - rel. algebra
fundamental operators derived operators
joins etc rename division
examples
![Page 48: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/48.jpg)
Division
Rarely used, but powerful. Suited for queries that include the
phrase “for all” Example: find suspicious suppliers,
i.e., suppliers that supplied all the parts in A_BOMB
![Page 49: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/49.jpg)
Division
SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3
ABOMBp#p1p2
BAD_Ss#s1
![Page 50: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/50.jpg)
Division
Observations: ~reverse of cartesian product
It can be derived from the 5 fundamental operators (!!)
How?
![Page 51: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/51.jpg)
Division
Answer:
]))([()( )()()( rsrrsr SRSRSR
]))([( )()( rsrSRSR gives those tuples t in such that for some tuple u in S, tu not in R.
)()( rSR
![Page 52: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/52.jpg)
Overview - rel. algebra
fundamental operators derived operators
joins etc rename division
examples
![Page 53: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/53.jpg)
Sample schema
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2
TAKESSSN c-id grade
123cis331 A234cis331 B
find names of students that take cis351
![Page 54: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/54.jpg)
Examples
find names of students that take cis351
)]([ 351 TAKESSTUDENTcisidcname
![Page 55: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/55.jpg)
Sample schemafind course names of ‘smith’
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2
TAKESSSN c-id grade
123cis331 A234cis331 B
![Page 56: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/56.jpg)
Examples
find course names of ‘smith’
)]
([ ''
CLASSTAKESSTUDENT
smithnamenamec
![Page 57: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/57.jpg)
Examples
find ssn of ‘overworked’ students, ie., that take cis331, cis342,
cis350
![Page 58: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/58.jpg)
Examples
find ssn of ‘overworked’ students, ie., that take cis331, cis342, cis350:
almost correct answer:
)(
)(
)(
350
342
331
TAKES
TAKES
TAKES
namec
namec
namec
![Page 59: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/59.jpg)
Examples
find ssn of ‘overworked’ students, ie., that take cis331, cis342, cis350
- Correct answer:
)]([
)]([
)]([
350
342
331
TAKES
TAKES
TAKES
namecssn
namecssn
namecssn
![Page 60: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/60.jpg)
Examples
find ssn of students that work at least as hard as ssn=123
(ie., they take all the courses of ssn=123, and maybe more)
![Page 61: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/61.jpg)
Sample schema
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2
TAKESSSN c-id grade
123cis331 A234cis331 B
![Page 62: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/62.jpg)
Examples
find ssn of students that work at least as hard as ssn=123 (ie., they take all the courses of ssn=123, and maybe more
)]([)]([ 123, TAKESTAKES ssnidcidcssn
![Page 63: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/63.jpg)
Conclusions
Relational model: only tables (‘relations’)
relational algebra: powerful, minimal:
5 operators can handle almost any query!
most non-trivial op.: join
![Page 64: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/64.jpg)
The bank database
![Page 65: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/65.jpg)
Temple University – CIS Dept.CIS616– Principles of Database Systems
V. Megalooikonomou
Relational Model II
(based on notes by Silberchatz,Korth, and Sudarshan and notes by C. Faloutsos at CMU)
![Page 66: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/66.jpg)
Overview
history concepts Formal query languages
relational algebra rel. tuple calculus rel. domain calculus
![Page 67: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/67.jpg)
Extended Relational-Algebra-Operations
Generalized Projection Outer Join Aggregate Functions
![Page 68: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/68.jpg)
Generalized Projection Extends the projection operation by allowing
arithmetic functions to be used in the projection list
F1, F2, …, Fn(E)
E is any relational-algebra expression
Each of F1, F2, …, Fn are arithmetic expressions involving constants and attributes in the schema of E.
Given relation credit-info(customer-name, limit, credit-balance), find how much more each person can spend:
customer-name, limit – credit-balance (credit-info)
![Page 69: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/69.jpg)
Aggregate Functions and Operations
Aggregation function takes a collection of values and returns a single value as a result ?
![Page 70: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/70.jpg)
Aggregate Functions and Operations
Aggregation function takes a collection of values and returns a single value as a result
avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values
Aggregate operation in relational algebra
G1, G2, …, Gn g F1( A1), F2( A2),…, Fn( An) (E) E is any relational-algebra expression G1, G2 …, Gn is a list of attributes on which to group (can be empty) Each Fi is an aggregate function Each Ai is an attribute name
![Page 71: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/71.jpg)
Aggregate Operation – Example
Relation r :A B
C
7
7
3
10
g sum(c) (r)sum-C
27
![Page 72: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/72.jpg)
Aggregate Operation – Example
Relation account grouped by branch-name:
branch-name g sum(balance) (account)
branch-name account-number balance
PerryridgePerryridgeBrightonBrightonRedwood
A-102A-201A-217A-215A-222
400900750750700
branch-name balance
PerryridgeBrightonRedwood
13001500700
![Page 73: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/73.jpg)
Aggregate Functions (Cont.)
Result of aggregation does not have a name Use rename operation For convenience, we permit renaming
as part of aggregate operation
branch-name g sum(balance) as sum-balance (account)
![Page 74: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/74.jpg)
Outer Join An extension of the join operation that
avoids loss of information Computes the join and then adds tuples
from one relation that does not match tuples in the other relation to the result of the join
Uses null values: null signifies that the value is unknown or does
not exist All comparisons involving null are (roughly
speaking) false by definition precise meaning of comparisons with nulls later
![Page 75: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/75.jpg)
Outer Join – Example Relation loan
loan-number amount
L-170L-230L-260
300040001700
Relation borrower
customer-name loan-number
JonesSmithHayes
L-170L-230L-155
branch-name
DowntownRedwoodPerryridge
![Page 76: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/76.jpg)
Outer Join – Example Inner Join
loan borrower
loan borrower
Left Outer Join
loan-number amount
L-170L-230
30004000
customer-name
JonesSmith
branch-name
DowntownRedwood
loan-number amount
L-170L-230L-260
300040001700
customer-name
JonesSmithnull
branch-name
DowntownRedwoodPerryridge
![Page 77: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/77.jpg)
Outer Join – Example Right Outer Join
loan borrower
loan borrower
Full Outer Join
loan-number amount
L-170L-230L-155
30004000null
customer-name
JonesSmithHayes
branch-name
DowntownRedwoodnull
loan-number amount
L-170L-230L-260L-155
300040001700null
customer-name
JonesSmithnullHayes
branch-name
DowntownRedwoodPerryridgenull
![Page 78: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/78.jpg)
Null Values It is possible for tuples to have a null value, denoted by
null, for some of their attributes null signifies unknown value or a value that does not exist The result of any arithmetic expression involving null is null Aggregate functions ignore null values
An arbitrary decision. Could have returned null as result instead Follow semantics of SQL in its handling of null values
For duplicate elimination and grouping, null is treated like any other value, and two nulls are assumed to be the same
Alternative: assume each null is different from each other Both arbitrary decisions, we follow SQL
![Page 79: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/79.jpg)
Null Values Comparisons with null values return the special
truth value unknown If false was used instead of unknown, then not (A < 5)
would not be equivalent to A >= 5 Three-valued logic using the truth value
unknown: OR: (unknown or true) = true,
(unknown or false) = unknown (unknown or unknown) = unknown
AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown
NOT: (not unknown) = unknown In SQL “P is unknown” evaluates to true if predicate P
evaluates to unknown Result of select predicate is treated as false if it
evaluates to unknown
![Page 80: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/80.jpg)
Modification of the Database
The content of the database may be modified using the following operations: Deletion Insertion Updating
All these operations are expressed using the assignment operator
![Page 81: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/81.jpg)
The bank database
![Page 82: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/82.jpg)
Deletion Expressed similarly to a query Instead of displaying tuples, the selected
tuples are removed from the database Can delete only whole tuples
cannot delete values on only particular attributes
A deletion is expressed in relational algebra by:
r r – Ewhere r is a relation and E is a relational algebra query.
![Page 83: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/83.jpg)
Deletion Examples Delete all account records in the Perryridge branch
account account – branch-name = “Perryridge” (account)
Delete all loan records with amount in the range of 0 to 50
loan loan – amount 0and amount 50 (loan)
Delete all accounts at branches located in Needham
r1 branch-city = “Needham” (account branch)
r2 branch-name, account-number, balance (r1)
r3 customer-name, account-number (r2 depositor)
account account – r2
depositor depositor – r3
![Page 84: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/84.jpg)
Insertion To insert data into a relation, we either:
specify a tuple to be inserted write a query whose result is a set of tuples to be
inserted in relational algebra, an insertion is
expressed by?
![Page 85: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/85.jpg)
Insertion To insert data into a relation, we either:
specify a tuple to be inserted write a query whose result is a set of tuples to be
inserted in relational algebra, an insertion is
expressed by:r r E
where r is a relation and E is a relational algebra expression
Insertion of a single tuple: let E be a constant relation containing one tuple
![Page 86: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/86.jpg)
Insertion Examples
Insert information in the database specifying that Smith has $1200 in account A-973 at the Perryridge branch.
account account {(“Perryridge”, A-973, 1200)}depositor depositor {(“Smith”, A-973)}
Provide as a gift for all loan customers in the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account.r1 (branch-name = “Perryridge” (borrower loan))
account account (branch-name, loan-number(r1)) x {(200)})
OR account account branch-name, account-number,200 (r1)
depositor depositor customer-name, loan-number,(r1)
![Page 87: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/87.jpg)
Updating A mechanism to change a value in a tuple
without charging all values in the tuple Use the generalized projection operator
r F1, F2, …, FI, (r)
Each F, is either the i-th attribute of r, if the i-th attribute is not updated, or, if the attribute is to be updated
Fi is an expression, involving only constants and the attributes of r, which gives the new value for the attribute
![Page 88: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/88.jpg)
Update Examples
Make interest payments by increasing all balances by 5 percent
account AN, BN, BAL * 1.05 (account)where AN, BN and BAL stand for account-number, branch-name and balance, respectively
Pay all accounts with balances over $10,0006 percent interest and pay all others 5 percent
account AN, BN, BAL * 1.06 ( BAL 10000 (account)) AN, BN, BAL * 1.05 (BAL 10000 (account))
![Page 89: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/89.jpg)
Views … if it is not desirable for all users to see the
entire logical model (i.e., all the relations stored in the database)
A person who needs to know a customer’s loan number but has no need to see the loan amount can see the relation:customer-name, loan-number (borrower loan)
Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view.
![Page 90: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/90.jpg)
View Definition A view is defined using the create view statement
which has the form
create view v as <query expression>
where <query expression> is any legal relational algebra query expression.
The view name, v, can be used to refer to the virtual relation
View definition creating a new relation by evaluating the query expression. Rather, a view definition causes the saving of an expression to be substituted into queries using the view
Materialized views
![Page 91: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/91.jpg)
View Examples Consider the view (named all-customer) consisting
of branches and their customerscreate view all-customer as
branch-name, customer-name (depositor account)
branch-name, customer-name (borrower loan)
We can find all customers of the Perryridge branch by writing:
customer-name
(branch-name = “Perryridge” (all-customer))
![Page 92: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/92.jpg)
Updates Through View Database modifications expressed as views must be
translated to modifications of the actual relations in the database
E.g., a person who needs to see all loan data in the loan relation except amount. The view is defined as: create view branch-loan as
branch-name, loan-number (loan)
Since we allow a view name to appear wherever a relation name is allowed, the person may write:
branch-loan branch-loan {(“Perryridge”, L-37)}
![Page 93: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/93.jpg)
Updates Through Views (Cont’d.)
The previous insertion must be represented by an insertion into the actual relation loan from which the view branch-loan is constructed
An insertion into loan requires a value for amount. The insertion can be dealt with by either:
rejecting the insertion and returning an error message to the user
inserting a tuple (“L-37”, “Perryridge”, null ) into the loan relation Some updates through views are impossible to translate
into database relation updates create view v as branch-name = “Perryridge” (account)) v v (L-99, Downtown, 23)
Others cannot be translated uniquely all-customer all-customer (Perryridge, John)
Have to choose loan or account, and create a new loan/account number!
![Page 94: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/94.jpg)
Views Defined Using Other Views
One view may be used in the expression defining another view
A view relation v1 is said to depend directly on a view relation v2 if v2 is used in the expression defining v1
A view relation v1 is said to depend on view relation v2 if either v1 depends directly to v2 or there is a path of dependencies from v1 to v2
A view relation v is said to be recursive if it depends on itself
![Page 95: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/95.jpg)
View Expansion
Define the meaning of views in terms of other views… Let view v1 be defined by an expression e1 that may itself
contain uses of view relations View expansion of an expression repeats the following
replacement step:
repeatFind any view relation vi in e1
Replace the view relation vi by the expression defining vi until no more view relations are present in e1
This loop will terminate as long as …?
![Page 96: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/96.jpg)
View Expansion
Define the meaning of views in terms of other views… Let view v1 be defined by an expression e1 that may itself
contain uses of view relations View expansion of an expression repeats the following
replacement step:
repeatFind any view relation vi in e1
Replace the view relation vi by the expression defining vi until no more view relations are present in e1
This loop will terminate as long as the view definitions are not recursive
![Page 97: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/97.jpg)
General Overview - rel. model
history concepts Formal query languages
relational algebra rel. tuple calculus rel. domain calculus
![Page 98: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/98.jpg)
Overview - detailed
Relational tuple calculus Why do we need it? Details Examples Equivalence with rel. algebra More examples; ‘safety’ of
expressions Rel. domain calculus + QBE
![Page 99: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/99.jpg)
Motivation
Q: weakness of rel. algebra? A: procedural
describes the steps (i.e., ‘how’) … still useful, for query optimization
though
![Page 100: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/100.jpg)
Solution: rel. calculus describes what we want two equivalent flavors:
‘tuple’ calculus and ‘domain’ calculus
basis for SQL and QBE, resp.
![Page 101: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/101.jpg)
Rel. tuple calculus (RTC)
first order logic
)}(|{ tPt
}|{ STUDENTtt
‘Give me tuples ‘t’, satisfying predicate P – e.g.:
![Page 102: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/102.jpg)
..or Tuple Relational Calculus
A nonprocedural query language, where each query is of the form
{t | P (t) } It is the set of all tuples t such that predicate P
is true for t t is a tuple variable, t [A] denotes the value of
tuple t on attribute A t r denotes that tuple t is in relation r P is a formula similar to that of the predicate
calculus
![Page 103: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/103.jpg)
Details
symbols allowed:
quantifiers: “for all”, “there exists”
Atom:
),(,
,,,,,,
,,,
,
]'[][
][
attrsattrt
constattrt
TABLEt
![Page 104: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/104.jpg)
Specifically
Formula: Atom
if P1, P2 are formulas, so are
if P(s) is a formula, so are
...21;21 PPPP
))((
))((
sPs
sPs
![Page 105: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/105.jpg)
Specifically
Reminders: DeMorgan implication: double negation:
))(())(( sPTABLEssPTABLEs
)21(21 PPPP
2121 PPPP
‘every human is mortal : no human is immortal’
![Page 106: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/106.jpg)
Reminder: our Mini-U db
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2
TAKESSSN c-id grade
123cis331 A234cis331 B
![Page 107: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/107.jpg)
Examples
find all student records
}|{ STUDENTtt
output tuple of type ‘STUDENT’
![Page 108: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/108.jpg)
Examples
(selection) find student record with ssn=123
![Page 109: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/109.jpg)
Examples
(selection) find student record with ssn=123
}123][|{ ssntSTUDENTtt
![Page 110: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/110.jpg)
Examples
(projection) find name of student with ssn=123
}123][|{ ssntSTUDENTtt
![Page 111: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/111.jpg)
Examples
(projection) find name of student with ssn=123
])}[][
123][(|{
namesnamet
ssnsSTUDENTst
‘t’ has only one column
![Page 112: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/112.jpg)
‘Tracing’
])}[][
123][(|{
namesnamet
ssnsSTUDENTst
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
s
Nameaaaa….jones…zzzz
t
![Page 113: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/113.jpg)
Examples cont’d
(union) get records of both PT and FT students
![Page 114: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/114.jpg)
Examples cont’d
(union) get records of both PT and FT students
}_
_|{
STUDENTPTt
STUDENTFTtt
![Page 115: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/115.jpg)
Examples
difference: find students that are not staff
(assuming that STUDENT and STAFF are union-compatible)
![Page 116: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/116.jpg)
Examples
difference: find students that are not staff
}
|{
STAFFt
STUDENTtt
![Page 117: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/117.jpg)
Cartesian product
e.g., dog-breeding: MALE x FEMALE gives all possible couples
MALEnamespikespot
FEMALEnamelassieshiba
x =M.name F.namespike lassiespike shibaspot lassiespot shiba
![Page 118: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/118.jpg)
Cartesian product
find all the pairs of (male, female)
]}[][
][][
|{
namefnameft
namemnamemt
FEMALEf
MALEmt
![Page 119: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/119.jpg)
‘Proof’ of equivalence
rel. algebra <-> rel. tuple calculus
![Page 120: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/120.jpg)
Overview - detailed
rel. tuple calculus why? details examples equivalence with rel. algebra more examples; ‘safety’ of
expressions re. domain calculus + QBE
![Page 121: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/121.jpg)
More examples
join: find names of students taking cis351
![Page 122: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/122.jpg)
Reminder: our Mini-U db
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2
TAKESSSN c-id grade
123 cis331 A234 cis331 B
![Page 123: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/123.jpg)
More examples
join: find names of students taking cis351
)}351][
][][
][][(
|{
cisidce
namesnamet
ssnessnsTAKESe
STUDENTst
![Page 124: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/124.jpg)
More examples
join: find names of students taking cis351
)}351][
][][
][][(
|{
cisidce
namesnamet
ssnessnsTAKESe
STUDENTst
projection
selection
join
![Page 125: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/125.jpg)
More examples
3-way join: find names of students taking a 2-unit course
![Page 126: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/126.jpg)
Reminder: our Mini-U db
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
CLASSc-id c-name units15-413 s.e. 215-412 o.s. 2
TAKESSSN c-id grade
123 15-413 A234 15-413 B
![Page 127: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/127.jpg)
More examples
3-way join: find names of students taking a 2-unit course
)}2][
][][
][][
][][(
|{
unitsc
namesnamet
idccidce
ssnessnsCLASSc
TAKESeSTUDENTst
selection
projection
join
![Page 128: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/128.jpg)
More examples
3-way join: find names of students taking a 2-unit course - in rel. algebra??
))
(( 2
CLASSTAKESSTUDENTunitsname
![Page 129: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/129.jpg)
Even more examples:
self -joins: find Tom’s grandparent(s)
PCp-id c-idMary TomPeter MaryJohn Tom
PCp-id c-idMary TomPeter MaryJohn Tom
![Page 130: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/130.jpg)
Even more examples:
self -joins: find Tom’s grandparent(s)
)}""][
][][
][][(
|{
Tomidcq
idptidpp
idpqidcp
PCqPCpt
![Page 131: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/131.jpg)
Hard examples: DIVISION
find suppliers that shipped all the ABOMB parts
SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3
ABOMBp#p1p2
BAD_Ss#s1
![Page 132: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/132.jpg)
Hard examples: DIVISION
find suppliers that shipped all the ABOMB parts
])))}#[]#[
]#[]#[
(
((|{
ppps
ssst
SHIPMENTs
ABOMBppt
![Page 133: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/133.jpg)
General pattern
three equivalent versions: 1) if it’s bad, he shipped it
2)either it was good, or he shipped it
3) there is no bad shipment that he missed
))}(((|{ tPABOMBppt
))}(((|{ tPABOMBppt
))}(((|{ tPABOMBppt
![Page 134: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/134.jpg)
More on division
find (SSNs of) students that take all the courses that ssn=123 does (and maybe even more)find students ‘s’ so that if 123 takes a course => so does ‘s’
![Page 135: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/135.jpg)
More on division
find students that take all the courses that ssn=123 does (and maybe even more)
)}
])[][1
][][1
(1
)123][((|{
ssnossnt
idctidct
TAKESt
ssntTAKEStto
![Page 136: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/136.jpg)
Safety of expressions
FORBIDDEN:
It has infinite output!! Instead, always use
}|{ STUDENTtt
}....|{ TABLESOMEtt
![Page 137: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/137.jpg)
Safety of expressions Possible to write tuple calculus expressions that
generate infinite relations, e.g., {t | t r } results in an infinite relation if the domain of any attribute of relation r is infinite
To guard against the problem, we restrict the set of allowable expressions to safe expressions.
An expression {t | P (t) } in the tuple relational calculus is safe if every component of t appears in one of the relations, tuples, or constants that appear in P
![Page 138: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/138.jpg)
More examples: Banking example
branch (branch-name, branch-city, assets) customer (customer-name, customer-
street, customer-city) account (account-number, branch-name,
balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-
number) borrower (customer-name, loan-number)
![Page 139: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/139.jpg)
Example Queries Find the loan-number, branch-name, and amount for
loans of over $1200{t | t loan t [amount] 1200}
Find the loan number for each loan of an amount greater than $1200{t | s loan (t [loan-number] = s [loan-number]
s [amount] 1200}
Notice that a relation on schema [loan-number] is implicitly defined by the query
![Page 140: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/140.jpg)
Example Queries Find the names of all customers having a loan, an
account, or both at the bank{t | s borrower(t[customer-name] = s[customer-name]) u depositor(t[customer-name] = u[customer-name])
Find the names of all customers who have a loan and an account at the bank
{t | s borrower(t[customer-name] = s[customer-name]) u depositor(t[customer-name] = u[customer-name])
![Page 141: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/141.jpg)
Example Queries Find the names of all customers having a loan at the
Perryridge branch{t | s borrower(t[customer-name] = s[customer-name]
u loan(u[branch-name] = “Perryridge” u[loan-number] = s[loan-number]))}
Find the names of all customers who have a loan at the Perryridge branch, but no account at any branch of the bank{t | s borrower(t[customer-name] = s[customer-name] u loan(u[branch-name] = “Perryridge” u[loan-number] = s[loan-number])) not v depositor (v[customer-name] = t[customer-name]) }
![Page 142: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/142.jpg)
Example Queries Find the names of all customers having a loan from
the Perryridge branch, and the cities they live in
{t | s loan(s[branch-name] = “Perryridge” u borrower (u[loan-number] = s[loan-number] t [customer-name] = u[customer-name]) v customer (u[customer-name] = v[customer-name]
t[customer-city] = v[customer-city])))}
![Page 143: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/143.jpg)
Example Queries
Find the names of all customers who have an account at all branches located in Brooklyn:
{t | c customer (t[customer.name] = c[customer-name]) s branch(s[branch-city] = “Brooklyn” u account ( s[branch-name] = u[branch-name] s depositor ( t[customer-name] = s[customer-name] s[account-number] = u[account-number] )) )}
![Page 144: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/144.jpg)
Temple University – CIS Dept.CIS616– Principles of Database Systems
V. Megalooikonomou
Relational Model III
(based on notes by Silberchatz,Korth, and Sudarshan and notes by C. Faloutsos at CMU)
![Page 145: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/145.jpg)
Overview
history concepts Formal query languages
relational algebra rel. tuple calculus rel. domain calculus
![Page 146: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/146.jpg)
General Overview - rel. model
history concepts Formal query languages
relational algebra rel. tuple calculus rel. domain calculus
![Page 147: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/147.jpg)
Overview - detailed
rel. tuple calculus why? details examples equivalence with rel. algebra more examples; ‘safety’ of
expressions rel. domain calculus + QBE
![Page 148: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/148.jpg)
Safety of expressions
FORBIDDEN:
It has infinite output!! Instead, always use
}|{ STUDENTtt
}....|{ TABLESOMEtt
![Page 149: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/149.jpg)
Safety of expressions Possible to write tuple calculus expressions that
generate infinite relations, e.g., {t | t r } results in an infinite relation if the domain of any attribute of relation r is infinite
To guard against the problem, we restrict the set of allowable expressions to safe expressions.
An expression {t | P (t) } in the tuple relational calculus is safe if every component of t appears in one of the relations, tuples, or constants that appear in P
![Page 150: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/150.jpg)
More examples: Banking example
branch (branch-name, branch-city, assets) customer (customer-name, customer-
street, customer-city) account (account-number, branch-name,
balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-
number) borrower (customer-name, loan-number)
![Page 151: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/151.jpg)
Example Queries Find the loan-number, branch-name, and amount for
loans of over $1200{t | t loan t [amount] 1200}
Find the loan number for each loan of an amount greater than $1200{t | s loan (t [loan-number] = s [loan-number]
s [amount] 1200}
Notice that a relation on schema [loan-number] is implicitly defined by the query
![Page 152: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/152.jpg)
Example Queries Find the names of all customers having a loan, an
account, or both at the bank{t | s borrower(t[customer-name] = s[customer-name]) u depositor(t[customer-name] = u[customer-name])
Find the names of all customers who have a loan and an account at the bank
{t | s borrower(t[customer-name] = s[customer-name]) u depositor(t[customer-name] = u[customer-name])
![Page 153: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/153.jpg)
Example Queries Find the names of all customers having a loan at the
Perryridge branch{t | s borrower(t[customer-name] = s[customer-name]
u loan(u[branch-name] = “Perryridge” u[loan-number] = s[loan-number]))}
Find the names of all customers who have a loan at the Perryridge branch, but no account at any branch of the bank{t | s borrower(t[customer-name] = s[customer-name] u loan(u[branch-name] = “Perryridge” u[loan-number] = s[loan-number])) not v depositor (v[customer-name] = t[customer-name]) }
![Page 154: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/154.jpg)
Example Queries Find the names of all customers having a loan from
the Perryridge branch, and the cities they live in
{t | s loan(s[branch-name] = “Perryridge” u borrower (u[loan-number] = s[loan-number] t [customer-name] = u[customer-name]) v customer (u[customer-name] = v[customer-name]
t[customer-city] = v[customer-city])))}
![Page 155: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/155.jpg)
Example Queries
Find the names of all customers who have an account at all branches located in Brooklyn:
{t | c customer (t[customer.name] = c[customer-name]) s branch(s[branch-city] = “Brooklyn” u account ( s[branch-name] = u[branch-name] s depositor ( t[customer-name] = s[customer-name] s[account-number] = u[account-number] )) )}
![Page 156: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/156.jpg)
General Overview
relational model Formal query languages
relational algebra rel. tuple calculus rel. domain calculus
![Page 157: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/157.jpg)
Overview - detailed
rel. tuple calculus dfn details equivalence to rel. algebra
rel. domain calculus + QBE
![Page 158: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/158.jpg)
Rel. domain calculus (RDC)
Q: why? A: slightly easier than RTC,
although equivalent - basis for QBE idea: domain variables (w/ F.O.L.) –
e.g.: ‘find STUDENT record with
ssn=123’
![Page 159: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/159.jpg)
Rel. Dom. Calculus
}123,,|,,{ sSTUDENTansans
find STUDENT record with ssn=123’
![Page 160: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/160.jpg)
Details
Like R.T.C - symbols allowed:
quantifiers
),(,
,,,,,,
,,,
,
![Page 161: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/161.jpg)
Details
but: domain (= column) variables, as opposed to tuple variables, e.g.:
STUDENTans ,,
ssnname address
![Page 162: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/162.jpg)
Domain Relational Calculus
A nonprocedural query language equivalent in power to the tuple relational calculus
Each query is an expression of the form:
{ x1, x2, …, xn | P (x1, x2, …, xn)}
x1, x2, …, xn represent domain variables P represents a formula similar to that of the
predicate calculus
![Page 163: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/163.jpg)
Example queries Find the branch-name, loan-number, and amount for loans of
over $1200
{ l, b, a | l, b, a loan a > 1200} Find the names of all customers who have a loan of over $1200
{ c | l, b, a ( c, l borrower l, b, a loan a > 1200)}
Find the names of all customers who have a loan from the Perryridge branch and the loan amount:
{ c, a | l ( c, l borrower b( l, b, a loan b = “Perryridge”))}
or { c, a | l ( c, l borrower l, “Perryridge”, a loan)}
![Page 164: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/164.jpg)
Example Queries Find the names of all customers having a loan, an
account, or both at the Perryridge branch: { c | l ({ c, l borrower
b,a ( l, b, a loan b = “Perryridge”)) a ( c, a depositor b,n ( a, b, n account b = “Perryridge”))}
Find the names of all customers who have an account at all branches located in Brooklyn:
{ c | n ( c, s, n customer) x,y,z ( x, y, z branch y = “Brooklyn”)
a,b ( x, y, z account c,a depositor)}
![Page 165: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/165.jpg)
Reminder: our Mini-U db
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2
TAKESSSN c-id grade
123cis331 A234cis331 B
![Page 166: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/166.jpg)
Examples
find all student records
}|{ STUDENTtt
},,|,,{ STUDENTansans
RTC:
![Page 167: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/167.jpg)
Examples
(selection) find student record with ssn=123
![Page 168: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/168.jpg)
Examples
(selection) find student record with ssn=123
}123][|{ ssntSTUDENTttRTC:
},,123|,,123{ STUDENTanan
or
}123,,|,,{ sSTUDENTansans
![Page 169: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/169.jpg)
Examples
(projection) find name of student with ssn=123
},,123|{ STUDENTann
![Page 170: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/170.jpg)
Examples
(projection) find name of student with ssn=123
}),,123(|{ STUDENTanan
need to ‘restrict’ “a”
])}[][
123][(|{
namesnamet
ssnsSTUDENTst
RTC:
![Page 171: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/171.jpg)
Examples cont’d
(union) get records of both PT and FT students
}_
_|{
STUDENTPTt
STUDENTFTtt
RTC:
![Page 172: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/172.jpg)
Examples cont’d
(union) get records of both PT and FT students
}_,,
_,,|,,{
STUDENTPTans
STUDENTFTansans
![Page 173: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/173.jpg)
Examples
difference: find students that are not staff
RTC:
}
|{
STAFFt
STUDENTtt
![Page 174: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/174.jpg)
Examples
difference: find students that are not staff
},,
,,|,,{
STAFFans
STUDENTansans
![Page 175: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/175.jpg)
Cartesian product
eg., dog-breeding: MALE x FEMALE gives all possible couples
MALEnamespikespot
FEMALEnamelassieshiba
x =M.name F.namespike lassiespike shibaspot lassiespot shiba
![Page 176: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/176.jpg)
Cartesian product
find all the pairs of (male, female) - RTC:
]}[][
][][
|{
namefnameft
namemnamemt
FEMALEf
MALEmt
![Page 177: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/177.jpg)
Cartesian product
find all the pairs of (male, female) - RDC:
}
|,{
FEMALEf
MALEmfm
![Page 178: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/178.jpg)
‘Proof’ of equivalence
rel. algebra <-> rel. domain calculus
<-> rel. tuple calculus
![Page 179: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/179.jpg)
Overview - detailed
rel. domain calculus why? details examples equivalence with rel. algebra more examples; ‘safety’ of
expressions
![Page 180: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/180.jpg)
More examples
join: find names of students taking cis351
![Page 181: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/181.jpg)
Reminder: our Mini-U db
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
CLASSc-id c-name units15-413 s.e. 215-412 o.s. 2
TAKESSSN c-id grade
123 15-413 A234 15-413 B
![Page 182: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/182.jpg)
More examples
join: find names of students taking cis351 - in RTC
)}351][
][][
][][(
|{
cisidce
namesnamet
ssnessnsTAKESe
STUDENTst
![Page 183: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/183.jpg)
More examples
join: find names of students taking cis351 - in RDC
)},351,
,,(|{
TAKESgciss
STUDENTansgasn
![Page 184: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/184.jpg)
Sneak preview of QBE:
)},351,
,,(|{
TAKESgciss
STUDENTansgasn
STUDENTSsn Name Address_x P.
TAKESSSN c-id grade_x cis351
![Page 185: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/185.jpg)
Sneak preview of QBE:
STUDENTSsn Name Address_x P.
TAKESSSN c-id grade_x cis351
very user friendly heavily based on RDC very similar to MS Access interface
![Page 186: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/186.jpg)
More examples
3-way join: find names of students taking a 2-unit course - in RTC:
)}2][
][][
][][
][][(
|{
unitsc
namesnamet
idccidce
ssnessnsCLASSc
TAKESeSTUDENTst
selection
projection
join
![Page 187: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/187.jpg)
Reminder: our Mini-U db
STUDENTSsn Name Address
123 smith main str234 jones forbes ave
CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2
gradeTAKESSSN c-id
123cis331 A234cis331 B
_x .P
_x _y
_y 2
![Page 188: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/188.jpg)
More examples
3-way join: find names of students taking a 2-unit course
}2,,
,,
,,
.............|{
CLASScnc
TAKESgcs
STUDENTans
n
![Page 189: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/189.jpg)
More examples
3-way join: find names of students taking a 2-unit course
)}
2,,
,,
,,
(,,,,|{
CLASScnc
TAKESgcs
STUDENTans
cngcasn
![Page 190: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/190.jpg)
Even more examples:
self -joins: find Tom’s grandparent(s)
PCp-id c-idMary TomPeter MaryJohn Tom
PCp-id c-idMary TomPeter MaryJohn Tom
![Page 191: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/191.jpg)
Even more examples:
self -joins: find Tom’s grandparent(s)
)}""][
][][
][][(
|{
Tomidcq
idptidpp
idpqidcp
PCqPCpt
![Page 192: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/192.jpg)
Even more examples:
self -joins: find Tom’s grandparent(s)
)}""][
][][
][][(
|{
Tomidcq
idptidpp
idpqidcp
PCqPCpt
)}"",
,(|{
PCTomp
PCpgpg
![Page 193: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/193.jpg)
Even more examples:
self -joins: find Tom’s grandparent(s)
)}"",
,(|{
PCTomp
PCpgpg
![Page 194: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/194.jpg)
Hard examples: DIVISION
find suppliers that shipped all the ABOMB parts
SHIPMENTs# p#s1 p1s2 p1s1 p2s3 p1s5 p3
ABOMBp#p1p2
BAD_Ss#s1
![Page 195: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/195.jpg)
Hard examples: DIVISION
find suppliers that shipped all the ABOMB parts
])))}#[]#[
]#[]#[
(
((|{
ppps
ssst
SHIPMENTs
ABOMBppt
![Page 196: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/196.jpg)
Hard examples: DIVISION
find suppliers that shipped all the ABOMB parts
])))}#[]#[
]#[]#[
(
((|{
ppps
ssst
SHIPMENTs
ABOMBppt
)},
(|{
SHIPMENTps
ABOMBpps
![Page 197: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/197.jpg)
More on division
find students that take all the courses that ssn=123 does (and maybe even more)
)}
])[][1
][][1
(1
)123][((|{
ssnossnt
idctidct
TAKESt
ssntTAKEStto
![Page 198: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/198.jpg)
More on division
find students that take all the courses that ssn=123 does (and maybe even more)
))})',,('
),,123((|{
TAKESgcsg
TAKESgcgcs
![Page 199: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/199.jpg)
Safety of expressions
similar to RTC FORBIDDEN:
},,|,,{ STUDENTansans
![Page 200: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/200.jpg)
Safety of Expressions
{ x1, x2, …, xn | P(x1, x2, …, xn)}
is safe if all of the following hold:1. All values that appear in tuples of the expression are values from dom a(P) (that is, the values appear either in P or in a tuple of a relation mentioned in P )
2. For every “there exists” subformula of the form x (P1(x)), the subformula is true if and only if there is a value x in dom (P1) such that P1(x) is true.
3. For every “for all” subformula of the form x (P1 (x)), the subformula is true if and only if P1(x) is true for all values x from dom (P1).
![Page 201: Temple University – CIS Dept. CIS616– Principles of Database Systems](https://reader038.fdocuments.us/reader038/viewer/2022110404/56812fab550346895d95309d/html5/thumbnails/201.jpg)
Overview - detailed
rel. domain calculus + QBE dfn details equivalence to rel. algebra