cs 405g: introduction to database systems lecture 5: logical design by relational model instructor:...

33
CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Upload: gerard-ross

Post on 29-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

CS 405G: Introduction to Database Systems

Lecture 5: Logical Design by Relational ModelInstructor: Chen Qian

Page 2: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 2

Today’s Outline

Relational Model Constraints (unfinished part of last class)

Update Operations Insertion Deletion Update Transaction Dealing with Constraint Violations

Page 3: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 3

Relational Integrity Constraints

Integrity Constraints are conditions that must hold on all valid relation instances.

There are four main types of constraints:1. Domain constraints

1. The value of a attribute must come from its domain

2. Key constraints

3. Entity integrity constraints

4. Referential integrity constraints

Page 4: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Primary Key Constraints

A set of fields is a candidate key (abbreviated as key) for a relation if :1. No two distinct tuples can have same values in all key

fields, and

2. Property 1 is not true for any subset of the key.

What if Part 2 is false? A super key: a set of fields that contains a key.

If there are multiple keys for a relation, one of the keys is chosen (by DBA) to be the primary key.

Page 5: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 5

Key Example

E.g., given a schema Student(sid: string, name: string, gpa: float) we have: sid is a key for Students. (What about name?) The set

{sid, gpa} is a superkey.

CAR (licence_num: string, Engine_serial_num: string, make: string, model: string, year: integer) What is the candidate key(s) Which one you may use as a primary key What are the super keys

Page 6: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 6

Key Example

Enroll (sid: string, cid: string) What is the candidate key(s)?

sid + cid Why

Page 7: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 7

Entity Integrity

Entity Integrity: The primary key attributes (PK) of each relation schema R cannot have null values in any tuple of r(R). Other attributes of R may be similarly constrained to

disallow null values, even though they are not members of the primary key.

Page 8: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Foreign Keys, Referential Integrity

Foreign key : Set of fields in one relation that is used to `refer’ to a tuple in another relation. (Must correspond to primary key of the second relation.) Like a `logical pointer’.

Foreign key constraint: The foreign key in the referencing relation must match the primary key of the referenced relation.

E.g. sid is a foreign key referring to Students: Student(sid: string, name: string, gpa: float) Enrolled(sid: string, cid: string, grade: string) If all foreign key constraints are enforced, referential

integrity is achieved, i.e., no dangling references.

Page 9: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Foreign Key constraints

Only students listed in the Students relation should be allowed to enroll for courses.

sid name login age gpa

53666 Jones jones@cs 18 3.453688 Smith smith@eecs 18 3.253650 Smith smith@math 19 3.8

sid cid grade53666 Carnatic101 C53666 Reggae203 B53650 Topology112 A53666 History105 B

EnrolledStudents

Possible violation: Add <50000, History105, B> to Enrolled. Possible violation: delete <53650, Smith, …> from Students.

Page 10: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 10

Other Types of Constraints

Semantic Integrity Constraints: based on application semantics and cannot be

expressed by the model per se e.g., “the max. no. of hours per employee for all

projects he or she works on is 56 hrs per week” A constraint specification language may have to be

used to express these SQL-99 allows triggers and ASSERTIONS to

allow for some of these

Page 11: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 11

Update Operations on Relations

Update operations INSERT a tuple. DELETE a tuple. MODIFY a tuple.

Constraints should not be violated in updates

Page 12: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 12

Update Operations on Relations

In case of integrity violation, several actions can be taken: Cancel the operation that causes the violation

(REJECT option) Perform the operation but inform the user of the

violation Trigger additional updates so the violation is

corrected (CASCADE option, SET NULL option) Execute a user-specified error-correction routine

Page 13: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 13

Key concept: Transaction

an atomic sequence of database actions (reads/writes) takes DB from one consistent state to another

consistent state 1 consistent state 2transaction

Page 14: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 14

Example

Here, consistency is based on our knowledge of banking “semantics”

In general, up to writer of transaction to ensure transaction preserves consistency

DBMS provides (limited) automatic enforcement, via integrity constraints e.g., balances must be >= 0

checking: $200savings: $1000

Transaction“transfer $100 from Saving to checking”

checking: $300savings: $900

Page 15: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

From E/R Diagrams to Relations

15

Called logical design (different from conceptual design)

Entity sets become relations with the same set of attributes.

Relationships become relations whose attributes are only: The keys of the connected entity sets. Attributes of the relationship itself.

Page 16: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 Luke Huan Univ. of Kansas 16

Design principles

KISS Keep It Simple, Stupid

Avoid redundancy Redundancy wastes space, complicates updates and

deletes, promotes inconsistency Capture essential constraints, but don’t introduce

unnecessary restrictions Use your common sense

Page 17: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Entity Set -> Relation

17

Relation: Beers(name, manf)

Beers

name manf

Page 18: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Relationship -> Relation

18

To represent a relationship, the attributes of the relation include:

1. the primary key attributes of each participating entity set, becoming foreign keys.

2. the descriptive attributes of the relationship set

employee department

name addr name location

Work

Work(employee name, dept name, duration)

duration

Page 19: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Relationship -> Relation The set of nondescriptive attributes is a candidate

key, if there are no key constraints.

employee department

name addr name location

Work

Work(employee name, dept name, duration)

duration

Page 20: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Relationship -> Relation If there is a key constraint, the key of the entity with an arrow is the candidate

key of the relation.

employee department

name addr name location

manage

Manage(employee name, dept name, duration)

duration

Page 21: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Relationship -> Relation

21

Drinkers BeersLikes

Favorite

Favorite(drinker name, beer name)

Married

husband

wife

Married(husband name, wife name)

name addr name manf

Buddies

1 2

Buddies(name1, name2)

Likes(drinker name, beer name)

Page 22: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Combining Relations

22

It is OK to combine the relation for an entity-set E with the relation R for a many-one relationship from E to another entity set.

Example: Drinkers(name, addr) and Favorite(drinker, beer) combine to make Drinker1(name, addr, favBeer).

Drinkers Beers

name addr name manf

Favorite

Page 23: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Combining Relations

23

Risk with Many-Many Relationships: Combining Drinkers with Likes would be a mistake.

It leads to redundancy

Drinkers BeersLikes

name addr name manf

name addr beerSally 123 Maple BudSally 123 Maple Miller

Redundancy

Page 24: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Handling Weak Entity Sets

24

Relation for a weak entity set must include attributes for its complete key (including those belonging to other entity sets), as well as its own, nonkey attributes.

An identifying (double-diamond) relationship is redundant and yields no relation.

Page 25: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 25

Translating weak entity sets

Remember the “borrowed” key attributes Watch out for attribute name conflicts

Building (building_name, year)Rooms (building_name, room_number, capacity)

Seats (building_name, room_number, seat_number, left_or_right)

Rooms In Buildingsname

year

number

capacity

In

Seatsnumber

L/R?

Page 26: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Example

26

Logins HostsAt

name name

time

Page 27: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Example

27

Logins HostsAt

name name

Hosts(hostName)Logins(loginName, hostName, time)At(loginName, hostName, hostName2)

Must be the same

time

At becomes part ofLogins

Page 28: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 28

Mapping of N-ary Relationship Types

For each n-ary relationship type R, where n>2, create a new relationship to represent R.

Include all foreign keys of the participating entity types. include any attributes of the n-ary relationship

type

Page 29: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 29

Ternary relationship types. (a) The SUPPLY relationship.

Page 30: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

04/22/23 30

Mapping the n-ary relationship type SUPPLY

Page 31: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Some exercise

Consider the relations Students, Faculty, Courses, Rooms, Enrolled, Teaches, and Meets.

1. List all the foreign key constraints among these relations.

2. Give an example of a (plausible) constraint involving one or more of these relations that is not a primary key or foreign key constraint.

04/22/23 31

Page 32: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Some exercise

1. No foreign keys for Students, Faculty, Courses, Rooms Enrolled: sid and cid should both have FKCs placed on

them. (Real students must be enrolled in real courses.) Teaches: fid and cid Meets: cid and rno.

2. the length of sid, cid, and fid could be standardized; limits could be placed on the size of the numbers entered

into the credits, room/course capacity, and faculty salary; an enumerated type should be assigned to the grade field etc

04/22/23 32

Page 33: CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian

Next class

Relational algebra (hard part!)

04/22/23 33