Top Banner
CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design
19

CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

Dec 16, 2015

Download

Documents

Marjorie Arnold
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

CS 370 Database SystemsCS 370 Database Systems

Lecture 6 Introduction to Database

Design

Page 2: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

A case Study

A primary school student writes a composition about a picnic: Today is Sep 9, the weather is fine. My classmates, Ali, Maria and I go to a picnic in park. Our teacher is Mr. Khan

Picnic

weather

destination

date

Students

Name

Teacher

Name

My Initial Design:

Page 3: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

Questions ?• Why “Ali”, “Maria”, “Mr. Khan” are not in the

ER diagram ?• What do these names tell us ?• What are the keys of Student, Picnic & Teacher

?• What are the cardinalities of the

relationships ?

Page 4: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

• Every student has an ID number, it is better to keep it in the database and use it as a key

• I bet that there won’t be teachers with the same name; otherwise, I’ll add employee number and use it as a key

• goes is N:M, why ? A picnic has more than one student participating; also, a student can go to more than 1 picnic. However, this N:M relationship allows a student to go to more than one picnic on the same date

• leading is N:1 , why? Depends on your assumptions– I assume a teacher can only lead 1 picnic on a certain date, s

o given the teacher name and the date, I can identify a picnic

• Picnic is made a weak entity. I could have added a PicnicNo, but it would be very awkward.

My solution

Student

StudentNo Name weatherdate

destination

Picnicgoes leading Teacher

Name

Page 5: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

E-R Design DecisionsE-R Design Decisions

• The use of an attribute or entity set to represent an object.– Should an address be an attribute or entity?

• Whether a real-world concept is best expressed by an entity set or a relation set.– Should marriage be an entity or relationship?– Should picnic be an entity or relationship?

• The use of a ternary relationship versus a pair of binary relationships.– See the borrow-loan-branch example

• The use of a strong or weak entity set.– See the employee-dependent example

Page 6: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

E-R Diagram for Company DatabaseE-R Diagram for Company Database

EMPLOYEE

WORKS_FOR

MANAGESCONTROLS

StartdateDEPARTMENT

WORKS_ON PROJECT

Hours

DEPENDENTS_OF

DEPENDENT

SUPERVISION

superviseesupervisor

Fname Minit Lname

Name SexAddress

SalarySsn

Bdate

Number Of Employees

Name

NumberLocations

Name

NumberLocation

RelationshipBirthdateSexName Can you translate it back into English?

Page 7: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

• Consider representing Part-time and Full-time employees in the company database:– Either you have two entity types will lots of similarity– Or you have a single entity type with redundancy for

most of the entities within it

• ER model is extended to support other features such as generalization (but it won’t be covered in this course!)

Limitations of ER modelLimitations of ER model

Page 8: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

Reduction of an E-R Schema to tablesReduction of an E-R Schema to tables

• Primary keys allow entity sets and relationship sets to be expressed uniformly as tables which represent the contents of the database.

• A database which conforms to an E-R diagram can be represented by a collection of tables. Always!

• Converting an E-R diagram to a table format is the basis for deriving a relational database design from an E-R diagram.

Page 9: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

Composite key

Representing Entity Sets as TablesRepresenting Entity Sets as Tables

• A weak entity set becomes a table that includes a column for the primary key of the identifying strong entity set

• A strong/regular entity set reduces to a table with the same attributes.

payment loan-no payment-datepayment-no payment-amount

L-17

L-15

L-23

5

22

11

10 May 1999

23 May 1999

17 May 1999

50

300

75

customer customer-name customer-streetcustomer-id

Jones

Hayes

Smith

321-12-3123

677-89-9011

019-28-3746

Main

Main

North

customer-city

Harrison

Rye

Harrison

Page 10: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

Representing Relationship Sets as TablesRepresenting Relationship Sets as Tables

cust-no loan-no share%

…. …. ….

depositor

•A many-to-many relationship set is represented as table with columns for the primary keys of the two participating entity sets, and any descriptive attributes of the relationship set.

customer depositor loan

loan-nocust-no share%

date

cust-name

Page 11: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

For 1:N and 1:1 relationships, you can create a table for each relationship

But it is more concise to merge the relationship-table with the entity-table on the “N” side

customer

cust-no cust-name

A12345

B56789

Peter Wong

Mary Cheung loan

loan-no L-001

L-002

date

Sep 2000

Aug 2001

customer borrow loan

cust-nocust-name

date

loan-no

cust-no

A12345

B56789

indicates who borrowed the

loan

Page 12: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

In a 1:N relationship, can we include the key from the “N” side in the table representing the entity in the “1” side? I.e., include Loan_no into the Customer table. Why and Why not?

Questions to Think AboutQuestions to Think About

CusNo Cust_name Loan_no

A12345 Peter Wong ...B56789 Mary Cheung ...

How can we express existence constraints on table?

Page 13: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

Questions to Think About (Cont.)Questions to Think About (Cont.)

In a 1:1 relationship, we can include the key from either entity into the table representing the other entity. Suppose the Loan-Customer relationship is 1:1, would you include the Customer_no into Loan or Loan_no into Customer?

customer borrow loan

cust-nocust-name

date

loan-no

loan

loan-no L-001L-002

dateSep 2000Aug 2001

cust-noA12345B56789

customer

cust-no cust-nameA12345B56789

Peter WongMary Cheung

loan-no L-001L-002

Which one is better?

Page 14: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

Questions to Think About (Cont.)Questions to Think About (Cont.)

How can we express existence constraints on table?

customer borrow loan

cust-nocust-name

date

loan-no

loan

loan-no L-001L-002

dateSep 2000Aug 2001

cust-noA12345

Not allowed; must be enforced by DBMS

Page 15: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

Weak Entities

Since a weak entity has to include the primary key of the identifying entity, the 1:N relationship is already captured. E.g., The payment table already contains information about the Loan (I.e., loan_no)

Already indicates the1:N relationship

between loan-no and payment-no

payment loan-no payment-datepayment-no payment-amount

L-17

L-17

L-17

5

7

6

10 May 1999

23 May 1999

17 May 1999

50

300

75

Page 16: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

Summary of Symbols Used in E-R NotationSummary of Symbols Used in E-R Notation

Page 17: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

Summary of Symbols Used in E-R NotationSummary of Symbols Used in E-R Notation

Page 18: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

E-R Diagram for a Banking EnterpriseE-R Diagram for a Banking Enterprise

Page 19: CS370 Spring 2007 CS 370 Database Systems Lecture 6 Introduction to Database Design.

CS370 Spring 2007

E-R Diagram for Exercise 2.12

E-R Diagram for a Publisher EnterpriseE-R Diagram for a Publisher Enterprise