10
Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 1/10 Discussion #22 Relational Data Model

Embed Size (px)

Citation preview

Page 1: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 1/10

Discussion #22

Relational Data Model

Page 2: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 2/10

Topics

• Databases

• Relational database

• Relations and schemas– Keys– Tuples and predicates

Page 3: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 3/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: 123-45-6789Name: Pat CarterAddress: 12 MainPhone: 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 – data retrieval– delete – summarize data (e.g. averages, totals, mins, …)– update – create/delete files

• A database system helps people manage databases.– data storage– integrity / security– crash recovery

Page 4: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 4/10

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: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 5/10

Relational Schemas• Each relation 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)

Page 6: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 6/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 schemae.g. cdh Dcourse Dday Dhour

Page 7: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 7/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.

Page 8: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 8/10

Keys ExamplesTable Key

• snap StudentIDName, 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: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 9/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.

Page 10: Discussion #22 1/10 Discussion #22 Relational Data Model

Discussion #22 10/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