30
Data Modeling and Entity-Relationship Model I IST210 1

Data Modeling and Entity- Relationship Model I IST2101

Embed Size (px)

Citation preview

Page 1: Data Modeling and Entity- Relationship Model I IST2101

IST210 1

Data Modeling and Entity-Relationship Model I

Page 2: Data Modeling and Entity- Relationship Model I IST2101

IST210 2

Question: Where do relations come from?

Suppose you are now designing database for a company, how should you systematically turn user requirements into actual relations?

Page 3: Data Modeling and Entity- Relationship Model I IST2101

IST210 3

I want a database to maintain departments in my company. Store

information about my employees, their projects and assignments. I want ….

$$$

User requirement

A database

YOUR JOB!

Page 4: Data Modeling and Entity- Relationship Model I IST2101

IST210 4

Use Normalization Process to Design a Database?

• One Approach1. List all data attributes that will be involved– OwnerID, OwnerName, OwnerEmail, PropertyID, PropertyName,

PropertyAddress

2. Develop functional dependencies– OwnerID (OwnerName, OwnerEmail) – PropertyID (PropertyName, PropertAddress, OwnerID, …)

3. Normalization process

Page 5: Data Modeling and Entity- Relationship Model I IST2101

What if we have many attributes?

• Sample attributes in user requirements:– DepartmentName, BudgetCode, OfficeNumber, Phone,

EmplyeeNumber, FirstName, LastName, Department, Phone, Email, ProjectID, ProjectName, Department, MaxHours, StartDate, EndData, ProjectID, EmployeeNumber, HoursWorded…

• Problems of this (top-down) approach– Lack of a systematic approach to identify all

relevant attributes– Entities and relationships are mixed together

Page 6: Data Modeling and Entity- Relationship Model I IST2101

IST210 6

I want a database to maintain departments in my company. Store

information about my employees, their projects and assignments. I want ….

$$$

User requirement

A database

YOUR JOB!

Data Modeling (Ch.4)

Database Design (Ch.5)

Page 7: Data Modeling and Entity- Relationship Model I IST2101

IST210 7

A Two-Step (Bottom-Up) Approach: Step 1

• Input: User requirement; Output: E-R Diagram• Use Entity-Relationship Diagram (E-R Diagram) to capture all user

requirements

Data Modeling (Ch.4)

Database Design (Ch.5)

Page 8: Data Modeling and Entity- Relationship Model I IST2101

IST210 8

A Two-Step (Bottom-Up) Approach: Step 2

Data Modeling (Ch.4)

Database Design (Ch.5)

• Input: E-R diagram; Output: A database• Transferring a data model to a relational database

• Entities Relations• Relationships Foreign keys and extra

Page 9: Data Modeling and Entity- Relationship Model I IST2101

IST210 9

Three Stages of Database Development

State 1. Requirements Analysis

Stage 3. Database Design

Stage 2. Entity-Relationship ModelWhy can’t we skip stage 2?

E-R model is a prototype of actual database system.The prototypes are used to obtain feedback from system users * Logical Stage

* Physical Stage

Page 10: Data Modeling and Entity- Relationship Model I IST2101

IST210 10

Three Stages of Database Development

• Requirements Analysis Stage– Users are interviewed– Obtain sample forms, reports, queries, …

• Component Design Stage– Create a data model– Data model: a presentation of the content, relationships, and

constraints of the data needed to support the requirements• Implementation Stage

– Data model transformed to database design– Database design: tables, relationships, and constraints– Database constructed and filled with data; queries, forms, and reports

are created; application programs are written; and all these are tested– Users trained, documentation written, system installed for use

Page 11: Data Modeling and Entity- Relationship Model I IST2101

IST210 11

The Requirements Stage

• Sources of requirements:– User Interviews– Forms– Reports– Queries– Business Rules– Use Cases

• Stories/Scenarios• Important for validating the data model, design, and

implementation

Page 12: Data Modeling and Entity- Relationship Model I IST2101

IST210 12

Requirements Become theE-R Data Model

• After the requirements have been gathered, they are transformed into an Entity Relationship (E-R) Data Model

• E-R Models consist of– Entities– Attributes– Identifiers– Relationships

Page 13: Data Modeling and Entity- Relationship Model I IST2101

IST210 13

Component 1: Entities

• An entity or entity class is something that users want to track– Customer, Order, Product

• An entity instance is a specific occurrence of an entity class– A customer John, an order 10021, a product

A42300

Page 14: Data Modeling and Entity- Relationship Model I IST2101

IST210 14

Component 2: Attributes

• An entity has attributes that describe the entity’s characteristics– ProjectName– StartDate– ProjectType– ProjectDescription

• Each attribute has a data type and properties.– Data type: string, number, date, …– Property: default value, value range, …

Page 15: Data Modeling and Entity- Relationship Model I IST2101

IST210 15

Component 3: Identifiers• Entity instances have identifiers• An identifier will identify a particular instance

in the entity class– SocialSecurityNumber– StudentID– EmployeeID

• Identifier can be considered as the primary key in a relation

Page 16: Data Modeling and Entity- Relationship Model I IST2101

IST210 16

Draw an Entity

Entity Name

Attributes

Identifier

Page 17: Data Modeling and Entity- Relationship Model I IST2101

IST210 17

In-Class Exercise A-1: Entity

I want a course registration database. Store information about my students, including

their IDs, names, and emails. A course should have information about course ID,

course name, and instructor name.

1. How many entities do you need?2. Draw a diagram for each entity using the presentation in the previous slide

Page 18: Data Modeling and Entity- Relationship Model I IST2101

IST210 18

In-Class Exercise B-1: Entity

I am the manager for apartment rental office. I want a database to maintain all my apartments. Each apartment building will have a building ID,

name, address, city, state, and zip code. A building have many apartments. An apartment have an

apartment number, number of bedrooms, number of bathrooms, and monthly rent cost.

1. How many entities do you need?2. Draw a diagram for each entity

Page 19: Data Modeling and Entity- Relationship Model I IST2101

IST210 19

Component 4: Relationships

• Entities can be associated with one another in relationships

• Relationship degree defines the number of entity classes participating in the relationship– Degree 2 is a binary relationship

• Most relationships we study in this course will be binary relationship

– Degree 3 is a ternary relationship – Degree 1 is a recursive (unary) relationship

Page 20: Data Modeling and Entity- Relationship Model I IST2101

IST210 20

Degree 2 Relationship: Binary

VIDEO CLIP

USER

Create/Be Created

CLASS

STUDENT

Register

Page 21: Data Modeling and Entity- Relationship Model I IST2101

IST210 21

Degree 3 Relationship: Ternary

VIDEO CLIP

USER COMMENT

Page 22: Data Modeling and Entity- Relationship Model I IST2101

IST210 22

Degree 1 Relationship: Recursive/Unary

USER Friending

Page 23: Data Modeling and Entity- Relationship Model I IST2101

IST210 23

Binary Relationship Cardinality

• Relationships are named and classified by their cardinality, which is a word that means count– Maximum Cardinality

• One-One, One-Many, Many-Many

– Minimum Cardinality• Optional, Mandatory

Page 24: Data Modeling and Entity- Relationship Model I IST2101

IST210 24

Maximum Cardinality

• maximum cardinality is the maximum number of entity instances that may participate in a relationship instance — one, many or other some fixed number– BASKETBALL-TEAM and PLAYER could be 1:5

Page 25: Data Modeling and Entity- Relationship Model I IST2101

IST210 25

One-to-One Binary Relationship

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

related to a single entity instance in another entity class

• An employee may have no more than one locker; and • A locker may only be accessible by one employee

Page 26: Data Modeling and Entity- Relationship Model I IST2101

IST210 26

One-to-Many Binary Relationship

• 1:N (one-to-many)– A single entity instance in one entity class is related to

many entity instances in another entity class • A video in YouTube is uploaded by one user; and • A user may upload several videos.

USER VIDEO1:N

UPLOAD

Page 27: Data Modeling and Entity- Relationship Model I IST2101

IST210 27

Many-to-Many Binary Relationship

• N:M (many-to-many)– Many entity instances in one entity class is related to many

entity instances in another entity class • A YouTube user may subscribe several channels; and• A particular channel may be subscribed by several users.

USER CHANNELN:M

SUBSCRIBE

Page 28: Data Modeling and Entity- Relationship Model I IST2101

IST210 28

Maximum Cardinality: Crow’s Foot Notation

USER VIDEO

USER CHANNEL

EMPLOYEE LOCKER

One Many (crow’s foot)

Attention: do not misplace one and many notations

An employee has (at most) one locker; a locker can belong to (at most) one employee

A user can upload many videos; a video can be uploaded by (at most) one user

A user can subscribe to many channels; a channel can be subscribed by many users

upload

subscribe

Page 29: Data Modeling and Entity- Relationship Model I IST2101

IST210 29

How to Determine Maximum Cardinality?

USER VIDEO

USER CHANNEL

EMPLOYEE LOCKER

Attention: do not misplace one and many notations

An employee has (at most) one locker; a locker can belong to (at most) one employee

A user can upload many videos; a video can be uploaded by (at most) one user

A user can subscribe to many channels; a channel can be subscribed by many users

upload

subscribe

• User requirements• Common Sense

Page 30: Data Modeling and Entity- Relationship Model I IST2101

IST210 30

In-Class Exercise: Maximum Cardinality

In-Class Exercise A-2• Draw a line between STUDENT and COURSE• Draw the Maximum Cardinality on the line using

Crow’s foot notation

In-Class Exercise B-2• Draw a line between BUILDING and APARTMENT• Draw the Maximum Cardinality on the line using

Crow’s foot notation