Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

25
Database Design J.G. Zheng June 29 th 2005 DB Chapter 5

Transcript of Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

Page 1: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

Database Design

J.G. ZhengJune 29th 2005

DB Chapter 5

Page 2: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

2

Overview

Entity Relationship Modeling Data modeling using Entity

Relationship Diagram (ERD)

Transforming ERD into tables

Page 3: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

3

Introduction/Review

Design is a process Analysis

Requirements (user view) Conceptual modeling (ERD)

(Logical) design Transforming ERD to tables and keys

Physical design and implementation Using MS Access

Page 4: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

4

Entity-Relationship Model

A model to represent the real world

Elements Entities Attributes Identifiers Relationships

For ER Model, refer to the book on page 419-423

Page 5: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

5

Entity

Entity Anything in the real world

Entity class (entity set) A set of things of the same type

Entity instance An exact instance of an entity class

ERD notation for entity A rectangular box

Books

Page 6: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

6

Attribute and Identifier

Attribute Properties of entity Notation: an oval

Identifier An attribute with unique value to identify each instance

Title

Books

ISBN

Price

Page 7: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

7

Relationship

Notation: a diamond

Degree of relationship Unary (recursive) – only 1 entity Binary – 2 entities Ternary – 3 entities N-ary – more entities

Books Publisherspublish

Page 8: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

8

Types of Binary Relationship

One-to-One (1:1) A single entity instance in one entity class is

related to a single entity instance in another entity class

Examples: Governors : States Students : PantherCards

ER Notation (Crow’s foot)

Governors Statesgovern

Page 9: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

9

Types of Binary Relationship

One-to-Many (1:N) A single entity instance in one entity class

(parent) is related to multiple entity instances in another entity class (child)

Examples Customers : Orders Students : Degrees

ER Notation (Crow’s foot)

Books Publisherspublish

Page 10: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

10

Types of Binary Relationship

Many-to-Many (N:M) Each entity instance in one entity class is

related to multiple entity instances in another entity class; and vice versa

Examples Books : Writers Customers : Banking Accounts

ER Notation (Crow’s foot)

Books Authorswrite

Page 11: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

11

CardinalityCardinality Describes participation in the relationship Figure 5.43 on page 423

Maximum cardinalities (types of relationships)

Minimum cardinalities Optional (zero) or Mandatory (one)

Certificates Programmershave

Page 12: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

12

A Complete ER Example

Page 13: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

13

ER Diagram ExerciseDraw a ERD about movie data

Let’s only consider the following entities and their attributes

Perfomers: PerformerID, FirstName, LastName, Gender

Movies: MovieID, Title, Maker, Year MovieMakers(companies): MakerID, Name

Assumptions A movie has at least one actor/actress An actor/actress does not have to be in any movie A movie is made by only one company

Page 14: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

14

Transforming ER to Tables

Guidelines to create tables Each entity becomes a relation (table) Attributes of the entity become fields of that

table Identifier becomes primary key

Rules to transform relationships Based on the type of the relationship

One-to-One One-to-Many Many-to-Many

Page 15: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

15

Transforming 1:1 Relationships

One-to-One The key from one relation is placed in

the other as a foreign key

LockerDesc

Location

LockerID

Locker

LockerID

EmpName

EmpID

Employee

Foreign Key

Primary Key

Page 16: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

16

Transforming 1:1 Relationships

EmpID

LockerDesc

Location

LockerID

Locker

EmpName

EmpID

Employee

Foreign Key

Primary Key

Usually, it does not matter which table receives the foreign key

Page 17: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

17

Transforming 1:N Relationships

One-to-Many The primary key from the “One” side is placed

in the “Many” side as a foreign key The foreign key is always on the “Many” side

Instructor

InstructorID

FirstName

Office

Department

CourseSection

CRN

Semester

CourseID

Time

TaughtBy

Page 18: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

18

Transforming 1:N Relationships

Another example

DeptName

Location

DeptID

Department

Dept

EmpName

EmpID

Employee

Foreign Key

Primary Key

Department Employeehas

Page 19: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

19

Transforming N:M Relationships

Many-to-Many There is no direct way to map many

to many relationships To represent an M:N relationship, a

table is created (intersection table) for the relationship

Orders PartsOrderItems

Page 20: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

20

Transforming N:M Example

Many-to-Many is designed as two One-to-Many relationships

Create an intersection table with the primary key from each original table as foreign keys

The intersection table usually has a composite primary key

OrderItems

OrderNum

PartNum

NumOrdered

Orders

OrderNum

OrderDate

CustomerNum

Parts

PartNum

Description

Class

Price

1:N

N:1

Page 21: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

21

M:N Relationship Exercise

Performers MoviesCast

PerformerId

FirstName

LastName

Length

Title

Character

MovieId

Gender

Page 22: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

22

Database Design Language (DBDL)

Customers (CustomerID, FirstName, LastName, Address, SSN, …)

AK SSN

Orders (OrderNumber, OrderDate, Orderby, ShippingMethod, …)

FK Orderby Customers

Page 23: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

23

IDEF1X

Understand Figure 5.2 on page 384

Page 24: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

24

Review QuestionGiven the entities SUPPLIER and PRODUCT are one-to-many relationship, which of the following would represent the correct table design?

A. PRODUCT (ProductID, Description, Cost)SUPPLIER (SupplierID, ContactName, PhoneNumber)

FK ProductID PRODUCT

B. PRODUCT (ProductID, Description, Cost, SupplierID)FK SupplierID SUPPLIER

SUPPLIER (SupplierID, ContactName, PhoneNumber, ProductID)FK ProductID PRODUCT

C. PRODUCT (ProductID, Description, Cost, ContactName) FK ContactName SUPPLIER SUPPLIER (SupplierID, ContactName, PhoneNumber)

D. PRODUCT (ProductID, Description, Cost, SupplierID)FK SupplierID SUPPLIER

SUPPLIER (SupplierID, ContactName, PhoneNumber)

Page 25: Database Design J.G. Zheng June 29 th 2005 DB Chapter 5.

25

Summary

E-R diagram is used widely for database design

Remember the rules to transform ERD to actual tables and relationships One to one One to many Many to many