Topic 03 & 04 : Entity Relationship Modelling

73
Er. Pradip Kharbuja Topic 3 & 4 Entity Relationship Modelling

description

Entity Relationship Modelling

Transcript of Topic 03 & 04 : Entity Relationship Modelling

Page 1: Topic 03 & 04 : Entity Relationship Modelling

Er. Pradip Kharbuja

Topic 3 & 4Entity Relationship Modelling

Page 2: Topic 03 & 04 : Entity Relationship Modelling

Entity Relationship(ER) Diagram

We will learn to develop ER Diagram in this topic.

Page 3: Topic 03 & 04 : Entity Relationship Modelling

Entity

An entity can be defined as anything, which can be uniquely identified and about which data is stored in a database.

A place, person, picture, concept (e.g. course, position, job) are the example of an entity.

Some other examples can be student, workers, car, departments, etc.

Page 4: Topic 03 & 04 : Entity Relationship Modelling

Entity Type

An entity type is a collection of similar entities.

An entity-type is a category.

An entity type is a group of objects, people, concepts or other items that have the same set of properties or attributes.

Page 5: Topic 03 & 04 : Entity Relationship Modelling

Entity Relationship(ER) Modelling

In 1976, Peter Chen first proposed modeling databases using a graphical technique that humans can relate easily.

An Entity–Relationship (ER) model is an abstract or conceptual way of describing a database

An ER Model is based on a perception of a real world that consists of collection of basic objects called entities and relationships among these objects.

Page 6: Topic 03 & 04 : Entity Relationship Modelling

Database Schema

The overall design or structure of the database is called the database schema.

Three types:

1. Internal Schema

2. Conceptual Schema

3. External Schema

Page 7: Topic 03 & 04 : Entity Relationship Modelling

Types of Schema

1. Internal Schema

describes the physical storage structure of database.

2. Conceptual Schema

hides the details of physical storage structures and concentrates on describing entities, data types, relationships, operations and constraints

3. External Schema

describes a subset of the database according to the format the user wants and hides the rest.

Page 8: Topic 03 & 04 : Entity Relationship Modelling

Data Independence

The ability to modify schema definition in one level without affecting schema definition in the next higher level is called data independence.

There are two levels of data independence.

1. Physical data independence

2. Logical data independence.

Page 9: Topic 03 & 04 : Entity Relationship Modelling

Physical Data Independence

Physical data independence means the capacity to change the internal schema or physical storage without affecting conceptual or external schema.

Internal schema may be changed to improve the performance.

Page 10: Topic 03 & 04 : Entity Relationship Modelling

Logical Data Independence

Logical data independence is the ability to modify the logical or conceptual schema without affecting to external schema or application program.

Logical Data independence means if we add some new columns or remove some columns from table then the user view and programs should not change.

Page 11: Topic 03 & 04 : Entity Relationship Modelling

Entity Relationship(ER) Modelling

The ER model achieves a high degree of data independence which means that the database designer do not have to worry about the physical structure of the database.

The goal of ER Modelling is to produce a model of data that is non-technical and free of ambiguities

Page 12: Topic 03 & 04 : Entity Relationship Modelling

Entity Relationship(ER) Diagram

ER diagram is a graphical modelling to standardize ER modelling.

The modeling can be carried out with the help of pictorial representation of entities, attributes, and relationships.

Page 13: Topic 03 & 04 : Entity Relationship Modelling

Attributes

An attribute is a property of an entity type.

The data that we want to keep about each entity within an entity type is contained in attributes.

Examples of Attributes :

1. Roll number, name and grade are the attributes of Student.

2. Brand, cost and weight are the attributes of Cellphone.

3. Book name, price, author, publisher, etc. are attributes of Book.

Page 14: Topic 03 & 04 : Entity Relationship Modelling

Types of Attributes

1. Single-valued attribute

2. Multi-valued attribute

3. Null Attribute

4. Simple Attribute

5. Composite Attribute

6. Key Attribute

7. Derived Attribute

8. Attribute Domain

Page 15: Topic 03 & 04 : Entity Relationship Modelling

Types of Attributes

1. Single-valued attribute : Attribute that holds a single value for an occurrence of an entity type. E.g. age, first_name, last_name, full_name, etc.

2. Multi-valued attribute :Attribute where there might be more than one value for a given occurrence of an entity type, e.g. for the 'Telephone Number', where a person or company might have many of these.

3. Null attribute :Attribute that has NULL.

Page 16: Topic 03 & 04 : Entity Relationship Modelling

Types of Attributes

4. Simple AttributeIt is composed of a single component. e.g. age, first_name, last_name, etc. But full_name is not simple attribute. Because it is composed of first_name and last_name.

5. Composite AttributeIt is composed of more than one component. For example, 'address' might have city, state, country, zip code, etc.

Page 17: Topic 03 & 04 : Entity Relationship Modelling

Types of Attributes

6. Key Attribute

Primary Key is a key attribute.

Not Null and Unique attribute of an entity

At most one attribute in each entity

7. Derived Attribute

The value of the derived attribute can be derived from the values of other attributes

Age of a person can be derived from the date of birth and current date i.e. age = current date – date of birth.

Page 18: Topic 03 & 04 : Entity Relationship Modelling

Types of Attributes

8. Attribute Domain

A domain is the set of allowable values for an attribute or number of attributes.

A domain therefore limits the values that an attribute can have.

For example, the domain of 'gender' would include the values 'Male' and 'Female'.

Similarly, the domain of fruit would include the values 'Apple', 'Orange' etc.

Page 19: Topic 03 & 04 : Entity Relationship Modelling

Relationship

A relationship is an association of entities showing how the entities are related with each other.

The examples of relationship are:

Teaching is the relationship between Lecturer and Student.

Buying is the relationship between Vendor and Customer.

Treatment is the relationship between Doctor and Patient.

Page 20: Topic 03 & 04 : Entity Relationship Modelling

Types of Relationship or Multiplicity

Number or range of possible occurrences of an entity type in relation to another entity type

1. One-to-One (1 : 1)

2. One-to-Many (1 : N)

3. Many-to-One (N : 1)

4. Many-to-Many (M : N)

Page 21: Topic 03 & 04 : Entity Relationship Modelling

Types of Relationship or Multiplicity

1. One-to -One (1 : 1)

Each department can have only one department head.

Each worker works in one department.

2. One-to-Many (1 : N)

One course can have many students registered.

A lecturer may be teaching to many students.

Page 22: Topic 03 & 04 : Entity Relationship Modelling

Types of Relationship or Multiplicity

3. Many-to-One (N : 1)

Many cities come under one country.

Many books can be written by one writer.

Many authors can write one book.

4. Many-to-Many (M : N)

An Author can write several Books, and a Book can be written by several Authors

A teacher teaches many class and a class studies from many teachers.

Page 23: Topic 03 & 04 : Entity Relationship Modelling

ER Diagram Notation (CHEN)

Represents an entity set

Represents an attribute

Represent linking of attributes to entity sets and of entity sets to relationship sets.

Represents a relationship

Page 24: Topic 03 & 04 : Entity Relationship Modelling

ER Diagram

Page 25: Topic 03 & 04 : Entity Relationship Modelling

ER Diagram

Draw an ER Diagram for ClassRoom which has two attributes : room_no and building_name.

Page 26: Topic 03 & 04 : Entity Relationship Modelling

ER Diagram

Page 27: Topic 03 & 04 : Entity Relationship Modelling

Task : Draw ER Diagram in MS-Visio

Page 28: Topic 03 & 04 : Entity Relationship Modelling

Multiplicity

A teacher teaches only one subject.

A teacher teaches many subjects.

Page 29: Topic 03 & 04 : Entity Relationship Modelling

Strong entity

Strong entity is one whose existence does not depend on other entity.

Weak entity

Weak entity is an entity that depends on another entity. Weak entity doesn't have key attribute of their own.

Associative or composite entity

Associative entity is used to create many-to-many relationship between different entities

Classification of Entity Sets (CHEN)

Entity sets can be broadly classified into:

Page 30: Topic 03 & 04 : Entity Relationship Modelling

Classification of Entity Sets (CHEN)

Consider the example, student takes course. Here student is a strong entity.

Course is considered as weak entity because, if there are no students to take a particular course, then that course cannot be offered. The Course entity depends on the student entity.

Page 31: Topic 03 & 04 : Entity Relationship Modelling

Associative or Composite entity

The database relational model does not offer direct support to many-to-many relationships. So, we require associative or composite entity.

Consider the example, customer purchases book.

A customer can purchase many books. And a book can be purchased by many customers. So, here purchases is an associative entity. The entity purchases is mediator for customer and book.

Page 32: Topic 03 & 04 : Entity Relationship Modelling

Attribute Notation (CHEN)

Single Valued Attribute

Multi-valued Attribute

Key Attribute

Derived Attribute

Composite Attribute

Page 33: Topic 03 & 04 : Entity Relationship Modelling

An employee has first name, last name and can have more than one qualifications.

Multi-valued Attribute

Page 34: Topic 03 & 04 : Entity Relationship Modelling

Key Attribute

Primary Key

A department has Primary Key dept_no, department_name and location

Page 35: Topic 03 & 04 : Entity Relationship Modelling

Derived Attribute

The value of the derived attribute can be derived from the values of other related attributes.

Age of a person can be derived from the date of birth and current date i.e. age = current date – date of birth. In this example, age is the derived attribute.

Page 36: Topic 03 & 04 : Entity Relationship Modelling

Composite Attribute

Composite attribute is one which can be further subdivided into simple attributes.

Page 37: Topic 03 & 04 : Entity Relationship Modelling

Task : ER Diagram

1. Draw an ER diagram for student.

2. Draw an ER diagram for customers and loan relation.

3. Draw an ER diagram for hospital with a set of patients and medical doctors.

4. Draw an ER diagram for banking system.

5. Draw an ER diagram for car insurance company.

Page 38: Topic 03 & 04 : Entity Relationship Modelling

Relationship Degree

Relationship degree refers to the number of associated entities in a relationship.

The relationship degree can be broadly classified into

1. Unary(Recursive) relationship

2. Binary relationship

3. Ternary relationship

Page 39: Topic 03 & 04 : Entity Relationship Modelling

Unary(Recursive) relationship

The number of associated entity is one.

An entity related to itself is known as recursive relationship.

Page 40: Topic 03 & 04 : Entity Relationship Modelling

Binary relationship

In a binary relationship, two entities are involved.

Consider the example: each department will have many workers.

Page 41: Topic 03 & 04 : Entity Relationship Modelling

Ternary relationship

In a ternary relationship, three entities are simultaneously involved.

Example : Consider a project is assigned to many employee. And the project is assigned to one location.

Page 42: Topic 03 & 04 : Entity Relationship Modelling

ER Diagram Notation (CROW'S FEET)

Entity Name

attribute namePK

attribute name

attribute name

Entity Name

Entity Relationship

Illustrates an association between two entities. It has a name which is a verb. It also has cardinality.

is assigned to

Page 43: Topic 03 & 04 : Entity Relationship Modelling

ER Diagram Notation (CROW'S FEET)

zero or more

one or more

one and only one

zero or 1

Cardinality Cardinality refers to the maximum number of times an instance in one entity can be associated with instances in the related entity. It can be zero or one or more.

Page 44: Topic 03 & 04 : Entity Relationship Modelling

Some examples (CROW'S FEET)

courseteacherteaches

coursestudenttakes

Page 45: Topic 03 & 04 : Entity Relationship Modelling

ER Diagram Notation (UML)

A Plane Ticket System

A passenger may book many tickets.

Each ticket is for one flight and one passenger.

A flight has many passenger.

FlightTicketPassenger10..N0..N1

Page 46: Topic 03 & 04 : Entity Relationship Modelling

Many-to-Many Relationship

Module having many Students and Students taking many Modules

Taken ByModule Student0..N 0..N

Attributes

ModuleID(PK)

ModuleName

Attributes

StudentID(PK)

StudentName

Page 47: Topic 03 & 04 : Entity Relationship Modelling

Many-to-Many Relationship

Module Student0..N 0..N

ModuleStudent1 1

Attributes

ModuleID(PK)

ModuleName

Attributes

StudentID(PK)

StudentName

Attributes

ModuleID(FK)

StudentID(FK)

Page 48: Topic 03 & 04 : Entity Relationship Modelling

ER Diagram Notation (UML)

A Book is written by one or more authors. An author writes one or more Books. A Category may have zero or many books.

[Note : Many to Many Relationship cannot be shown in relational model]

1..N

AuthorBook AuthorBook11..N1

Category

1

0..N

Page 49: Topic 03 & 04 : Entity Relationship Modelling

A Gardening Company

The company hires out workers as they need to.

Workers in the town stay in lodgings. A lodging may have many

workers in it. We are only concerned with the workers’ current

lodging. Workers possess certain skills such as ‘Tree Surgery’ and

‘Garden Design’. They may have more than one skill.

[Note : Try to identify nouns that could possibly be entities.]

Page 50: Topic 03 & 04 : Entity Relationship Modelling

A Gardening Company - Nouns

The company hires out workers as they need to.

Workers in the town stay in lodgings. A lodging may have many

workers in it. We are only concerned with the workers’ current

lodging. Workers possess certain skills such as ‘Tree Surgery’ and

‘Garden Design’. They may have more than one skill.

[Note : We are only concerned with one town. So, town is not entity here. But 'Town' is an attribute of the Lodging.]

Page 51: Topic 03 & 04 : Entity Relationship Modelling

Possible Solution

0..*SkillWorker SkillWorker

10..*1

Lodging

0..*

1

Page 52: Topic 03 & 04 : Entity Relationship Modelling

Primary Key

The PRIMARY KEY constraint uniquely identifies each record in a database table.

Primary keys must contain unique values.

A primary key column cannot contain NULL values.

Each table should have a primary key, and each table can have only ONE primary key.

departments

------------------------

dept_no (PK)

department_name

location

Page 53: Topic 03 & 04 : Entity Relationship Modelling

Foreign Key

A FOREIGN KEY in one table points to a PRIMARY KEY in another table

A FOREIGN KEY is a column or a combination of columns that is used to establish and enforce a link between two tables or entities.

departments

------------------------

dept_no (PK)

department_name

location

workers

------------------

emp_no (PK)

first_name

last_name

age

job_title

dept_no (FK)

Page 54: Topic 03 & 04 : Entity Relationship Modelling

Creating Foreign Key

Page 55: Topic 03 & 04 : Entity Relationship Modelling

A Problem

A campus may have one or many staffs. Also a campus may have one or many department.

DepartmentStaff Campus1 1 1..*1..*

Page 56: Topic 03 & 04 : Entity Relationship Modelling

A Problem

Campus ID Campus Name

C1 Softwarica

C2 Islington

C3 The British College

Staff ID Name Campus ID (FK)

S1 Eric Hudson C2

S2 Sandra Smith C1

S3 Malia Shah C1

Department ID Name Campus ID (FK)

D1 Humanities C1

D2 Computing C2

D3 Maths C2

Campus

Department

Staff

Now the question is :

In which Department does

Eric work?

Page 57: Topic 03 & 04 : Entity Relationship Modelling

The Solution

CampusStaff Department1 1..* 11..*

The solution is to adopt a different structure where we have a Campus having one or more Departments which in turn have one or more Staff.

Page 58: Topic 03 & 04 : Entity Relationship Modelling

The Solution

Campus ID Campus Name

C1 Softwarica

C2 Islington

C3 The British College

Staff ID Name Department ID (FK)

S1 Eric Hudson D3

S2 Sandra Smith D1

S3 Malia Shah D1

Department ID Name Campus ID (FK)

D1 Humanities C1

D2 Computing C2

D3 Maths C2

Campus

Department

Staff

Now do you know in which

Department does Eric work?

Page 59: Topic 03 & 04 : Entity Relationship Modelling

Presentation

Fan Traps

Chasm Traps

Pronunciation /ˈkaz(ə)m/

Page 60: Topic 03 & 04 : Entity Relationship Modelling

Problems with ER Models

There are several problems that may arise when designing a ER model. These are known as connection traps.

There are two main types of connection traps:

1. Fan traps

2. Chasm traps

Page 61: Topic 03 & 04 : Entity Relationship Modelling

Fan Traps

Fan traps occur in a situation when a model represents relationship between entity types however a path between certain entity occurrences is ambiguous.

Example: (Staff)-1:N-works in-1-(Campus)-1-has-1:N-(Department).

In this model, it may be impossible to determine the department a staff belongs to.

DepartmentStaff Campus1 1 1..*1..*

Page 62: Topic 03 & 04 : Entity Relationship Modelling

Fan Traps – The Solution

Restructuring the model resolves trap (Campus)-1-has-1:N-(Department)-1-has-1:N-(Staff)

CampusStaff Department1 1..* 11..*

Page 63: Topic 03 & 04 : Entity Relationship Modelling

Chasm Traps

Chasm Traps occur where there are relationships between entities, but one of the relationships is non-mandatory.

A model suggests the existence of a relationship between entity types, but the pathway does not exist between certain entity occurrences. This connection traps is known as Chasm Traps.

http://db.grussell.org/section005.html

Page 64: Topic 03 & 04 : Entity Relationship Modelling

Chasm Traps – The Problem

A branch has many staff members who manage properties, but not all properties are managed by a member of staff, and not all staff manage properties.

Staff PropertyBranch1...* 0...*0...11

Page 65: Topic 03 & 04 : Entity Relationship Modelling

Chasm Traps – The Problem

Branch ID Branch Name

1 Ilford

2 Redbridge

Staff ID Name Branch ID (FK)

S1 Davinda 1

S2 Roberta 1

S3 Eddie 2

Property ID Property Name Staff ID (FK)

P1 Yap Mansions S1

P2 Hill House

P3 Usher House S2

Branch Staff

Property

Because Hill House is not managed by a member of Staff, we do not know from which Branch that Property is managed.

Which Branch manages‘Hill House’?

Page 66: Topic 03 & 04 : Entity Relationship Modelling

Chasm Traps – The Solution

The solution is to change the structure and represent both relationships.

Property

Staff

Branch

1...* 0...*0...11

1 0...*

Page 67: Topic 03 & 04 : Entity Relationship Modelling

Chasm Traps – The Solution

Branch ID Branch Name

1 Ilford

2 Redbridge

Staff ID Name Branch ID (FK)

S1 Davinda 1

S2 Roberta 1

S3 Eddie 2

Property ID Property Name Staff ID (FK) Branch ID (FK)

P1 Yap Mansions S1 1

P2 Hill House 2

P3 Usher House S2 1

Branch Staff

Property

We now know which Branch manages ‘Hill House’

Page 68: Topic 03 & 04 : Entity Relationship Modelling

Draw the ER : Car Hire Company

Company has a number of models of car available for hire.

Customers hire cars and this is recorded against a particular car rather than a model, as the company might have several cars of the same model.

When a customer wants to reserve a particular model they can do so. This is recorded against a model rather than the car.

When a car of that model becomes available, it will be held for the customer.

When they come in for it, a record is made on the reservation to say which car satisfied the reservation.

Page 69: Topic 03 & 04 : Entity Relationship Modelling

Draw the ER : Car Hire Company

Company has a number of models of car available for hire.

Customers hire cars and this is recorded against a particular car rather than a model, as the company might have several cars of the same model.

When a customer wants to reserve a particular model they can do so. This is recorded against a model rather than the car.

When a car of that model becomes available, it will be held for the customer.

When they come in for it, a record is made on the reservation to say which car satisfied the reservation.

Page 70: Topic 03 & 04 : Entity Relationship Modelling

Car Hire Company

Model

HireCar

Reservation

Customer

0..N

1

0..N 11 0..N

0..N 1

1

0..N

1 0..N

Page 71: Topic 03 & 04 : Entity Relationship Modelling

Task

1. A customer records systems for a mail order beauty products company. A customer is assigned to one and only one geographical region. A customer may be interested in a number of different product lines. Any particular product line belongs to one product category that may contain many product lines.

2. A boat is rented to a customer for a set period of time. Any damage to the boat is recorded for that particular rental.

3. The shop needs to keep track of rentals. A member can rent films. A film can be rented by many members. A film can be rented by the same member more than once.

Page 72: Topic 03 & 04 : Entity Relationship Modelling

References

http://www.studytonight.com/dbms/er-diagram.php

http://www.blackwasp.co.uk/SQLDerivedTables.aspx

http://www2.cs.uregina.ca/~bernatja/crowsfoot.html

http://revj.sourceforge.net/traps.html

http://db.grussell.org/section005.html

http://www.tutorialspoint.com/dbms/dbms_data_independence.htm

http://en.wikipedia.org/wiki/Three_schema_approach

http://en.wikipedia.org/wiki/Data_independence

Page 73: Topic 03 & 04 : Entity Relationship Modelling

END OF TOPIC 3 & 4

Any Questions