+ All Categories
Home > Documents > 1 Lecture 3: Database Modeling (continued) April 5, 2002.

1 Lecture 3: Database Modeling (continued) April 5, 2002.

Date post: 21-Dec-2015
Category:
View: 216 times
Download: 3 times
Share this document with a friend
Popular Tags:
32
1 Lecture 3: Database Modeling (continued) April 5, 2002
Transcript
Page 1: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

1

Lecture 3: Database Modeling (continued)

April 5, 2002

Page 2: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

2

Q: what do these arrow mean ?

A: store, person, invoice determines movie and store, invoice, movie determines person

Rental

VideoStore

Person

Movie

Invoice

Arrows in Multiway Relationships: Correction

Page 3: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

3

Roles in Relationships

Purchase

What if we need an entity set twice in one relationship?

Product

Person

Store

salesperson buyer

Page 4: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

4

Attributes on Relationships

Purchase

Product

Person

Store

date

Page 5: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

5

Converting Multi-way Relationships to Binary

Purchase

Person

Store

Product

StoreOf

ProductOf

BuyerOf

date

Page 6: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

6

Design Principles

PurchaseProduct Person

What’s wrong?

President PersonCountry

Moral: be faithful!

Page 7: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

7

Design Principles:What’s Wrong?

Purchase

Product

Store

date

personNamepersonAddr

Moral: pick the right kind of entities.

Page 8: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

8

Design Principles:What’s Wrong?

Purchase

Product

Person

Store

dateDates

Moral: don’t complicate life more than it already is.

Page 9: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

9

Next Attactions

• Subclasses (2.4)

• Constraints: (2.5)

• Weak entity sets (2.6)

Page 10: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

10

Modeling Subclasses

Some objects in a class may be special• define a new class• better: define a subclass

Products

Software products

Educational products

So --- we define subclasses (in ODL and in E/R).

Page 11: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

11

Subclasses in ODL

interface SoftwareProduct: Product{ attribute string platform; attribute integer requiredMemory;}

interface EducationalProduct: Product{ attribute struct Interval {integer begin, integer end} ageGroup; attribute string topic;}

interface SoftwareProduct: Product{ attribute string platform; attribute integer requiredMemory;}

interface EducationalProduct: Product{ attribute struct Interval {integer begin, integer end} ageGroup; attribute string topic;}

The two classes inherit all the properties of Product.

Page 12: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

12

Understanding Subclasses

• Think in terms of records:– Product

– SoftwareProduct

– EducationalProduct

field1

field2

field1

field2

field1

field2

field3

field4field5

Page 13: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

13

Multiple Inheritance in ODL

Product

Educational Product

Educ-softwareProduct

Software Product

ageGrouptopic

Platformsrequired memory

Educational-method

Page 14: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

14

interface EducSoftwareProduct:

SoftwareProduct, EducationalProduct {

attribute string educational-method;

}

interface EducSoftwareProduct:

SoftwareProduct, EducationalProduct {

attribute string educational-method;

}

Page 15: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

15

Understanding Multiple Inheritance

• Think in terms of records:– EducSoftwareProduct

field1

field2field3

field4field5

Page 16: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

16

How do we resolve conflicts?

Product

Educational Product

Educ-softwareProduct

Software Product

ageGrouptopic

Platformsrequired memory

Educational-method

Rating(ATA)

Rating(ASA) Rating?

Page 17: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

17

Product

name category

price

isa isa

Educational ProductSoftware Product

Age Groupplatforms

Subclasses in E/R Diagrams

Page 18: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

18

• ODL: classes are disjoint

p1 p2

p3sp1

sp2

ep1

ep2

ep3

Difference between ODL and E/R inheritance

Product

SoftwareProductEducationalProduct

Page 19: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

19

• E/R: entity sets overlap

Difference between ODL and E/R inheritance

SoftwareProduct

EducationalProduct

p1 p2

p3sp1

sp2

ep1

ep2

ep3

Product

Page 20: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

20

• No need for multiple inheritance in E/R

• we have three entity sets, but four different kinds of objects

SoftwareProduct

EducationalProduct

p1 p2

p3sp1

sp2

ep1

ep2

ep3

Product

esp1 esp2

Page 21: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

21

Constraints

• A constraint = an assertion about the database that must be true at all times

• part of the db schema

• types in programming languages do not have anything similar

• correspond to invariants in programming languages

Page 22: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

22

Modeling ConstraintsFinding constraints is part of the modeling process. Commonly used constraints:

Keys: social security number uniquely identifies a person.

Single-value constraints: a person can have only one father.

Referential integrity constraints: if you work for a company, it must exist in the database.

Domain constraints: peoples’ ages are between 0 and 150. General constraints: all others (at most 50 students enroll in a class)

Page 23: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

23

KeysA set of attributes that uniquely identify an object or entity:

Person: social-security-number name name + address name + address + age

Perfect keys are often hard to find, so organizations usuallyinvent something.

Page 24: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

24

Keys

• Multi-attribute keys:– E.g. name + address

• Multiple keys:– E.g social-security-number, name + address

Page 25: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

25

Keys in ODL

Defining multiple keys:

(key ssn employeID (name address age))

interface Person (key ssn) { attribute string ssn; attribute string name; ... }

interface Person (key ssn) { attribute string ssn; attribute string name; ... }

Page 26: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

26

Keys in E/R Diagrams

address name ssn

Person

Product

name category

price

No formal way to specify multiple keys in E/R diagrams

Underline:

Page 27: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

27

Single Value Constraints

• Sometimes we can choose to allow one or more values

• ODL: – attributes are always single value– relationships have single or multiple values

relationship person president;

relationship set<person> presidents;

Page 28: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

28

Single Value Constraints

• E/R:

offers offers

Page 29: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

29

Single Value Constraints

Single Value Constraint:

• we explicitly require one value

• two flavors:– allow nulls– do not allow nulls

Page 30: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

30

Referential Integrity Constraints

• In some formalisms we may refer to other object but get garbage instead– e.g. a dangling pointer in C/C++

• the Referential Integrity Constraint explicitly requires a reference to exists

Page 31: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

31

Referential Integrity Constraints

• In ODL:– means that a relationship cannot be NULL

• In E/R:

CompanyProduct makes

CompanyProduct makes

Page 32: 1 Lecture 3: Database Modeling (continued) April 5, 2002.

32

Weak Entity SetsEntity sets are weak when their key attributes come from otherclasses to which they are related.

This happens if:

- part-of hierarchies - splitting n-ary relations to binary.

UniversityTeam affiliation

numbersport name


Recommended