+ All Categories
Home > Documents > ITN 170 - MySQL Database Programming 1 Lecture 3 :Database Analysis and Design (II) ITN 170 MySQL...

ITN 170 - MySQL Database Programming 1 Lecture 3 :Database Analysis and Design (II) ITN 170 MySQL...

Date post: 21-Dec-2015
Category:
Upload: brent-parker
View: 226 times
Download: 0 times
Share this document with a friend
Popular Tags:
47
ITN 170 - MySQL Database Programming 1 Lecture 3 :Database Analysis and Design (II) ITN 170 MySQL Database Programming
Transcript

ITN 170 - MySQL Database Programming

1

Lecture 3 :Database Analysis and Design (II)

ITN 170 MySQL Database Programming

ITN 170 - MySQL Database Programming

2

Topics covering:

Entity definition

Attribute definition

ERD and ERM

Identify and Model Entities

Relationships

ITN 170 - MySQL Database Programming

3

Entity An entity is something that can be identified in the user’s work environment; something that the users want to track. Example: Mary Doe, Peter Chen, etc (They are instructors at WTCC.)

Entity Classes (also called entity) a collection of the same type of entities.

Attribute Attributes, within an entity, describe the entity’s characteristics.

E-RM / E-RDEntity-relational diagram is a set of concepts and graphical symbols that can be used to create database. The E-R model was first published by Peter Chen in 1976.

Entities Definition

ITN 170 - MySQL Database Programming

4

Examine the nouns. Are they things of significance? Name each entity. Is there information of interest about the entity that the

business needs to hold? Is each instance of the entity uniquely identifiable? Which

attribute or attributes could serve as its UID? Write a description of it. “An EMPLOYEE has significance

as a paid worker at the company. For example, John Brown and Mary Smith are EMPLOYEEs”.

Diagram each entity and all of its attributes.

Identify and model entities from a set of interview notes.

ITN 170 - MySQL Database Programming

5

“I’m the manager of a training company that provides instructor-led courses in management techniques. We teach many courses, each of which has a code, a name, and a fee. Introduction to UNIX and C Programming are two of our more popular courses. Courses vary in length from one day to four days. An instructor can teach several courses. Paul Rogers and Maria Gonzales are two of our best teachers. We track each instructor’s name and phone number. Each course is taught by only one instructor. We create a course and then line up an instructor.

The students can take several courses over time, and many of them do this. Jamie Brown from AT&T took every course we offer! We track each student’s name and phone number. Some of our students and instructors do not give us their phone numbers.”

Example: Identify the entities in the following set of information

ITN 170 - MySQL Database Programming

6

“I’m the manager of a training company that provides instructor-led courses in management techniques. We teach many

courses, each of which has a code, a name, and a fee. Introduction to UNIX and C Programming are two of our more popular courses. Courses vary in length from one day to four days.

An instructor can teach several courses. Paul Rogers and Maria Gonzales are two of our best teachers. We track each instructor’s name and phone number. Each course is taught by only one instructor. We create a course and then line up an instructor.

The students can take several courses over time, and many of them do this. Jamie Brown from AT&T took every course we offer! We track each student’s name and phone number. Some of our students and instructors do not give us their phone numbers.”

Example Result:

ITN 170 - MySQL Database Programming

7

COURSEcodenamefeeduration

INSTRUCTOR (TEACHER)namephone number

STUDENTnamephone number

Solution

The following entities model the Training Company's information requirements.

ITN 170 - MySQL Database Programming

8

Entity Descriptions

A COURSE has significance as a training event offered by the Training Company. For Example, Introduction to UNIX and C Programming.

A STUDENT has significance as a participant in one or more COURSEs. For example, Jamie Brown.

An INSTRUCTOR has significance as a teacher of one or more COURSEs. For example, Paul Rogers and Maria Gonzales.

ITN 170 - MySQL Database Programming

9

Relationships

A relationship is a two-directional, significant association between two entities, or between an entity and itself (e.g. employee & manager).

Relationship Syntax:

Each entity1relationship

name each entity2Must be

ormay be

One or moreor

one and only one{ {} }

ITN 170 - MySQL Database Programming

10

Relationships

The relationship between INSTRUCTER and COURSE is:

Each COURSE may be taught by one and only one INSTRUCTOR

Each INSTRUCTOR may be assigned to one or more COURSEs.

Example:

ITN 170 - MySQL Database Programming

11

Relationships

Each direction of relationship has:

Each direction of relationship has:a name – e.g. taught by or assigned to.an optionality – either must be or may be.a degree – either one and only one, or one or more.

Quick Notes

Cardinality is a synonym for the term degree.A degree of 0 is addressed by may be.

ITN 170 - MySQL Database Programming

12

Relationships

Diagramming Conventions

A line between two entitiesLower case relationship namesOptionality

- - - - - - - - - - Optional (may be)

___________ Mandatory (must be)

Degree___________ One and only one

___________ One or more

ITN 170 - MySQL Database Programming

13

Relationships

mandatory

one

optional

many

Example:

ITN 170 - MySQL Database Programming

14

Relationships

First read a relationship in one direction, and then read the relationship in the other direction:

Example:Read the relationship between EMPLOYEE and DEPARTMENT.

EMPLOYEE DEPARTMENT

Read this relationship first from left to right, and then from right to left.

assignedto

responsiblefor

ITN 170 - MySQL Database Programming

15

Relationships

Relationship from Left to Right (partial diagram)

EMPLOYEE DEPARTMENT

assignedto

Each EMPLOYEE must be assigned to one and only one DEPARTMENT

ITN 170 - MySQL Database Programming

16

Relationships

Relationship from Right to Left (partial diagram)

EMPLOYEE DEPARTMENT

Each DEPARTMENT may be responsible for one or more EMPLOYEEs.

responsiblefor

ITN 170 - MySQL Database Programming

17

Relationships

In class exercises:Exercise 1. Read the relationship between STUDENT and COURSE.

STUDENT COURSE

enrolledin

takenby

Write down the relationships.

ITN 170 - MySQL Database Programming

18

Relationships

In class exercises:Exercise 1. Read the relationship between STUDENT and COURSE.

Each STUDENT must be enrolled in one or more COURSEs.Each COURSE may be taken by one or more STUDENTs.

STUDENT COURSE

enrolledin

takenby

ITN 170 - MySQL Database Programming

19

Relationships

In class exercises:Exercise 2. Read the relationship between PAYCHECK and EMPLOYEE.

PAYCHECK EMPLOYEEfor

thereceiver

of

Write down the relationships.

ITN 170 - MySQL Database Programming

20

Relationships

In class exercises:Exercise 2. Read the relationship between PAYCHECK and EMPLOYEE.

Each PAYCHECK must be for one and only one EMPLOYEE.Each EMPLOYEE may be the receiver of one or more PAYCHECKs.

PAYCHECK EMPLOYEEfor

thereceiver

of

ITN 170 - MySQL Database Programming

21

Relationships

There are three types of relationships:Relationship Types

- Many to One Relationships- Many to Many Relationships- One to One Relationship

All relationships should represent the information requirements and rules of the business.

ITN 170 - MySQL Database Programming

22

Relationships

A Many to One Relationship (M to 1 or M:1) has a degree of one or more in one direction and a degree of one and only one in the other direction.

Quick Notes - M:1 relationships are very common. - M:1 relationships that are mandatory in both directions are rare.

ITN 170 - MySQL Database Programming

23

Relationships

Each CUSTOMER must be visited by one and only one SALES REPRESENTATIVE.Each SALES REPRESENTATIVE may be assigned to visit one or more CUSTOMERs.

CUSTOMERSALESREPRESENTATIVE

visitedby

assignedto visit

Example:

ITN 170 - MySQL Database Programming

24

Relationships

A Many to Many Relationship (M to M or M:M) has a degree of one or more in both directions.

Example 1:There is a M:M relationship between STUDENT and COURSE.

STUDENT COURSE

enrolledin

takenby

Each STUDENT must be enrolled in one or more COURSEs.Each COURSE may be taken by one or more STUDENTs.

ITN 170 - MySQL Database Programming

25

Relationships

Each EMPLOYEE may be assigned to one or more JOBs.Each JOB may be carried out by one or more EMPLOYEEs.

EMLOYEE JOB

assignedto

Carriedout by

Example 2:There is a M:M relationship between EMPLOYEE and JOB.

ITN 170 - MySQL Database Programming

26

Relationships - M:M relationships are very common.

- Many to Many relationships that are usually optional in both directions (e.g. EMPLOYEE vs JOB), (this implies that an instance of either entity can exist without an association with the other).

- Many to Many Relationship may be optional in just one direction (e.g. STDUDENT vs COURSE).

- Many to Many Relationships that are mandatory at both ends are very rare because each is fully dependent on the other for existence.

ITN 170 - MySQL Database Programming

27

RelationshipsA One to One Relationship (1 to 1 or 1:1) has a degree of one and only in both directions.

Example:There is a 1:1 relationship between MICROCOMUTER and MOTHERBOARD.

MICROCOMPUTER MOTHERBOAD

the hostfor

Each MICROCOMPUTER must be the host for one and only one MOTHERBOARD.Each MOTHERBOARD may be incorporated into one and only one MICROCOMPUTER.

incorporatedinto

ITN 170 - MySQL Database Programming

28

Relationships

Quick Notes1:1 relationships are rare (in the real world, you usually

do not see an example, but think about one)

Furthermore, A 1:1 Relationship that is mandatory in both directionsboth directions is very rare (Think about an example)

Entities which seem to have a 1:1 relationship may really be the same entity.

HUSBAND vs WIFE

HUSBAND vs WIFECOUPLE husband wife

ITN 170 - MySQL Database Programming

29

RelationshipsAnother Example:There is a 1:1 relationship between INVOICE and ORDER.

INVOICE ORDER

INVOICE ORDER

In this example, although the names order and invoice are different, the data contained in them is probably almost identical.

ITN 170 - MySQL Database Programming

30

Relationships

ORDER number type

ITEM number description

issued for

the originator

of

originated by

stored in

the repository

for

WAREHOUSE id address

CUSTOMER first name last name

Answer:

in

ITN 170 - MySQL Database Programming

31

Determine the existence of a relationship.Name each direction of the relationship.Determine the optionality of each direction of

the relationship.Determine the degree of each direction of the

relationship.Read the relationship aloud to validate it.

Follow a series of five steps to analyze and model relationships.

ITN 170 - MySQL Database Programming

32

DETERMINE A RELATIONSHIP’S EXISTENCE

Determine the existence of a relationship.Examine each pair of entities to determine if a relationship exists.

Ask About a Relationship’s Existence Does a significant relationship exist between ENTITY A

and ENTITY B?

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

33

DETERMINE A RELATIONSHIP’S EXISTENCE

Example 1

Consider the entities DEPARTMENT and EMPLOYEE. Is there a significant relationship between

DEPARTMENT and EMPLOYEE? Yes, there is a significant relationship between

DEPARTMENT and EMPLOYEE

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

34

DETERMINE A RELATIONSHIP’S EXISTENCE

Example 2

Consider the entities DEPARTMENT and ACTIVITY. Is there a significant relationship between

DEPARTMENT and ACTIVITY? No, there is NOT a significant relationship between

DEPARTMENT and ACTIVITY.

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

35

NAME THE RELATIONSHIP

ExistenceNameOptionalityDegreeValidate

It is a good working practice to name every relationship in your Entity Relationship Model. In some methodologies, it is optional; and in others, it is mandatory. However, since every relationship represents a business rule and the name reflects that rule, it is sensible to try to name all relationships.

ITN 170 - MySQL Database Programming

36

NAME THE RELATIONSHIP

ExistenceNameOptionalityDegreeValidate

The name that you give to the relationship is important since it reflects your understanding of the business rule that links the entities together. Without a name, a relationship may be misleading, and incorrect assumptions can easily be made.

ITN 170 - MySQL Database Programming

37

NAME THE RELATIONSHIP

Name each direction of a relationship.

Ask a Relationship’s Name How is an ENTITY A related to ENTITY B? An ENTITY A is

relationship name in ENTITY B. How is an ENTITY B related to an ENTITY A? An ENTITY B

is relationship name an ENTITY A.

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

38

NAME THE RELATIONSHIP

Example

Consider the relationship between DEPARTMENT and EMPLOYEE. How is a DEPARTMENT related to an EMPLOYEE?

Each DEPARTMENT is responsible for an EMPLOYEE. How is an EMPLOYEE related to a DEPARTMENT?

Each EMPLOYEE is assigned to a DEPARTMENT.

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

39

NAME THE RELATIONSHIP

Use a list of relationship name pairs to assist in naming relationships.

Useful Relationship Name Pairs based on the basis for bought from supplier of description of for operated by the operator for represented by the representation of responsible for the responsibility of

ExistenceNameOptionalityDegreeValidate

Oracle’s suggestion: Do not use related to or associated withas relationship name

ITN 170 - MySQL Database Programming

40

NAME THE RELATIONSHIP

Quick Note:

You must not use words such as related to or associated with. The presence of the relationship indicates an association. The name must convey the business rule causing the association. Choose a name that is meaningful within the business being modeled.

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

41

NAME THE RELATIONSHIP

Use a list of relationship name pairs to assist in naming relationships.

Useful Relationship Name Pairs based on the basis for bought from supplier of description of for operated by the operator for represented by the representation of responsible for the responsibility of

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

42

DETERMINE THE RELATIONSHIP’S OPTIONALITY

Determine the optionality of each direction of the relationship

Ask About a Relationship’s Optionality Must ENTITY A be relationship name ENTITY B? Must ENTITY B be relationship name ENTITY A?

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

43

DETERMINE THE RELATIONSHIP’S OPTIONALITY

Example

Consider the relationship between DEPARTMENT and EMPLOYEE

Must an EMPLOYEE be assigned to a DEPARTMENT? Always? Is there any situation in which an EMPLOYEE will not be assigned to a DEPARTMENT?

No, an EMPLOYEE must always be assigned to a DEPARTMENT.

Must a DEPARTMENT be responsible for an EMPLOYEE?

No, a DEPARTMENT does not have to be responsible for an EMPLOYEE.

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

44

DETERMINE THE RELATIONSHIP’S DEGREE

Determine the degree of the relationship in both directions

Ask about a Relationship’s Degree May ENTITY A be relationship name more than one

ENTITY B? May ENTITY B be relationship name more than one

ENTITY A?

ExistenceNameOptionalityDegreeValidate

ITN 170 - MySQL Database Programming

45

DETERMINE THE RELATIONSHIP’S DEGREE

Consider the relationship between DEPARTMENT and EMPLOYEE

May an EMPLOYEE be assigned to more than one DEPARTMENT?

No, an EMPLOYEE must be assigned to only one DEPARTMENT.

May a DEPARTMENT be responsible for more than one EMPLOYEE?

Yes, a DEAPRTMENT may be responsible for one or more EMPLOYEEs.

ExistenceNameOptionalityDegreeValidate

Example

ITN 170 - MySQL Database Programming

46

DETERMINE THE RELATIONSHIP’S DEGREE

Add the relationship degrees to the E-R Diagram.

ExistenceNameOptionalityDegreeValidate

EMPLOYEE DEPARTMENT

assignedto

responsiblefor

ITN 170 - MySQL Database Programming

47

VALIDATE THE RELATIONSHIP

Read the Relationship AloudRelationship must be readable and make

business sense.

ExistenceNameOptionalityDegreeValidate

EMPLOYEE DEPARTMENT

assignedto

responsiblefor

Re-examine the E-R model and validate the relationship.

Each EMPLOYEE must be assigned to one and only one DEPARTMENTEach DEPARTMENT may be responsible for one or more EMPLOYEEs.


Recommended