start of day 8 reading: chap. 2 & 9. relational data model
TRANSCRIPT
![Page 1: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/1.jpg)
START OF DAY 8Reading: Chap. 2 & 9
![Page 2: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/2.jpg)
Relational Data Model
![Page 3: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/3.jpg)
Relational Database
A persistent collection of relationsInformation about various kinds of objects (persons, places, things, events)
Each relation holds information about various kinds of objects (persons, places, things, events, etc.)
Each relation, or table, is characterized by a set of attributes or properties from a domain
e.g., person(SSN, Name, Address, Phone)
Each individual object, or record, is a tuple of valuese.g., (123-45-6789, Pat Carter, 12 Main, 555-5555)
Set of records make up the relation, i.e., subset of the cross-product of the attributes’ domains
Assume DB exists – in reality, create tables, insert tuples, etc.
DBMS – manage storage, integrity/security, crash recovery, etc.
![Page 4: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/4.jpg)
StudentID Name Address Phone
12345 C. Brown 12 Apple St. 555-1234
67890 L. Van Pelt 34 Pear Ave. 555-5678
22222 P. Patty 56 Grape Blvd. 555-9999
33333 Snoopy 12 Apple St. 555-1234
Course StudentID Grade
CS101 12345 A
CS101 67890 B
EE200 12345 C
EE200 22222 B+
EE200 33333 B
CS101 33333 A-
PH100 67890 C+
Course Day Hour
CS101 M 9AM
CS101 W 9AM
CS101 F 9AM
EE200 Tu 10AM
EE200 W 1PM
EE200 Th 10AM
PH100 Tu 11AM
Course Prerequisite
CS101 CS100
EE200 EE005
EE200 CS100
CS120 CS101
CS121 CS120
CS205 CS101
CS206 CS121
CS206 CS205
Course Room
CS101 Turing Aud.
EE200 25 Ohm Hall
PH100 Newton Lab.
Relational Database Examplesnap cr
cdh csgcp
![Page 5: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/5.jpg)
Relational Schemas
Each table has a schemaNameSet of attributesDomain for each attribute
Example: Names: snap, cp, cdh, cr, csgAttributes: table headers
Domains:studentID: integerall the rest are strings, but we could be more
specific (e.g. time, day, grade)
![Page 6: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/6.jpg)
Relational Tables
Tables consist of n-tuples, where n is the arity or degree of the relation (i.e., the number of attributes)
Each n-tuple t D1 D2 … Dn, where the Di’s are the domainse.g., a 3-tuple t of cdh is an element string string string
or string day time or course day time depending on how specific we make our domains
A table is a set of tuples, all with the same schemae.g., cdh Dcourse Dday Dhour
![Page 7: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/7.jpg)
Tables & Keys
Because a table is a set of tuples, there are no duplicates
There is always a set of attributes whose values uniquely identify a tuple (even if it is all of them)
A set of attributes whose values always uniquely identify a tuple constitutes a key
Typically, one or two attributes make up a key
Keys must be declared: we cannot assume uniquenesse.g., Name is not a key; there could be another C. Brown
Some systems add a tuple identifier as the key
![Page 8: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/8.jpg)
Keys ExamplesTable Keysnap StudentID
Name, Address, Phone (possible key?)
cp Course Prerequisite
cdh Course Day HourCan a course meet twice on the same day?If not: Course Day
cr Course RoomDoes a course always meet in the same room?If so: Course
csg Course StudentID
![Page 9: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/9.jpg)
Predicates and Tuples
A table name for tuples of arity n is an n-place predicatecdh('CS101','M','9AM')Asserts that CS101 meets on Monday at 9:00 am
Predicates give each tuple a meaning in the ordinary sense of predicatesThe subset of D1 D2 … Dn present in the database are
those assigned T; all others are assigned F (CWA)Interpretation:
DomainFor each predicate and every substitution, T or F
Every relation is a set, and every set is a predicate.
Hence every relation is a predicate (and vice-versa)
![Page 10: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/10.jpg)
Database TuplesDatabase tuples (strictly speaking) are not true
subsets of D1 D2 … Dn because we can alter the column order if we do so “correctly”
More properly defined, a tuple in a relation is a set of attribute-value pairse.g. {(Course, 'CS101'), (Day, 'M'), (Hour, '9AM') }
= {(Day, 'M'), (Course, 'CS101'), (Hour, '9AM') }Normally, we factor out the attribute and fix the orderImplication: we can interchange columns
cr = Course Room = Room Course CS101 Turing Aud. Turing Aud.CS101
EE200 25 Ohm Hall 25 Ohm HallEE200
PH100 Newton Lab. Newton Lab.PH100
![Page 11: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/11.jpg)
Relational Algebra
![Page 12: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/12.jpg)
Relational Algebra
What is an algebra?a pair: (set of values, set of operations)
ADT type Class Object
e.g., stack: (set of all stacks, {pop, push, top, …})
integer: (set of all integers, {+, -, *, })
What is relational algebra?(set of relations, set of relational operators)
Operators: , , , , , , , ||
![Page 13: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/13.jpg)
Relational Algebra is Closed
Closed: all operations produce values in the value set(reals, {+, *, }) closed
(reals, {+, *, , }) not closed (divide by 0)
(reals, {+, *, >}) not closed (T/F not in value set)
(computer reals, {+, *, }) not closed (overflow, roundoff)
(relations, relational operators) closed
Implication: we can always nest (or compose) relational operators; can’t for algebras that are not closed.
![Page 14: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/14.jpg)
Set Operations: , , and
Relations are sets; thus set operations work.
Examples:R = A B 1 2 2 2 2 3
S = A B 2 2 2 3 4 2 5 5
RS = A B 1 2 2 2 2 3 4 2 5 5
RS = A B 2 2 2 3
RS = A B 1 2
SR = A B 4 2 5 5
![Page 15: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/15.jpg)
Set Operations (cont’d)
Definition: schema(R) = {A, B} = AB, i.e. the set of attributes
We sometimes write R(AB) to mean the relation R with schema AB.
Definition: union compatibleschema(R) = schema(S)required precondition for , ,
Definitions: R S = { t | t R t S}R S = { t | t R t S}R S = { t | t R t S}
![Page 16: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/16.jpg)
Tuple Restriction: [X]
Restriction is a tuple operator (not a relational operator).t[X] restricts tuple t to the attributes in X.
A B Ct = 1 2 3
t[A] = (1) t[AC] = (1,3)
![Page 17: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/17.jpg)
Renaming:
ABR renames attribute A to be B in R
A must be in schema(R)B must not be in schema(R)
Example: let
CBQ = A B 2 2 3 2
RCBQ = A B 1
2 2
2 2
3 3
2
R = A B 1 2 2 2 2 3
Q = A C 2 2 3 2
• But with :
RQ = ?Not union compatible
![Page 18: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/18.jpg)
Renaming (cont’d)
Q = ABR renames attribute A to B; the result is Q.
Precondition:A schema(R)B schema(R)
Postcondition:schema(Q) = (schema(R) {A}) {B}Q = {t' | t (tR t' = (t – {(A, t[A])}) {(B, t[A])})}
R = {{(A,1), (C,2)} {(A,2), (C,2)}}
Q = ABR = {{(B,1), (C,2)}
{(B,2), (C,2)}}
![Page 19: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/19.jpg)
Selection: The selection operation selects the tuples that satisfy
a condition.
A=1R = A B 1 2
B=2R = A B 1 2 2 2
A=2B2R = A B 2 2
2 3A=3R = A B Note: empty, but still retains the schema
PR = { t | t R P(t) }
• Precondition: each attribute mentioned in P must be in schema(R).
• Postcondition: PR = { t | t R P(t) }
schema(PR) = schema(R)
Meaning: apply predicate P to tuple t by substituting into P appropriate t values.
R = A B 1 2 2 2 2 3
![Page 20: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/20.jpg)
Projection: The projection operation restricts tuples in a
relation to those designated in the operation.
R = A B 1 2 2 2 2 3Q = A B C 1 1 1 2 1 1 3 4 5
AR = A 1 2
BR = B 2 3
BCQ = B C 1 1 4 5
ABR = R = A,BR = {A,B}R
Precondition: X schema(R)
Postcondition: XR = { t' | t (t R t' = t[X]) }schema(XR) = X
![Page 21: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/21.jpg)
Practice ExercisesPE1
Using the following database, compute:
Trail Name Characteristics
Activity2 Description=Waterfall Recreational features
Feature=Vista Characteristics
Trails Name=North Fork and Forest-ID=2 Trails
![Page 22: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/22.jpg)
Cross Product:
Standard cartesian product adapted for relational algebra
R = A B 1 2 2 2
S = C D 1 1 2 2 3 3
R S = A B C D 1 2 1 1 1 2 2 2 1 2 3 3 2 2 1 1 2 2 2 2 2 2 3 3
![Page 23: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/23.jpg)
Cross Product (cont’d)
R = A B 1 2 = t' 2 2
S = C D 1 1 2 2 3 3 = t''
Precondition: schema(R) schema(S) = Postcondition: R S = { t | t' t''(t' R t'' S t = t' t'')}
schema(R S) = schema(R) schema(S)
t' = { (A,1), (B,2) }
t'' = { (C,3), (D,3) }
t' t'' = { (A,1), (B,2), (C,3), (D,3) }
![Page 24: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/24.jpg)
Cross Product (cont’d)
R = A B 1 2 2 2
S = C A 1 1 2 2 3 3
What if R and S have the same attribute, e.g. A?
Can’t do cross productSolution: RenameAAS
R AAS = A B C A 1 2 1 1 1 2 2 2 1 2 3 3 2 2 1 1 2 2 2 2 2 2 3 3
![Page 25: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/25.jpg)
Natural Join: ||
R = A B 1 2 2 2
S = B C 1 2 2 1 3 2
R || S = A B C 1
2 1 2 2 1
(R )
Cross Product A B
1 2 1 2 1 2 2 2
1 2 2 2
2 1 2 2
3 2
R || S = ABC
Projection
B=B'
Selection Renaming
BB'S
B' C1 22 13 2
1 2
2 2 1
1
![Page 26: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/26.jpg)
Natural Join (cont’d)In general, we can equate 0, 1, 2, or more
attributes using || .A join is defined as:
schema (R || S) = schema(R) schema(S)
R || S = {t | t[schema(R)] R t[schema(S)]
S}
There are no preconditions join always works.
![Page 27: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/27.jpg)
Natural Join (cont’d)
R = A B 1 1 2 3 4 1
S = C D 1 1 1 5
R || S = A B C D 1 1
1 1 1 1
1 5 2 3
1 1 2 3
1 5 4 1
1 1 4 1
1 5
R = A B 1 2 2 2 2 3
S = B C 1 1 2 2 3 3
R || S = A B C 1 2
2 2 2
2 2 3
3R = A B C 1 2 3 2 2 4 2 3 5
S = A B D 1 1 1 2 2 2 2 2 1
R || S = A B C D 2
2 4 2 2
2 4 1
0 attributes in common (full cross product)
1 attribute in common
2 attributes in common
![Page 28: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/28.jpg)
Practice ExercisesPE1
Using the following database, compute:
Department COURSE|x| Semester SECTION
Department=Math COURSE |x| Instructor=Anderson SECTION
Credit_hours=4 COURSE|x| Year=05 SECTION
![Page 29: START OF DAY 8 Reading: Chap. 2 & 9. Relational Data Model](https://reader036.vdocument.in/reader036/viewer/2022062314/56649e235503460f94b1012c/html5/thumbnails/29.jpg)
END OF DAY 8Homework: #12