database design 2: creating an er model cs 320. review: steps in creating an entity-relationship...

27
Database Design 2: Creating an ER Model CS 320

Upload: lewis-austin

Post on 04-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Database Design 2: Creating an ER Model

CS 320

Page 2: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Review: Steps in Creating an Entity-Relationship Model

1. Identify entities

2. Identify entity attributes and primary keys

3. Specify relationships

Page 3: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

ER Model Notation

Represent entities as rectangles List attributes within the rectangle

UniversityStudent

PK StudentID

StudentName StudentDOB StudentAge

Entity

Attributes

Primary key:Field whose value isunique for each record & serves as an identifier

Page 4: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Aside: Tools for Creating ER Models Microsoft Visio Other open source tools Drawing tools

Powerpoint Illustrator

Page 5: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Data Model Naming Conventions Entity names should be short, descriptive,

compound words UWEC_STUDENT, CandyProduct Entity names will ultimately correspond to table

names

Why compound words?Avoids "reserved" wordsEnsures every entity has a unique name

Page 6: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Data Model Naming Conventions(continued) Attribute names should be descriptive compound

words that correspond to the entity name Attribute names will ultimately correspond to field

names Relates the field back to the table

Every attribute name within the database should be unique

Page 7: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Example Entity (Table) and Attribute (Field) Names

CUST_ID CUST_NAME CUST_TYPE CUST_ADDR CUST_ZIP CUST_PHONE USERNAME PASSWORD

1 Jones, Joe P 1234 Main St. 91212 434-1231 jonesj 12342 Armstrong,Inc. R 231 Globe Blvd. 91212 434-7664 armstrong 33333 Sw edish Burgers R 1889 20th N.E. 91213 434-9090 sw edburg 23534 Pickled Pickles R 194 CityView 91289 324-8909 pickpick 53335 The Candy Kid W 2121 Main St. 91212 563-4545 kidcandy 23516 Waterman, Al P 23 Yankee Blvd. 91234 w ateral 89007 Bobby Bon Bons R 12 Nichi Cres. 91212 434-9045 bobbybon 30118 Crow sh, Elias P 7 77th Ave. 91211 434-0007 crow el 10339 Montag, Susie P 981 Montview 91213 456-2091 montags 9633

10 Columberg Sw eets W 239 East Falls 91209 874-9092 columsw e 8399

PURCH_ID PROD_ID CUST_ID PURCH_DATE DELIVERY_DATE POUNDS STATUS

1 1 5 28-Oct-04 28-Oct-04 3.5 PAID2 2 6 28-Oct-04 30-Oct-04 15 PAID3 1 9 28-Oct-04 28-Oct-04 2 PAID3 3 9 28-Oct-04 28-Oct-04 3.7 PAID4 3 2 28-Oct-04 3.7 PAID5 1 7 29-Oct-04 29-Oct-04 3.7 NOT PAID5 2 7 29-Oct-04 29-Oct-04 1.2 NOT PAID5 3 7 29-Oct-04 29-Oct-04 4.4 NOT PAID6 2 7 29-Oct-04 3 PAID7 2 10 29-Oct-04 14 NOT PAID7 5 10 29-Oct-04 4.8 NOT PAID8 1 4 29-Oct-04 29-Oct-04 1 PAID8 5 4 29-Oct-04 7.6 PAID9 5 4 29-Oct-04 29-Oct-04 3.5 NOT PAID

PROD_ID PROD_DESC PROD_COSTPROD_PRICE

1 Celestial Cashew Crunch 7.45$ 10.00$

2 Unbrittle Peanut Paradise 5.75$ 9.00$

3 Mystery Melange 7.75$ 10.50$

4 Millionaire’s Macadamia Mix 12.50$ 16.00$

5 Nuts Not Nachos 6.25$ 9.50$

CUST_TYPE_IDCUST_TYPE_DESC

P Private

R Retail

W Wholesale

CANDY_CUSTOMER

CANDY_PURCHASECANDY_CUST_TYPE

CANDY_PRODUCT

Page 8: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Primary Key Attributes Attribute whose value is unique for every

entity instance Every entity MUST have a PK Designate by:

Placing as first attribute in the entity Underline Label using "PK"

UniversityStudent

PK StudentID

StudentName StudentDOB StudentAge

Page 9: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Selecting Primary Keys Must be values that are:

Unique for every possible record Do not change Best practice: numeric with no blank spaces or

formatting characters

Often you need to create a surrogate key ID value that serves only to identify the object in the

database Exception: objects with "natural" primary keys

SKU ISBN VIN

Page 10: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Atomic and Composite Attributes Atomic attribute: represents a single data

value15, "Joline", 12/25/2009

Composite attribute: can be decomposed into atomic attributes"James B. Brown""5580 Pinewood Road, Eau Claire, WI 54701"

Page 11: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Composite Attributes Always decompose into atomic

components for: Sorting Searching Formatting

Student_First_NameStudent_MIStudent_Last_Name

Student_Address_Line_1Student_Address_Line_2Student_CityStudent_StateStudent_CountryStudent_Postal_Code

Page 12: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Multi-Valued Attributes Attribute that might have multiple values

for the same entity:

Page 13: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Modeling Multi-Valued AttributesIf it has a definite maximum number, leave as a repeating

attributeIf the upper limit is variable, make a new entity and create a

relationship

Page 14: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Derived Attributes Values that can be derived from other

attributesStudent_Age = 22 (DOB = 11/20/1989,

current date is 11/13/2011) Order_Total = $500 (Item 1 cost = $200, Item

2 cost = $300)

Page 15: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Modeling Derived Attributes In general, don't store derived attributes Instead, create attributes to represent the

underlying data values from which you can derive the valueExamples:

DOB => Age Current_Price and Units_Sold of an item (for a

sales order)

Page 16: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Data Model Relationships Specify the number of instances of one entity

that can be associated with instances of a related entity

Types: 1:M 1:1 M:M

“M” denotes some value greater than 1 whose upper bound is undetermined

This is called relationship cardinality

Page 17: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Example 1:M Relationship

Video_ID Video_Title Video_Format

1000 The Princess Bride DVD

1001 Sideways Bluray

1002 Just Visiting DVD

1003 Crash Bluray

Store_ID Store_Name

1 Northside

2 Southside

Page 18: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Example 1:1 Relationship

Spouse_ID Spouse_Name

52 Ryan, Judy

53 Redmann, Rudy

Customer_

ID

Customer_

Name

Customer_Address

1 Ryan, Paul 5454 Hyde Court

2 Myers, Mary 112 Birch Place

Page 19: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Example M:M Relationship

Page 20: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Putting it all together:

Page 21: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Summary: The Data Modeling Process

1. Define entities

2. Define attributes & primary keys

3. Define relationships

Page 22: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Test Yourself: Which of the following is the best choice for the primary key of a STUDENT entity?

a. STUDENT_NAME

b. STUDENT_EMAIL

c. STUDENT_PHONE_NUMBER

d. STUDENT_SOC_SECURITY_NUMBER

e. None of the above are good choices

Page 23: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Test Yourself: Which of the following is the best choice for the primary key of a STUDENT entity?

a. STUDENT_NAME

b. STUDENT_EMAIL

c. STUDENT_PHONE_NUMBER

d. STUDENT_SOC_SECURITY_NUMBER

e. None of the above are good choices

Page 24: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Test Yourself: In the video rental store database, VIDEO_ID is an example of a:

a. Primary key

b. Surrogate key

c. Multi-valued attribute

d. Derived attribute

e. Both a and b

Page 25: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Test Yourself: In the video rental store database, VIDEO_ID is an example of a(n):

a. Primary key

b. Surrogate key

c. Multi-valued attribute

d. Derived attribute

e. Both a and b

Page 26: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Your Turn: Draw the ER Model for the following

database application:

Al’s Body Shop keeps data on customers that includes name, address, phone number, car make, car model, car year, and VIN.

When a customer brings a car in for service, the service manager completes a work order that contains the date of the service, the name of each service performed, a description of each service, and the charge for each service.

Page 27: Database Design 2: Creating an ER Model CS 320. Review: Steps in Creating an Entity-Relationship Model 1. Identify entities 2. Identify entity attributes

Solution