2mis6010-l databases and spreadsheets

21
1 2mis6010-L 2mis6010-L Databases and Databases and Spreadsheets Spreadsheets Lecturer Lecturer Nishant Pithia Nishant Pithia

Upload: idania

Post on 13-Jan-2016

33 views

Category:

Documents


3 download

DESCRIPTION

2mis6010-L Databases and Spreadsheets. Lecturer Nishant Pithia. Week 2: Database Design. Lecture : a gentle introduction to database design, using last week practical exercises as an example Practical Session : Database Design with E-R Diagram. Database Design. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 2mis6010-L  Databases and Spreadsheets

11

2mis6010-L 2mis6010-L Databases and Databases and SpreadsheetsSpreadsheets

LecturerLecturer

Nishant PithiaNishant Pithia

Page 2: 2mis6010-L  Databases and Spreadsheets

22

Week 2: Database DesignWeek 2: Database Design

LectureLecture: a gentle introduction to : a gentle introduction to database design, using last week database design, using last week practical exercises as an examplepractical exercises as an example

Practical SessionPractical Session: Database Design : Database Design with E-R Diagramwith E-R Diagram

Page 3: 2mis6010-L  Databases and Spreadsheets

33

Database DesignDatabase Design

DB Design is the process of creating DB Design is the process of creating a design for a database that will a design for a database that will support the enterprise’s operations and support the enterprise’s operations and

objectives objectives and meet the enterprise’s information and meet the enterprise’s information

needs. needs.

Standard Approaches for DB Design:Standard Approaches for DB Design:E-R Diagram E-R Diagram NormalizationNormalization

Page 4: 2mis6010-L  Databases and Spreadsheets

44

Database Design – E-R Diagram Database Design – E-R Diagram (ERD)(ERD)

Entity-Relationship (ER) Modeling is a Entity-Relationship (ER) Modeling is a conceptual modeling technique to identify conceptual modeling technique to identify the entities and the relationship between the the entities and the relationship between the entities for relational database design.entities for relational database design.

Elements of ERD:Elements of ERD:1.1. Entity:Entity: a group of real life objects (such as a group of real life objects (such as

people, products, and so on) with the same people, products, and so on) with the same properties. properties.

In Ms Access, an entity is used to represent a In Ms Access, an entity is used to represent a table.table.

2. Attribute:2. Attribute: a property of an entity. a property of an entity.

In Ms Access, attributes are referred to those In Ms Access, attributes are referred to those fields/columns in the tablesfields/columns in the tables. .

Page 5: 2mis6010-L  Databases and Spreadsheets

55

Elements of ERD –Elements of ERD –Primary Key and Entity IntegrityPrimary Key and Entity Integrity

Elements of ERD:Elements of ERD:3.3. Primary Key:Primary Key: the attribute used to uniquely identify each occ the attribute used to uniquely identify each occ

urrence of an entity. urrence of an entity. In Ms Access, PK is the key attribute as an ID to uniquely identifIn Ms Access, PK is the key attribute as an ID to uniquely identif

y data for each row/entry.y data for each row/entry. For every table, there must be PK(s) For every table, there must be PK(s) to identify each row of data. to identify each row of data.

Entity Integrity: An Integrity Constraint of DB setting followingEntity Integrity: An Integrity Constraint of DB setting followingrulesrules

The Existence of PK valueThe Existence of PK value: PK can not be null value: PK can not be null value (i.e. mu (i.e. must contain some value). st contain some value). A A null valuesnull values is an unknown value. is an unknown value.

The Uniqueness of PK Value: The Uniqueness of PK Value: the value of PK for each row the value of PK for each row must be differentmust be different..

Otherwise, you will violate the entity integrity of the database. Otherwise, you will violate the entity integrity of the database.

For standard database design, these essential elements canFor standard database design, these essential elements canbe presented in E-R diagrambe presented in E-R diagram

Page 6: 2mis6010-L  Databases and Spreadsheets

66

Entity and Attribute Entity and Attribute for Last week Exercisefor Last week Exercise

For example,For example,

Customer as an entity having attributes, such as No, Customer as an entity having attributes, such as No, name, address; Customer No is the PK for Customer name, address; Customer No is the PK for Customer table.table.

Product as an entity having attributes, such as No, Product as an entity having attributes, such as No, name; Product No is the PK for Customer table.name; Product No is the PK for Customer table.

Order as an entity having attributes, such as No, Order as an entity having attributes, such as No, name; Product No is the PK for Customer table.name; Product No is the PK for Customer table.

No

Customer

name

Address

Order

No Name

Product

NoName

Description

Page 7: 2mis6010-L  Databases and Spreadsheets

77

Example of Entity Integrity and Example of Entity Integrity and PKPK

CustomerNo(PK)

Name Address

123 Anderson …………

089 Smith …………

89 Jones …………

334 Jones …………

OrderNoOrderNo(PK)(PK)

OrderDetails

O123 ………..

O124 …………

O233 ………….

ProductNoProductNo(PK)(PK)

Name Description

A001 ……… …………………..

A002 ……….. …………………

A003 ……….. …………..………

For each entity with attributes, you will have For each entity with attributes, you will have following proposed table view in mind following proposed table view in mind (not the real (not the real database).database).

Page 8: 2mis6010-L  Databases and Spreadsheets

88

ERD Element - RelationshipERD Element - Relationship

For relational database, We use “relationship” For relational database, We use “relationship” asas

an element to link different entities togetheran element to link different entities togetherinstead of isolating them from one another.instead of isolating them from one another. Relationship: a set of meaningful association aRelationship: a set of meaningful association a

mong entities. mong entities. Different types of association between entitiesDifferent types of association between entities

One-to-oneOne-to-one One-to-many (or Many-to-one)One-to-many (or Many-to-one) Many-to-manyMany-to-many

Page 9: 2mis6010-L  Databases and Spreadsheets

99

Types of Relationship with Types of Relationship with examplesexamples

One-to-one: the One-to-one: the head of a Department head of a Department manages the department.manages the department.

Head Department

Each Head can only manage one department,Each Head can only manage one department,each department can be only managed byeach department can be only managed byone head. one head.

managesmanages

Page 10: 2mis6010-L  Databases and Spreadsheets

1010

Types of Relationship with Types of Relationship with examplesexamples

One to Many (or Many-to-one)One to Many (or Many-to-one)

Department Course

Each department has one or more courses;Each department has one or more courses;each course can be only run in a particulareach course can be only run in a particulardepartment. department.

ITPhysics

Arts

Msc ITBsc IT

Bsc Physics BA ArtsMA Arts

HasHas

Is_Run_InIs_Run_In

Page 11: 2mis6010-L  Databases and Spreadsheets

1111

Types of Relationship with Types of Relationship with examplesexamples

Many-to-ManyMany-to-Many

Department Course

Each department has one or more courses;Each department has one or more courses;each course can be run in more than oneeach course can be run in more than onedepartment. department.

ITPhysics

Arts

Msc ITBsc Physics and IT

BA Arts With IT BA Arts

HasHas

Is_Run_InIs_Run_In

Page 12: 2mis6010-L  Databases and Spreadsheets

1212

Some Tips for Database DesignSome Tips for Database DesignStep 1:Identify the entities with attributes and Step 1:Identify the entities with attributes and

the the Relationships among entitiesRelationships among entities

Make assumptions, by clearly understanding the Make assumptions, by clearly understanding the scenario/background information for your scenario/background information for your database design.database design.

((Different assumptions will bring your totally Different assumptions will bring your totally different DB design!!!)different DB design!!!)

Map entities with attributes and Find the Map entities with attributes and Find the “direct “direct relationship”relationship” between different entities. between different entities.

(must have very clear logic)(must have very clear logic) Clearly identify the types of relationshipClearly identify the types of relationship

Step 2: Use some rules to check your designStep 2: Use some rules to check your designStep 3: Realize the Relationships in table viewStep 3: Realize the Relationships in table view

Find and create Foreign Key (the matching field) Find and create Foreign Key (the matching field) between the entities (tables) as the link to between the entities (tables) as the link to realize the relationship realize the relationship

Step 4: Normalize the Tables.Step 4: Normalize the Tables.

Page 13: 2mis6010-L  Databases and Spreadsheets

1313

Step 1 for Last Week Step 1 for Last Week Exercises Exercises

Customer OrderProduct

CustomerNo (PK)

Name

123 Anderson

089 Smith

89 Jones

334 Jones

OrderNo(PKOrderNo(PK))

OrderDetails

O123 ………..

O124 …………

O233 ………….

ProductNo(PK) Name Description

A001 …... …………………..

A002 …… …………………

A003 ……. …………..………

EntityEntityViewViewwith with AttributesAttributes

ProposedProposedTableTableView withView withPK UnderPK UnderEntityEntityIntegrityIntegrity

Think AboutThink About: Any Assumption for this example? What are the : Any Assumption for this example? What are the relationships among entities? Where are the direct relationships?relationships among entities? Where are the direct relationships?

Page 14: 2mis6010-L  Databases and Spreadsheets

1414

Step 1 Step 1 for Last Week Exercises for Last Week Exercises Under Assumption 1Under Assumption 1

AndersonCooper

King

O214O315O266O478O122

D021B001M746B111A222C122U120U129

One Customer has one or morOne Customer has one or more orders; each order can only e orders; each order can only belong to a particular custombelong to a particular customerer (different customers’ names can not appear in the same order)

An Order can have more than one An Order can have more than one product; each product can be only product; each product can be only belong to a particular customer’s belong to a particular customer’s order order (bespoken or personalized products. e.g. Rolls Royce; designer branded fashion for celebrities)

Customer Order Product

Assumption 1

Conceptual View

ER View

Page 15: 2mis6010-L  Databases and Spreadsheets

1515

Step 1 for Last Week Exercises Step 1 for Last Week Exercises Under Assumption 2Under Assumption 2

One Customer has one or morOne Customer has one or more orders; each order can only e orders; each order can only belong to a particular custombelong to a particular customerer (different customers’ names can not appear in the same order)

One Order can contain more than one One Order can contain more than one product; product; each product can appear in each product can appear in different orders. (e.g. consumer different orders. (e.g. consumer products. e.g. clothes in shopping products. e.g. clothes in shopping centre; books, etc… )centre; books, etc… )

Assumption 2

Conceptual View

ER View

AndersonCooper

King

O214O315O266O478O122

D021B001

B111

C122U120U129

Customer Order Product

Page 16: 2mis6010-L  Databases and Spreadsheets

1616

Step 2: Some General Rules Step 2: Some General Rules for Good DB Designfor Good DB Design

For multiple 1:M (or M:1) relationships For multiple 1:M (or M:1) relationships Try to avoid such fan shape connection for databTry to avoid such fan shape connection for datab

ase design in ER Viewase design in ER View

Customer

Order Product

Problem: What are the products in the order??Problem: What are the products in the order?? Fan shape “trap” will exist when the direct relationship is Fan shape “trap” will exist when the direct relationship is

incorrectly identifiedincorrectly identifiedIn our example (Slide 14 & 15), under different assumptions,In our example (Slide 14 & 15), under different assumptions,

both DB Design in ER View do not have fan shape.both DB Design in ER View do not have fan shape.

Page 17: 2mis6010-L  Databases and Spreadsheets

1717

Step 3 – Finding and Create Foreign Key Step 3 – Finding and Create Foreign Key for Relationship in Table Viewfor Relationship in Table View

Foreign Key (FK)Foreign Key (FK) is is an attribute/a an attribute/a field of one table and a primary key of field of one table and a primary key of another table. It is another table. It is the matching fieldthe matching field between interrelated tables. between interrelated tables.

The use of FK introduces another new intThe use of FK introduces another new integrity constraint - egrity constraint - Referential IntegrityReferential Integrity (or FK constraint). (or FK constraint). It simply sets a rule that: It simply sets a rule that: if a FK exists in a taif a FK exists in a ta

ble, the value of the FK must match the valuble, the value of the FK must match the value of the primary key of some row in another e of the primary key of some row in another tabletable. .

Page 18: 2mis6010-L  Databases and Spreadsheets

1818

Example of FK and RIExample of FK and RIDept_ID Name

D01 IT

D02 Arts

Course_Code

Name

C01 Msc IT

C02 Bsc IT

C03 BA Arts

• Which is the PK to be created as a FK in another table for relationship? Course_Code or Dept_ID?

• Dept_ID (the PK of department) in Course table as the FK to link and represent the relationship between them.

Dept ID Name

D01 IT

D02 Arts

Course_Code

Dept_ID Name

C01 D01 Msc IT

C02 D01 Bsc IT

C03 D02 BA Arts

Page 19: 2mis6010-L  Databases and Spreadsheets

1919

Step 3 for Last Week Exercise Step 3 for Last Week Exercise - Under Assumption 1- Under Assumption 1

CustomerNo Name OrderNo(FKOrderNo(FK))

123 Anderson O123

123 Anderson O124

089 Smith ………..

89 Jones ………

334 Jones ……….

OrderNoOrderNo OrderDetails

ProductNo(FK)

O123 ……….

A001

O124 ……

O233 …….. A002

O233 ……….

A003

ProductNo Product_details

A001 …….

A002 ……

A003 ….....

CustomerNo Name …….....

123 Anderson ………

089 Smith ………..

89 Jones ………

334 Jones ……….

OrderNoOrderNo OrderDetails

O123 ………..

O124 ……

O233 ……

ProductNo name ……

A001 ……. …….

A002 …… ……

A003 …..... ….....

A004 ………. …….

Violate Entity Integrity (No Duplication for PK value)Violate Entity Integrity (No Duplication for PK value)

Page 20: 2mis6010-L  Databases and Spreadsheets

2020

Step 3 Finding FK for Relationship Step 3 Finding FK for Relationship – Under Assumption 1– Under Assumption 1

CustomerNo Name …….....

123 Anderson ………

089 Smith ………..

89 Jones ………

334 Jones ……….

OrderNoOrderNo OrderDetails

O123 ………..

O124 ……

O233 ……

ProductNo name ……

A001 ……. …….

A002 …… ……

A003 …..... ….....

A004 ………. …….

CustomerNo Name …….....

123 Anderson ………

089 Smith ………..

89 Jones ………

334 Jones ……….

OrderNoOrderNo Custome_No(FK)

OrderDetails

O123 123 ………..

O124 123 ……

O233 89 ……

ProductNo OrderNo(FK) Name ……

A001 O123 ……… …….

A002 O233 ……… ……

A003 O233 …….. ….....

Page 21: 2mis6010-L  Databases and Spreadsheets

2121

Answer from Assumption 1 Answer from Assumption 1