1 database systems lecture #3 yan pan school of software, sysu 2011

32
1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

Upload: maria-bryant

Post on 26-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

1

Database SystemsLecture #3

Yan Pan

School of Software, SYSU

2011

Page 2: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

2

Agenda This time:

Constraints Relational model Converting E/R to relations

Next time: Functional dependencies

Page 3: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

3

Quick topic: Weak entity sets Def: some or all key attributes belong to another

ES Plays role in a connecting relationship The key consists of:

Possibly its own attributes and All key attributes of entity sets from supporting

relationships

Page 4: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

4

Conditions for supporting relationships

Supporting relationship R:EF R is many-one (or 1-1) EF With referential integrity (rounded arrow) R is binary E receives key attributes of F

F itself may be weak Another entity set G, and so on recursively…

A1

A2

RE F

Page 5: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

5

For several supporting relships from E to F Keys of each F role appear as foreign key of E

Other, non-supporting many-one relationships are not affected

Conditions for weak entity sets

Bride

Groom

Wedding A1

A2People

Witness

Page 6: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

6

Weak entity set e.g. Example: Hierarchy – species & genus Idea: species name unique per genus only

Exercise: email addresses & logins address = username @ host zhangsan@ mail.sysu.edu.cn Password table stores just username

Draw E/R diagram with weak entity set Username supported by entity set Host

Species Belongs-to Genus

Page 7: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

7

Next topic: Constraints (约束 ) Review: programmer-defined rules stating what should

always be true about consistent databases Restrictions on data (egs?):

Keys Single value constraints Referential Integrity (参照完整性 ) Domain constraints General constraints

Can’t infer (推断 ) constraints from data may hold “accidentally” but they are a part of the schema

Page 8: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

8

E/R keys Uniquely identify entity in ES Attribute or set of attributes

Two entities cannot agree on all key attributes These attributes determine all others

Every ES should have a key possibly including all attributes

Primary key attributes underlined More than one possible key:

Candidate keys, primary key Practical tip: create art key attribute

E.g. SSN, course-id, employee-id, etc. SSN shorter than (name,address)

address

name ssn

Person

Page 9: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

9

Single-valued constraints “at most one” value

Already saw sharp arrows for relationships

Attributes have this automatically could be null or one value Can think of key atts as (non-null) single-valued

TACourse Assists

Page 10: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

10

Referential integrity “Exactly one value”

NOT NULL & foreign keys in SQL Relationships

Non-null value refers to entity that exists Refer to entity with foreign key HTML analogy: no broken links Programming analogy: no dangling pointers Multiple ways of handling violations…

InstructorCourse Taught

Page 11: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

11

Referential integrity – E/R e.g.

Insertion – must refer to existing entity Suppose need to add:

course: DBMS instructor: Yan Pan

Q: Which order?

Q: What if relship were exactly-exactly, say, M(Hs,Ws)? i.e., referential integrity in both directions?

A: Put both inserts in one xact – later

Students CoursesEnrolls

Instructor

Taught

Page 12: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

12

Other kinds of constraints Domain constraints

E.g. date: must be after 1980 Enumerated type: grades A through F, no E No special E/R notation just write near line

General constraints: A class may have no more than 100 students; a

student may not have more than 6 courses:

Students CoursesEnroll <=6<=100

Page 13: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

13

Next topic: the Relational Data Model Invented by Ted Codd

Researcher at IBM We’ll see his name again…

Related work at Berkeley

Introduced in a paper a paper published in June, 1970

Page 14: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

14

DB development path

the

WorldE/R

design

Relational

schema

Relational

DB

Page 15: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

15

Relations as tables

Name Price Category Manufacturer

gizmo $19.99 gadgets GizmoWorksPower gizmo $29.99 gadgets GizmoWorksSingleTouch $149.99 photography CanonMultiTouch $203.99 household Hitachi

tuples/rows/records/entities

Attribute names

Product table/relation

Page 16: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

16

Relational terminology Relation is composed of tuples

Tuple = sequence of attribute values Attribute has atomic types

Relation schema: relation name + attribute names + attribute types

Database schema: set of relation schemas

Page 17: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

17

Relations as sets Recall: math relation is a subset of the cross-

product of the attribute value sets R subset-of S x T Product subset-of Name x Price x Cat x Mft

One member of Product relation: (gizmo, $19.99, gadgets, GizmoWorks) in Product Product(gizmo, $19.99, gadgets, GizmoWorks)

Usual updates: add/delete/change a tuple in this set Updates to the schema are rare, painful (why?)

Page 18: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

18

From E/R models to relations Recall justification:

design is easier in E/R but implementation is easier/faster in R

Analogy to program compilation: design is easier in C/Java/whatever implement. is easier/faster in machine/byte code

Strategy:1. apply semi-mechanical conversion rules2. improve by combining some relations3. improve by normalization

involves finding functional dependencies

Page 19: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

19

E/R conversion rules Relationship relation

attributes: keys of entity-sets/roles key: depends on multiplicity

Entity set … relation attributes: attributes of entity set key: key of ES

NB: mapping of types is not one-one We’ll see: mapping one tokens is also not one-one

Special treatment: Weak entity sets Isa relations & subclasses

Page 20: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

20

Entity Sets Entity set Students

ssn

name

address

Students

John

Howard

Name

South Carolina444-555-6666

Park Avenue111-222-3333

AddressSSN

Rel: Students

Page 21: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

21

Key: keys of both entities

Binary many-to-many relationships

C30.0046444-555-6666

C20.0056111-222-3333

SE-304111-222-3333

CourseIDssn

Relation: Enrolls

EnrollsS_addr

S_NameStudents Course

Course-Name

CourseID

ssn

Page 22: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

22

Many-to-one relationships

Key: keys of many entitiy

Movies Studiosowns

2003SyliaM202

1999Mr. Ripley.M101

YearTitleMovieID

Movies

OrlandoDisneyS73

NYCMiramaxS35

AddressNameStudioID

Studios

S35

S73

StudioID

CN22222

CN11111

CopyrightNo

M202

M101

MovieIDOwns

CopyrightNo

MovieID

Title

Year StudioID

NameAddress

Page 23: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

23

Many-to-one: a better design

Q: What if a movie’s Owns row were missing?

2003SyliaM202

1999Mr. Ripley.M101

YearTitleMovieID

Movies

S35

S73

StudioID

CN22222

CN11111

CopyrightNo

M202

M101

MovieID

Owns

CN22222

CN11111

CopyrightNo

S35

S73

StudioID

2003

1999

Year

SyliaM202

Talent Mr. Ripley

M101

TitleMovieID

Movies’

Page 24: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

24

Many-to-many relationships again NB: Won’t work for many-many relationships

acts

MovieID Title Year

M101 Mr. Ripley 1999

M202 Sylia 2003

M303 P.D. Love 2002

StarID Name Address

T400 Gwyneth P. Bev.Hills

T401 P.S. Hoffman Hollywood

T402 Jude Law Palm Springs

MovieID StarID

M101 T400

M202 T400

M101 T401

M101 T402

M303 T401

Movies

Stars

Acts

Movies Stars

Page 25: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

25

Many-to-many relationships again

MovieID Title Year StarID

M101 Talented Mr. Ripley 1999 T400

M101 Talented Mr. Ripley 1999 T401

M101 Talented Mr. Ripley 1999 T402

M202 Syvlia 2003 T400

M303 Punch Drunk Love 2003 T401

And here’s why:

Page 26: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

26

Multiway relationships & roles

Different roles treated as different entity sets Key: keys of the many entities

Students Courses

TAs

tutors graders

enrolls

TA_SSN Name

SSN CourseID

Name Name

Page 27: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

27

Multiway relationships & roles

Enrolls(S_SSN, Course_ID, Tutor_SSN, Grader_SSN)

SSN Name

111-11-1111 George

222-22-2222 Dick

TA_SSN Name

333-33-3333 Wesley

444-44-4444 Howard

555-55-5555 John

Students TAsCourseID Name

SE-304 Databases

C20.0056 Software

Courses

S_SSN CourseID Tutor_SSN Grader_SSN

111-11-1111 SE-304 333-33-3333 444-44-4444

222-22-2222 SE-304 444-44-4444 555-55-5555

Page 28: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

28

Converting weak ESs – differences

Atts of Crew Rel are: attributes of Crew key attributes of supporting

ESs

Crew Unit-of Studio

StudioNameCrew_ID

address

C2Miramax

C1Disney

C1Miramax

Crew_IDStudioName

Crew

Supporting relships are omitted (why?)

Page 29: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

29

Weak entity sets - relationships

Crew Studio

StudioNameCrew_ID

address

Insurance

IName

Address 1260 7th Av.NYBlueCross

1250 6th Av.NYAetna

AddressIName

InsuranceSubscribes

Unit-of

Page 30: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

30

Weak entity sets - relationships Non-supporting relationships for weak ESs are

converted keys include entire weak ES key

C21

C22

C21

Crew_ID

Aetna

BlueCross

Aetna

Insurer

Universal

Disney

Universal

StudioName

Subscribes

Page 31: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

31

Conversion example Video store rental example, plus some atts

Q: Conversion to relations?

Rental

VideoStore

Customer

Movie

date

yearMNameaddress

Cname

MID

Page 32: 1 Database Systems Lecture #3 Yan Pan School of Software, SYSU 2011

32

Next week For next week:

Review/skim Ch.3 section 5 (from today) Read Ch.19 sections 1-3