Top Banner
Lesson 4: The Relational Model
74

Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Jan 19, 2016

Download

Documents

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: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Lesson 4: The Relational Model

Page 2: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Lesson Overview

• Database Design

• Entities

• Relationships

• E-R Diagrams

Page 3: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Database Design

LOGICAL DESIGN

PHYSICAL DESIGN

REQUIREMENTS ANALYSIS

Data Structure Design

Data Structure Refinement

E-R Diagrams

Data Models

Page 4: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Database Design

• The first step in database design is the requirement analysis where database designers interview prospective users to understand and document their data requirements.

Page 5: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Database Design

• Once the requirements have been analysed, the designer creates a conceptual schema for the database to make a graphic representation of the organisations’ data requirements independent of the DBMS and the hardware.

Page 6: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Database Design

• Steps in Database Design involve:• 1. Users’requirements analysis• 2. Creation of the conceptual schema by the

database designers in the logical design.• 2a. Design the Data Structure• 2b. Refine the Data Structure• The conceptual schema is a graphic

representation of the organisations’ data requirements independent of the DBMS and the hardware.

Page 7: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Database Design

• In the first phase of the logical design, i.e. the data structure design, the conceptual schema of the database is developed. This is DBMS-independent.

• One way of representing the logical design is by using the Entity-Relationship (E-R) Diagram

Page 8: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Database Design

• In Data Structure refinement the DBMS-independent logical design is transformed into a data model compatible with the chosen DBMS.

• An Entity is a real world object which exist and is distinguishable from other objects.

• An entity is represented by a set of attributes which are properties that described the entity’s characteristics.

Page 9: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Database Design

• The identifier of an entity instance is one or more attributes.

• This identifier may be unique or not unique.• A unique identifier identifies one and only one,

entity instance.• The unique identifier is known as a superkey. • The superkey can be one attribute or a

combination of attributes.

Page 10: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Entities• An Entity is a real world object

which exist and is distinguishable from other objects.

• We wish to record the data about these objects.

• An entity may be concrete such as a person, a book, a course etc.

Page 11: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Entity

• An entity may also be abstract or a concept such as a holiday, registration, a flight reservation etc.

• An entity set or an entity class is a set of all entities of the same type.

• It is the general form or description.

Page 12: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Entities

• The set of all students registered for a particular course for instance can be defined as the entity set STUDENT.

• The entity set CUSTOMER might represent the set of all persons having an account at a bank.

Page 13: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Entities

• Entity sets are not disjoint.

• For e.g.. we may define the entity set of all the customers of the bank, CUSTOMER, and the entity set of all employees of the bank, EMPLOYEE.

• A Person entity may be an EMPLOYEE entity, a CUSTOMER entity, both or neither.

Page 14: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Entities

• an entity instance is the representation of

a particular entity, e.g. customer with the

name Lim is an instance of the entity

CUSTOMER.

Page 15: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Attributes

• An entity is represented by a set of attributes.

• Attributes are properties which described the entity’s characteristics.

• For the entity employee, possible attributes are EMPLOYEENO, EMPLOYEENAME, DATEOFHIRE.

Page 16: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Attributes

• Attributes can be single or multiple valued, simple or composite, stored or derived.

• For each attribute there is a set of permitted values, called the domain of that attribute.

Page 17: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Attributes

• The domain of the attribute CustomerName may be the set of all text strings of length 50.

Page 18: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

• Simple (atomic)

• Composite

• Single-valued

• Multi-valued

Attribute typesAttribute types

Page 19: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Simple Attributes (Atomic)

• A simple attribute is an attribute that cannot be further divided into its components.

• E.g.. ?

Page 20: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Composite Attribute

• An attribute that can be further subdivided is a composite attribute.

• E.g.?

Page 21: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Single-Valued Attribute

• A single valued attribute can only have one value.

• E.g.An employee can have only one date of birth, one First Name, one Social security number

• An employee can have more than one degree, so EMP_Degree is a multivalued attribute

• Tip: Good database designs ensure that only signle valued attributes are stored in the database.

Page 22: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Identifiers

• Entity instances have names that identify them.

• CUSTOMER has CustomerName or CustomerNO

• STUDENt has Name or StudentNo

• COURSE has CourseName or CourseNo

Page 23: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Identifiers

• The identifier of an entity instance is one or more attributes.

• The identifier may be unique or not unique.

• If it is unique, then, it identifies one and only one, entity instance.

• If it is not unique, the value will identify more than one instance.

Page 24: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Identifiers

CustomerNameCustomerName is not a unique identifier is not a unique identifier because it can produce a set of instances.because it can produce a set of instances.

Two or more customers may have the same Two or more customers may have the same name. name.

If If CustomerNameCustomerName is to be used then is to be used then additional data, such as additional data, such as AddressAddress, , TelephoneNoTelephoneNo or or DateOfBirthDateOfBirth is required to is required to find a unique instance.find a unique instance.

Page 25: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Relationships

• A relationship is an association between entities.

• A relationship set is a set of relationships of the same type.

• Relationship sets are associations between entity sets.

Page 26: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Relationships

• Relationship instances are association between entity instances.

• A relationship can have attributes.

Page 27: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Relationships

STUDENT

CourseCOURSE

Register

Page 28: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Relationships

• Register is a relationship between STUDENT and COURSE

• A possible attribute for Register is Date.

• Relationships have degrees and cardinalities.

• The number of entities in the relationship is the degree of the relationship.

Page 29: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Relationship Degrees : Binary(Degree 2)

LECTURER

STUDENT

Teach

Page 30: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Relationship Degrees: Ternary (Degree 3)

CHILD

MOTHER FATHER

Parent

Page 31: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Relationship Degrees

• In a binary relationship (degree 2) each relationship set involves two entity sets.

• In a ternary relationship each relationship set involves three entity sets.

Page 32: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Binary Relationships :Relationship

Types• There are three types of binary

relationships:

• one-to-one, one-to-many, many-to-one and many-to-many.

• These are the cardinalities of the relationships.

Page 33: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

One-to-one Relationship (1:1)

a1

a2

a3

b1

b2

b3

A B

Page 34: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

One-to-one Relationship (1:1)

• In a one-to-one relationship an entity in A is associated with one and only one entity in B.

• A relationship between a wife and a husband is a1:1 relationship.

WIFE HUSBAND

Marriage

has1 1

Page 35: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

One-to-many Relationship (1:M)

a1

a2

a3

b1

b2

b3

AB

b4

b5

Page 36: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

One-to-many Relationship (1:M)

• An entity in A is associated with any number of entities in B.

• The relationship between Faculty and Students is a 1:M relationship

FACULTY STUDENTEnrols

1 M

Page 37: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Many-to-one Relationship (M:1)

a1

a2

a3

b1

b2

b3

AB

a4

a5

Page 38: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Enrollment

Is-Enrolled

Many-to-one Relationship (M:1)

• An entity A is associated with at most one entity in B. An entity in B , however, can be associated with any number of entities in A.

• The relationship between Student and Faculty is M:1

STUDENT FACULTY

M 1

Page 39: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Many-to-many Relationship (M:M)

a1

a2

a3

b1

b2

b3

A B

Page 40: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Many-to-many Relationship (M:M)

• An entity in A is associated with many entities in B, and an entity in B is associated with many entities in A.

LECTURER STUDENT1:MTeach

M M

Page 41: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Many-to-many Relationship (M:M)

• The relationship between Lecturer and Student is M:M

• In the relational model, M:M relationships are broken into sets of 1:M relationships

LECTURERSTUDENT

LECT-STUD

1

M

1

M

Page 42: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Composite Entity

– Designed to transform an M:N relationship into two 1:M relationships

– Key comprises at least the primary keys of the entities that it connects

Page 43: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Weak Entity Sets• An entity set that does not have a

primary key is referred to as a weak entity set.

• The existance of a weak entity set depends on the existence of a strong entity set; it must relate to the strong set via a on-to-many relationship set.

• A weak entity cannot exist without the related entity’s presence.

• E.g. Dependent is a weak entity

EMPLOYEE DEPENDENTSHas

Page 44: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Weak Entity

• The primary key of a weak entity set is formed by the primary key of the strong entity set on which the weak entity set’s existence depends on.

Page 45: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Optional Relationship

• The relationship between Employee and Dependent is an optional one.

• An employee can have 0 or more dependents.

• Thus there will be cases of particular employee without dependents.

Page 46: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Mandatory Relationships

• The relationship between an EMPLOYEE and DEPARTMENT

• A Department must be occupied by an employee

• An employee must be the occupant of a department.

• 2nd e.g.: An EMPLOYEE must have one EMPLOYMENT_CONTRACT and each EMPLOYMENT _CONTRACT must have one EMPLOYEE associated with it.

Page 47: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Subtype/Supertype Relationship

• The relationship between EMPLOYEE and LECTURER is a 1:1 relationship.

• It is also a subtype/supertype relationship because the LECTURER entity contains attributes that are unique to LECTURERS while the EMPLOYEE entity contains the entity’s common attribute.

Page 48: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Recursive Relationship

• Happens when an entity is related to itself.

COURSE prerequisite

Page 49: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Entity-Relationship(E-R) Diagram

• The overall logical structure of a database is expressed graphically by an Entity-Relationship (E-R) diagram.

• The diagram consists of

• Rectangles which represent entity sets.

• Ellipses which represent attributes.

Page 50: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Entity-Relationship(E-R) Diagram

• Diamonds which represent relationship sets.

• Lines which link attributes to entity sets and entity sets to relationship sets.

Page 51: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Symbols

• Entity

• Relationship

• Composite Entity

• Optionality

CUSTOMER

generates

INV_LINE

DEPENDENT(Weak entity)

Page 52: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

An ERD Segment

CUSTOMER generates INVOICE

1 M

(0,N) (1,1)

cardinalities

connectivities

Business rules: 1. One customer may generate many invoices 2. Each invoice is generated by one customer

3. Some customers have never generated an invoice (The customer list apparently includes potential customers. Therefore, INVOICE is optional to CUSTOMER.)

Page 53: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

An expanded ERD segment

CUSTOMER generates INVOICE

INV_LINEPRODUCT

1 M

(0,N) (1,1)

(O,N) (1,1)

1 M

Page 54: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

1:M Relationships

CUSTOMER generates INVOICE

1 M

(0,N) (1,1)

EMPLOYEE has DEPENDENT

1 M

(0,N) (1,1)

Page 55: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

A supertype/subtype relationship

EMPLOYEE is a PILOT

1 1

(0,1) (1,1)

The supertype contains allcommon attributes such as

names, addresses, etc.

The subtype contains all specializedattributes, such as pilot license,

flight check dates, etc.

Page 56: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

EMPLOYEE DEGREEEMP_DEGREE

1 M

(0,N) (1,1)(1,1)

M 1

Implementing multi-valued attributes

(1,N)

Business rules:Some employees do not have degrees. (Employees are not required to

have a degree.) Therefore, some employees do not occur in the EMP_DEGREE (composite) entity.

A degree is entered only if at least one employee has earned thatdegree. But a degree can be earned by many employees.

Page 57: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

(1,1)

EMPLOYEE

1

(0,N)

(1,1)

MM

(1,1)

M M

(1,1)(1,N)

CUSTOMER

1

1

(1,N)

(0,N)

(0,N)

(1,1)

contracts coordinates

leads

PROJECT

PROJ_EMP

1

M

1

An ERD for a consulting company

Page 58: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

1 M M1

CUSTOMER INVOICE LINE

(0,N) (1,1) (1,1)(1,N)

PRODUCT

generates

M

1

(1,1)

(0,N)

An Invoicing ERD: Composite Entity LINE

Relational Schema

Page 59: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

1 M M1

CUSTOMER INVOICE LINE

(0,N) (1,1) (1,1)(1,N)

PRODUCT

generates

appears in

contains

M

1

(1,1)

(0,N)

An Invoicing ERD: Composite Entity LINEDecomposed Through Named Relationships

Relational Schema

Page 60: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

1 1 M1

EMPLOYEE MECHANIC MAINT_LOG

(0,1) (1,1) (1,1)(1,N)

MAINT_LINE

is a

contains

writes

M

1(1,N)

(1,1)

1 M

CUSTOMER CAR

(0,N) (1,1)

owns

needs

1

M(1,1)

(0,N)

1

PART

(0,N)

is in

(0,1)

M

A Car Maintenance ERD

Page 61: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

1

M

M

1TEAM

GAME

PLAYER(1,N)

(1,1)

(1,1)uses

(1,1)M

(1,N)(1,N)

1

M

CITY

(1,N)

(1,1)sponsors

1 A recursive M:Nrelationship. Implementedthrough the compositeentity GAME. (TEAM plays TEAM in a GAME.)

RelationalSchema

Page 62: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

E-R Diagram for a BANK Database

BANK BRANCH

ACCOUNT

CUSTOMER

LOAN

Name

Code Add Addr

BrNo

LoanNoType Amt

AcctNo

Balance

Type

CustNo

Phone Name Add

ACCTS

A-C

LOANS

BRANCHES

Page 63: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• Steps in Database Design involve:• 1. Users’requirements analysis• 2. Creation of the conceptual schema by

the database designers in the logical design.

• 2a. Design the Data Structure• 2b. Refine the Data Structure• The conceptual schema is a graphic

representation of the organisations’ data requirements independent of the DBMS and the hardware.

Page 64: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• In the first phase of the logical design, i.e. the data structure design, the conceptual schema of the database is developed. This is DBMS-independent.

• One way of representing the logical design is by using the Entity-Relationship (E-R) Diagram.

Page 65: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• In Data Structure refinement the DBMS-independent logical design is transformed into a data model compatible with the chosen DBMS.

• An Entity is a real world object which exist and is distinguishable from other objects.

Page 66: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• An entity is represented by a set of attributes which are properties that described the entity’s characteristics.

• The identifier of an entity instance is one or more attributes.

Page 67: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• This identifier may be unique or not unique.

• A unique identifier identifies one and only one, entity instance.

Page 68: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• The unique identifier is known as a superkey.

• The superkey can be one attribute or a combination of attributes.

Page 69: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• {CustomerName, Address} is a superkey.

• Candidate keys are superkeys such that the subset of that key is not a superkey.

• {CustomerName, Address} is a candidate key.

• {CustomerNo, Address} is a superkey but not a candidate key.

Page 70: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• The candidate key chosen by the database designer as the identifier is called a primary key.

Page 71: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• A relationship is an association between entities.

• A binary relationship (degree 2) each relationship set involves two entity sets.

• binary relationships can be one-to-one, one-to-many, many-to-one and many-to-many.

Page 72: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Summary

• (E-R) diagram consists of

• Rectangles which represent entity sets.

• Ellipses which represent attributes.

• Diamonds which represent relationship sets.

• E-R diagrams are used to represent the logical structure of a database.

Page 73: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Next Lecture Lesson 5: Normalization I

Page 74: Lesson 4: The Relational Model. Lesson Overview Database Design Entities Relationships E-R Diagrams.

Q & A

[email protected]