Sd2052 - Lect 3 and 4 Entity Modelling 2010

25
SD2052 - Database Systems 1 Review A data model is a (relatively) simple abstraction of a complex real-world data environment Basic data modeling components are: Entities Attributes Relationships Constraints Importance of Business Rules Relational model Current database implementation standard ER model is a popular graphical tool for data modeling that complements the relational model Data modeling requirements are a function of different data views (global vs. local) and level of data abstraction: External, Conceptual, Internal SD2052 - DATABASE SYSTEMS LECTURE 3 and 4: ENTITY RELATIONAL MODELLING Source: Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Transcript of Sd2052 - Lect 3 and 4 Entity Modelling 2010

Page 1: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 1

ReviewA data model is a (relatively) simple abstraction of a complex real-world data environmentBasic data modeling components are:

EntitiesAttributesRelationshipsConstraints

Importance of Business RulesRelational model

Current database implementation standardER model is a popular graphical tool for data modeling that complements the relational model

Data modeling requirements are a function of different data views (global vs. local) and level of data abstraction: External, Conceptual, Internal

SD2052 -

DATABASE SYSTEMS

LECTURE 3 and 4:

ENTITY RELATIONAL

MODELLING

Source: Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Page 2: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 3

Learning Outcome

That the relational database model takes a logical view of data

The main characteristics of entity relationship components

How ERD components affect database design and implementation

The relational model’s basic components are relations implemented through tables in a relational DBMS

How relations are organised in tables composed of rows (tuples) and columns (attributes)

How relationships between entities are defined and refined and how those relationships are incorporated into the database design process

SD2052 - Database Systems 4

Entity Relationship Modelling

ER (Entity-Relational) model developed by Peter Chen in 1976 to aid database design. Used for conceptual design.

ER diagrams give a visual indication of the design. It is a diagrammatic technique used to analyse the nature of application system

Page 3: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 5

The Importance of Data Models

Data models

Relatively simple representations, usually graphical, of complex real-world data structures

Facilitate interaction among the designer, the applications programmer, and the end user

End-users have different views and needs for data

Data model organises data for various users

SD2052 - Database Systems 6

Business Rules

Brief, precise, and unambiguous descriptions of policies, procedures, or principles within a specific organisation

Apply to any organisation that stores and uses data to generate information

Description of operations that help to create and enforce actions within that organisation’s environment

Must be easy to understand and widely disseminated

Describe characteristics of the data as viewed by the company

Page 4: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 7

Basic Characteristics of ER Data Model

Entity (Table):

Anything about which data are to be collected and stored, eg, customer, student, products (generally nouns)

Attribute (Column Headings/Field):

Characteristic or properties of an entity, eg, name, date of birth, description, Employee_Name, Student_Name, etc

Relationship:

Describes an association/relationship (generally verbs)among entities

One-to-many (1:M) relationship

Many-to-many (M:N or M:M) relationship

One-to-one (1:1) relationship

Constraint:

A restriction placed on the data. Eg, a student can only enroll on one course.

SD2052 - Database Systems 8

Examples of ER Modelling Notations

Page 5: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 9

Case Study: Tiny College

Business Rules:A professor may teach many classes, however a class can only be taught by only one professor.

A class may enrol many students, and each student may enrol in many classes.

Each course may generate many classes, but each class references a single course.

Finally, a class requires one room, but a room may be scheduled for many classes.

SD2052 - Database Systems 10

Example of Crow’s feet ER Model

Entity

Degree of relationship

Relationship name Optionality/ cardinality

NOTE: You should be able to ‘read’ an E-R model in sentence form. e.g “A professor

teaches many class(es)” and “A class can only be taught by one professor”. The subject and object of the sentence are entities (nouns) and the relationship is the verb. Inverse relationship names should be shown.

Page 6: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 11

Attributes

An attribute is a property or characteristic of an entity that is of interest to the organisation.

Eg, Date_of_Birth, Student_Name, Years_Employed, Product_Description, etc

Attributes represents column headings/fields in a relational table

The domain of an attribute is the set of allowable values for the attribute.

Eg the domain of the attribute, “Student_Name” may be “Diana”, “Charles”, etc

SD2052 - Database Systems 12

Attributes - Keys

Primary key (PK) is an attribute (or a combination of attributes) that uniquely identifies any given entity (row):

Primary Key/Superkey (PK)

Any key that uniquely identifies each row

Foreign key (FK)

An attribute whose values match primary key values in its original table

Composite key

Composed of more than one attribute

Candidate key

A superkey without redundancies

Page 7: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 13

Composite Key

Composite Key:Composite entity/table must contain at least the must contain at least the primary keys of original tablesprimary keys of original tables

Linking entity/table contains multiple occurrences of the foreign key values. Must be repeated in table

Student Enroll ClassST_ID ST_ID CL_IDCL_ID

ST_ID CL_ID Date_Enroll

S_01 C_04 17-Jan-09

S_01 C_07 17-Jan-09

S_20 C_04 17-Jan-09

S_01 C_15 17-Jan-09

SD2052 - Database Systems 14

Example of Keys

Primary Key

Primary Key

Foreign Key

Page 8: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 15

Relationship Degree

Indicates number of entities or participants associated with a relationship

Binary relationship

Two entities are associated

Ternary relationship

Three entities are associated

Unary relationship

Association is maintained within single entity

SD2052 - Database Systems 16

One-to-Many Relationships

1:M relationship (Binary relationship)

Relational modeling ideal

Should be the norm in any relational database design

(Binary relationship) - Two entities are associated

The Primary Key from the “1” relationship must be included in

the ”M” relationship.

Page 9: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 17

22 7799

Course Classhas

has

One-to-Many Relationships

Instance of an entity

Think of instances as records/rows

in a table

Instance of

an entity

SD2052 - Database Systems 18

One-to-Many Relationships

Page 10: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 19

One-to-One Relationships

1:1 relationshipShould be rare in any relational database design

One entity can be related to only one other entity, and vice versaSometimes means that entity components were not defined properlyCould indicate that two entities actually belong in the same tableAs rare as 1:1 relationships should be, certain conditions absolutely require their use

Where would you put the Foreign Key?

SD2052 - Database Systems 20

1:1 Relationship (Table format)

A Foreign Key must be included in one of the entities/tables that are related base on business rule.

Page 11: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 21

Many-to-Many Relationships

M:N relationshipsCannot be implemented as such in the relational model

M:N relationships can be changed into two 1:M relationships

Can avoid problems inherent to M:N relationship by creating a composite entity or bridge entity

Where would you put the Foreign Key?

SD2052 - Database Systems 22

Data Redundancy

Data redundancy leads to data anomalies:

Such anomalies can destroy the effectiveness of the database

Insertion: Occur when entering new records

Deletion: Occur when deleting records

Modifications/update: Occur when changes must be made to existing records.

Foreign keys

Control data redundancies by using common attributes shared by tables

Crucial to exercising data redundancy control

Page 12: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 23

The M:N Relationship (contd)

How many classes is Bowser taking?

You must have only single valued data for each attribute.

SD2052 - Database Systems 24

The M:N Relationship (contd)

Page 13: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 25

The M:N Relationship (contd)

To resolve the M:N relationship:

Must convert the M:N to 1:M

Include a Linker (composite)

entity/table between entities

Composite entity/table must contain at least the primary keys of original tables

Linking entity/table contains multiple occurrences of the foreign key values

Additional attributes may be assigned as needed

SD2052 - Database Systems 26

The M:N Relationship (contd)

Linker

Entity/Table

Transfer both “M”

relationship symbol to the Linker entity/table

Page 14: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 27

The M:N Relationship (contd)

Composite

Keys

Foreign Key values

are being repeated for

each record

SD2052 - Database Systems 28

The M:N Relationship (contd)

Where would you put the Foreign Keys?

Page 15: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 29

The M:N Relationship (contd)

SD2052 - Database Systems 30

Connectivity and Cardinality

Connectivity/Optionality

Used to describe the relationship classification

Cardinality

Expresses minimum and maximum number of entity occurrences associated with one occurrence of related entity

Established by very concise statements known as business rules

Page 16: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 31

Connectivity and Cardinality (contd)

SD2052 - Database Systems 32

Relationship Participation (contd)

Page 17: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 33

Relationship Participation

Optional participation

One entity occurrence does not require corresponding entity occurrence in particular relationship

Mandatory participation

One entity occurrence requires corresponding entity occurrence in particular relationship

OptionalMandatory

SD2052 - Database Systems 34

Relationship Participation (contd)

Page 18: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 35

Unusual Relationships: Ternary relationship

3-Way

Linker

SD2052 - Database Systems 36

Unusual Relationships: Ternary relationship

Page 19: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 37

Unusual Relationships: Unary relationship (Recursive)

Relationship can exist between occurrences of the same entity set

Naturally found within unary relationship

SD2052 - Database Systems 38

Recursive Relationships (contd)

Page 20: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 39

Recursive Relationships (contd)

SD2052 - Database Systems 40

Unusual Relationship (contd)

Student

Lecturer

Tutored by

tutors

S#

L#

L#

Taught b

y

teaches

Student taught by “M” Lecturer – Lecturer teaches “M” student

Student tutored by one lecturer – Lecturer tutors “M” student

Page 21: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 41

Case Study: Tiny College (Recap)

Business Rules:A professor may teach many classes, however a class can only be taught by only one professor.

A class may enrol many students, and each student may enrol in many classes.

Each course may generate many classes, but each class references a single course.

Finally, a class requires one room, but a room may be scheduled for many classes.

SD2052 - Database Systems 42

Tiny College ER Modelling/Diagram

Page 22: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 43

ER Modelling - stepsNOTE: (this is how I do it)

First of all, identify the enterprise which is = TINY COLLEGE (YOU DO NOT MODEL THE ENTERPRISE)

Secondly, read through the scenario and identify the entities (remember what an entity is !!)

Third, read through again and try to establish the relationships between the entities.

Fourth, go through the scenario again and try to identify attributes of entities. This way, it is highly possible that some of the entities that may have been identified will actually come out as attributes by the time you are through.

CAUTION: Do not read too much into the scenario and try to incorporate things that are not RELEVANT to the Application Domain but which may be CONVENIENT for you.

SD2052 - Database Systems 44

RULES AND TIPS IN ER Modelling/DIAGRAM

1. One to One Relationships:For each binary 1:1 relationship choose the entity with total participation and include the Primary key of the other in that entity as a foreign key

2. One to Many Relationships:For each 1:M relationship - move the Primary key from the entity on the 1 side to the entity on the M(many) side where it will become a foreign key.

3. Many to Many Relationships:For each M:M relationship – break it up with a new entity called a linker. Combine the Primary keys from the original tables which then become a COMPOSITE KEY (in the linker table).

Page 23: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 45

Assumptions

Assumptions are very important in ER diagramming

ER modelling is highly subjective. It is similar to painting a picture. The artists perception is what is painted.

In ER modelling assumptions can be made by the modeller with regards to the Application domain, although whatever assumption made must fit within the Application domain.

Assumptions are made to highlight those important facts that are not so obvious, they are ambiguous,but influences the identification of entities, relationships or attributes.

SD2052 - Database Systems 46

Assumptions (Contd)

Example:

Draw an entity-relationship diagram for database showing fatherhood, motherhood and spouse relationships among men and women.

ER 1 for scenario 1 ER 1 for scenario 2

Husband Wife

Child

has

has

Husband/

Father

Wife/

Mother

Child

has

has

Page 24: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 47

Assumptions

For scenario 1:In Western cultures Each man marries one and only one wife at a particular time.In Western cultures Each woman marries one husband at a particular time.Each child belongs one parent (i.e. if the Application Domain has a specific restriction. Eg. If modelling relationship between only biological parents)

For scenario 2:In other cultures a man can marry one or more wives at a particular time.In other cultures a child can belong to one or more parents (i.e. if the Application Domain does not state any specific restriction)

SD2052 - Database Systems 57

Integrity Rules

Page 25: Sd2052 - Lect 3 and 4 Entity Modelling 2010

SD2052 - Database Systems 72

THINKTHINKand be

POSITIVEPOSITIVEalways!