Download presentation
Presentation is loading. Please wait.
Published byBranden Francis Modified over 9 years ago
1
START OF DAY 8 Reading: Chap. 2 & 9
2
Relational Data Model
3
Relational Database A persistent collection of relations Information 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 values e.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.
4
StudentIDNameAddressPhone 12345C. Brown12 Apple St.555-1234 67890L. Van Pelt34 Pear Ave.555-5678 22222P. Patty56 Grape Blvd.555-9999 33333Snoopy12 Apple St.555-1234 CourseStudentIDGrade CS10112345A CS10167890B EE20012345C EE20022222B+ EE20033333B CS10133333A- PH10067890C+ CourseDayHour CS101M9AM CS101W9AM CS101F9AM EE200Tu10AM EE200W1PM EE200Th10AM PH100Tu11AM CoursePrerequisite CS101CS100 EE200EE005 EE200CS100 CS120CS101 CS121CS120 CS205CS101 CS206CS121 CS206CS205 CourseRoom CS101Turing Aud. EE20025 Ohm Hall PH100Newton Lab. Relational Database Example snapcr cdhcsg cp
5
Relational Schemas Each table has a schema Name Set of attributes Domain for each attribute Example: Names: snap, cp, cdh, cr, csg Attributes:table headers Domains:studentID: integer all the rest are strings, but we could be more specific (e.g. time, day, grade)
6
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 D 1 D 2 … D n, where the D i ’s are the domains e.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 schema e.g., cdh D course D day D hour
7
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 uniqueness e.g., Name is not a key; there could be another C. Brown Some systems add a tuple identifier as the key
8
Keys Examples TableKey snapStudentID Name, Address, Phone (possible key?) cpCourse Prerequisite cdhCourse Day Hour Can a course meet twice on the same day? If not:Course Day crCourse Room Does a course always meet in the same room? If so:Course csgCourse StudentID
9
Predicates and Tuples A table name for tuples of arity n is an n-place predicate cdh('CS101','M','9AM') Asserts that CS101 meets on Monday at 9:00 am Predicates give each tuple a meaning in the ordinary sense of predicates The subset of D 1 D 2 … D n present in the database are those assigned T; all others are assigned F (CWA) Interpretation: Domain For 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)
10
Database Tuples Database tuples (strictly speaking) are not true subsets of D 1 D 2 … D n 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 pairs e.g. {(Course, 'CS101'), (Day, 'M'), (Hour, '9AM') } = {(Day, ' M ' ), (Course, ' CS101 ' ), (Hour, ' 9AM ' ) } Normally, we factor out the attribute and fix the order Implication: we can interchange columns cr = Course Room =RoomCourse CS101 Turing Aud.Turing Aud.CS101 EE200 25 Ohm Hall25 Ohm HallEE200 PH100 Newton Lab.Newton Lab.PH100
11
Relational Algebra
12
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: , , , , , , , | |
13
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.
14
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 R S = A B 1 2 2 2 2 3 4 2 5 5 R S = A B 2 2 2 3 R S = A B 1 2 S R = A B 4 2 5 5
15
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 compatible schema(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}
16
Tuple Restriction: [X] Restriction is a tuple operator (not a relational operator). t[X] restricts tuple t to the attributes in X. A B C t = 1 2 3 t[ A ] = (1) t[ AC ] = (1,3)
17
Renaming: A B R renames attribute A to be B in R A must be in schema(R) B must not be in schema(R) Example: let C B Q = A B 2 2 3 2 R C B Q = 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 : R Q = ? Not union compatible
18
Renaming (cont’d) Q = A B R 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 (t R t' = (t – {(A, t[A])}) {(B, t[A])})} R = {{(A,1), (C,2)} {(A,2), (C,2)}} Q = A B R = {{(B,1), (C,2)} {(B,2), (C,2)}}
19
Selection: The selection operation selects the tuples that satisfy a condition. A=1 R = A B 1 2 B=2 R = A B 1 2 2 2 A=2 B 2 R = A B 2 2 2 3 A=3 R = A B Note: empty, but still retains the schema P R = { t | t R P(t) } Precondition: each attribute mentioned in P must be in schema(R). Postcondition: P R = { t | t R P(t) } schema( P R) = 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
20
Projection: The projection operation restricts tuples in a relation to those designated in the operation. R = A B 1 2 2 2 2 3 Q = A B C 1 1 1 2 1 1 3 4 5 A R = A 1 2 B R = B 2 3 BC Q = B C 1 1 4 5 AB R = R = A,B R = {A,B} R Precondition: X schema(R) Postcondition: X R = { t' | t (t R t' = t[ X ]) } schema( X R) = X
21
Practice Exercises PE1 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
22
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
23
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) }
24
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 product Solution: Rename A A S R A A S = 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
25
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 2 2 1 2 2 2 2 1 2 2 3 2 R | | S = ABC Projection B=B' Selection Renaming B B' S B' C 1 2 2 1 3 2 1 2 2 1 1
26
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.
27
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 3 R = 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
28
Practice Exercises PE1 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
29
END OF DAY 8 Homework: #12
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.