Relational Data Model

advertisement
Discussion #22
Relational Data Model
Discussion #22
1/10
Topics
• Databases
• Relational database
• Relations and schemas
– Keys
– Tuples and predicates
Discussion #22
2/10
Database
• A persistent collection of information about various kinds of objects
(persons, places, things, events).
• Each object or entity can be characterized by a set of attributes or
properties from a domain.
e.g.
person(SSN, Name, Address, Phone)
• Each individual object has a value associated with it for each attribute; this
is a tuple and forms a record.
e.g.
SSN:
Name:
Address:
Phone:
123-45-6789
Pat Carter
12 Main
555-5555 .
• A set of records is a file (relation, for a relational database).
• A collection of files is a database (relations or tables, for a relational
database).
• Typical operations on a database:
– insert
– delete
– update
– data retrieval
– summarize data (e.g. averages, totals, mins, …)
– create/delete files
• A database system helps people manage databases.
– data storage
– integrity / security
– crash recovery
Discussion #22
3/10
Relational Database Example
snap
StudentID
cr
Name
Address
Phone
Course
Room
12345
C. Brown
12 Apple St.
555-1234
CS101
Turing Aud.
67890
L. Van Pelt
34 Pear Ave.
555-5678
EE200
25 Ohm Hall
22222
P. Patty
56 Grape Blvd.
555-9999
PH100
Newton Lab.
33333
Snoopy
12 Apple St.
555-1234
cp
cdh
csg
Course
Prerequisite
Course
Day
Hour
Course
StudentID
Grade
CS101
CS100
CS101
M
9AM
CS101
12345
A
EE200
EE005
CS101
W
9AM
CS101
67890
B
EE200
CS100
CS101
F
9AM
EE200
12345
C
CS120
CS101
EE200
Tu
10AM
EE200
22222
B+
CS121
CS120
EE200
W
1PM
EE200
33333
B
CS205
CS101
EE200
Th
10AM
CS101
33333
A-
CS206
CS121
PH100
Tu
11AM
PH100
67890
C+
CS206
CS205
Discussion #22
4/10
Relational Schemas
• Each relation has a schema
– name
– set of attributes
– domain for each attribute
• Example:
– Names:
– Attributes:
– Domains:
Discussion #22
snap, cp, cdh, cr, csg
table headers
studentID: integer
all the rest are strings, but we could be
more specific (e.g. time, day, grade)
5/10
Relational Tables
• Table tuples are 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 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  Dcourse  Dday  Dhour
Discussion #22
6/10
Tuple Sets (Relations, Tables) & Keys
• Because a table is a set of tuples, there are no duplicates.
Thus, 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.
Discussion #22
7/10
Keys  Examples
Table
• snap
Key
StudentID
Name, Address, Phone (possible key?)
• cp
Course Prerequisite
• cdh
Course Day Hour
Can a course meet twice on the same day?
If not:
Course Day
• cr
Course Room
Does a course always meet in the same room?
If so:
Course
• csg
Discussion #22
Course StudentID
8/10
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 D1  D2  …  Dn present in the database
are those assigned T; all others are assigned F.
(Closed-World Assumption)
– Interpretation:
• Domain
• For each predicate and every substitution, T or F.
Discussion #22
9/10
Database Tuples
• Database 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 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
= Room
Course
CS101 Turing Aud.
Turing Aud. CS101
EE200 25 Ohm Hall
25 Ohm Hall EE200
PH100 Newton Lab.
Newton Lab. PH100
Discussion #22
10/10
Download