Download - Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Transcript
Page 1: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Sree Nilakanta

Page 2: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Developing Relational Models

• What is the relational model and what is a relational database?

• How are relational schemas and their keys defined?• How are entity classes and entities represented in a

relational model and database?• How are attributes and attribute values represented in a

relational model and database?• How are relationship types and relational models

represented in a relational model and database?• How are weak entity classes represented in a relational

model?• How is inheritance represented in a relational model?

Page 3: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Basics of the Relational Model

• The relational model represents information in tables (called relations)– Each table represents a set of entities– Each column of a table represents the attribute values of the entities– Each row of a table represents a single entity

• A database schema is a collection of table definitions (relation schemas)• A relational database is a collection of tables

– Each table stores objects for a single relation schema

Page 4: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Relation Schemas and Keys• The rows of a relational table are unique

– No 2 rows have the same values for all of the attributes

• A key is a collection of attributes in which– No 2 rows have the same values for all attributes in the key

• Every table must have a key– Why?

• A relation schema is the specification of the structure of a table– Name of the table– Name and type of each attribute– Declaration of the key

• A key declaration is a constraint– A table is not allowed to have 2 different rows that have the same value

for the key– Database systems enforce key constraints

• By blocking any attempt to modify a table that will result in a violation of the key constraint

Page 5: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Relation is not Relationship

• Be careful of these two words– Relation– Relationship

• A relation is a table that contains a set of entities• A relationship is a connection between two entities• We must be very careful to use the correct word

– Listen closely to the lectures and correct me if I get it wrong!

Page 6: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Translating E-R Diagrams

• We create a relational model from an E-R model– For each component of the E-R diagram, create a representation

in the relational model

• This chapter describes how to systematically translate an E-R model into a database schema– Entity classes– Simple attributes– Composite attributes– Key attributes– Relationship types of different cardinalities– Weak entity classes– Multi-valued attributes– Inheritance

Page 7: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Representing Entity Classes• For each strong entity classes in your E-R model create a relation schema:• Rule 1a: Define a relation schema by the same name.• Rule 1b: For each single-valued attribute of the entity class

– create an attribute by the same name in the relation schema and specify a type for the attribute

• Rule 1c: Define the key of the new relation schema as the key of the entity class

– If the entity class key consists of multiple simple attributes, the key of the relation schema will be that set of attributes.

– Underline your selected key attribute in each schema in order to identify the key.

Page 8: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Composite Attributes

• Rule 2. For each composite attribute of a strong entity class– create an attribute in the relation schema for each component attribute

– If appropriate, use the name of the composite attribute as a prefix for each of the component attribute names

• Schema: Customer (accountId string, lastName string, firstName string, street string, city string, state string, zipcode string, balance number)

accountId lastName firstName street city state zipcode balance

101 Block Jane 1010 Main St.

Apopka FL 30458 0.00

102 Hamilton Cherry 3230 Dade St.

Dade City

FL 30555 4.47

103 Harrison Kate 103 Dodd Hall

Apopka FL 30457 30.57

104 Breaux Carroll 76 Main St. Apopka FL 30458 34.58

Page 9: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Representing Relationship Types as Attributes

• A relationship type may be represented by attributes in a relation schema

• Add the key attributes of one table to the related table• Consider relationship type Video IsCopyOf Movie

– Key of Movie is movieId– Add attribute movieId to table Video

• Schema Video (videoId int, [other attributes omitted] movieId int references Movie)

• Attribute movieId of relation Video is called a foreign key– Because it’s value is the value of the key of an entity in another

(foreign) table. – Each video is related to the movie with the same movieId

Page 10: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

One-to-Many RelationshipTypes

• For a one-to-many relationship type

– Add the key attributes of one entity class to the other entity class (foreign key attributes).

– Add the foreign key attributes to the class whose cardinality is 1

• Rule 3: For each one-to-many relationship type R between subject class S and target class T

– add the key attributes of class S to class T as foreign keys

– Name the attributes using the role that S plays in relationship type R

– Add the attributes of the relationship type R to target class T.

Page 11: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

One-to-One Relationship Types

• The foreign key attributes may be added to either schema– Each entity class is to-one in the relationship type

• Choose which class to include the foreign key attributes– One option is to try to minimize the number of null values

• Rule 4: For each one-to-one relationship type between two classes, choose one class to be the subject and one to be the target– Add the key attributes of the subject class to the target schema

as foreign key attributes– Add the attributes of the relationship type to the target schema,

just as in Rule 3

Page 12: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Many-to-Many Relationship Types

StoreEmployeeMM

workerWorksIn

ssn storeId

358-44-7865 3

579-98-8778 5

358-44-7865 5

• Many-to-Many relationship types between 2 classes cannot be represented as simple attributes in either related table

• Rule 5: For each many-to-many relationship type R between classes S and T– Create a new relation schema R

– Add attributes to represent the key of S and the key of T as foreign key attributes

– The key of schema R is the combination of those attributes

– Add the relationship attributes to schema R, as in Rule 3

• Schema: WorksIn (ssn string references Employee, storeId number references Store)

Page 13: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Video

1

Customer

1Has Rental

1

MHas

IdentifyingRelationship

Type

Weak EntityClass

costdateDue dateRented

Owner EntityClass

Weak Entity Classes• Weak Entity classes have no keys of their

own• Create keys from

– Foreign keys of identifying relationship types

– Partial keys of the weak class

• Rule 6: For each weak entity class W– Create a new relation schema with the

same name– For each identifying relationship,

• Add the key attributes of the related class to the new schema as foreign key attributes

– Declare the key of the schema to be the combination of

• the foreign key attributes and• The partial key attributes of the weak

entity class – Add the simple and composite attributes of

class W to the schema, as in Rules 2 and 3

• Relation Rental, shown at right, still must add foreign key for Customer

Page 14: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Multi-valued Attributes

1Customer

lastName

address

accountIdfirstName

zipcodestatecitystreet

numberRentals

is apart of

MOtherUser

otherUser

• Represent each multi-valued attribute as if it were a weak entity class with – identifying relationship with owner class– All composite attributes of multi-valued attribute are partial keys

• Rule 7: For each multi-valued attribute M of an entity class C– Define a new relation schema M– Add the components of attribute M to the new schema– Add the key attributes of the schema that contains the other attributes of C to M

as a foreign key– Define the key of the new schema to be the combination of all of its attributes.

• The diagram below shows attribute otherUsers represented as a weak entity class and as a schema

– Do not modify the E-R diagram. This diagram is included to illustrate the concept.

Page 15: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Inheritance

• First representation strategy– Represent each superclass and each subclass as individual tables

• Each table has the attributes of the corresponding class

• Each subclass table has the key of the superclass as both key and foreign key

• Rule 8a: – Create a relation schema for each superclass C using rules 1 and 2.

• For each subclass of C that has a defining attribute, add that attribute to the schema for C.

– For each subclass S, create a new relation schema.

• Add the simple and composite attributes of class S to the schema, as in Rules 1b and 2.

• Add the key of the superclass C as a foreign key referencing relation C.

• Declare the key of the subclass relation for S to be this foreign keys

Page 16: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Example of Rule 8a

d

Video

Movie

isCopy

Of

1M

movieId

media

"dvd" "tape"

dateAcquired

videoId

captioninglanguagesvideo

Formatsoundtrackformat

DVD Videotape

Superclass

Subclass

Inheritancerelationship

type

Definingattribute

Page 17: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Case in Point

• Relational Model for Video Sales for BigHit Online• Process

– Evaluate E-R model for BigHit Online• Figure 4.25, repeated as Fig. 5.23

– Transform E-R diagram into relation schemas• Apply rules 1-8

– Evaluate relation schemas for clarity, accuracy and completeness

Page 18: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

E-R Diagram for BigHit Online

Sale1

Includes

MPurchases

1

Customer

ShoppingCart

Selects

Includes

1

M M

M

M

creditCards

expirationaccountNumber

type

quantity

quantity

creditCard

totalCost saleIddateSold

cartIddate

Created

zipcode

statecity

street zipcode

statecity

street

shippingAddresses

lastName

accountId

firstName expiration

accountNumber

type

Movie

o

costquantitycostquantity

VideotapeDVD

genre

movieId

title

videoFormat

languages

captioning

format

soundtrack

Page 19: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Creating Relation Schemas

• After applying Rule 1, before adding relationship types and inheritance– Customer: (accountId number, lastName string, firstName string)– Sale: (saleId number, dateSold date, totalCost number)– Movie: (movieId number, title string, genre string)– ShoppingCart: (cartId number, dateCreated date)

• Adding composite attributes to Sale– Sale: (saleId number, dateSold date, totalCost number,

creditCardType string, creditCardExpiration date, creditCardAccountNumber number)

• Adding relationship type Purchases to Sale– Sale: (saleId number, dateSold date, totalCost number,

creditCardType string, creditCardExpiration date, creditCardAccountNumber number, accountId number references Customer)

Page 20: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Adding Relationship Types• Add Selects to ShoppingCart

– ShoppingCart: (cartId number, dateCreated date, accountId number references Customer)

• Add schemas for many-to-many relationship types– SaleItem: (saleId number references Sale, movieId references Movie,

quantity number)– CartItem: (cartId number references ShoppingCart, movieId references

Movie, quantity number)• Add schemas for multi-valued attributes

– CreditCards: (accountId number references Customer, accountNumber number, type string, expiration date)

– ShippingAddresses: (accountId number references Customer, street string, city string, state string, zipcode string)

• Add schemas for subclasses of Movie– DVD: (movieId number references Movie, videoFormat string,

languages string, captioning string, cost currency, quantity number)– Videotape: (movieId number references Movie, format string,

soundtrack string, cost currency, quantity number)

Page 21: Copyright © 2003 Addison-Wesley Sree Nilakanta. Copyright © 2003 Addison-Wesley Developing Relational Models What is the relational model and what is.

Copyright © 2003 Addison-Wesley

Resulting Database Schema for BigHit Online

• Customer: (accountId number, lastName string, firstName string)• Sale: (saleId number, dateSold date, totalCost number, creditCardType

string, creditCardExpiration date, creditCardAccountNumber number, accountId number references Customer)

• Movie: (movieId number, title string, genre string)• ShoppingCart: (cartId number, dateCreated date, accountId number

references Customer) • SaleItem: (saleId number references Sale, movieId references Movie,

quantity number)• CartItem: (cartId number references ShoppingCart, movieId references

Movie, quantity number)• CreditCards: (accountId number references Customer, accountNumber

number, type string, expiration date)• ShippingAddresses: (accountId number references Customer, street string,

city string, state string, zipcode string)• DVD: (movieId number references Movie, videoFormat string, languages

string, captioning string)• Videotape: (movieId number references Movie, format string, soundtrack

string)