53
1 Entity - Entity - Relationship Relationship Model Model Chapter 3 Unit II Data Base Management System [DBMS]

Dbms ii mca-ch3-er-model-2013

Embed Size (px)

Citation preview

Page 1: Dbms ii mca-ch3-er-model-2013

1

Entity -Entity -Relationship Relationship

ModelModelChapter 3

Unit II

Data Base Management System[DBMS]

Page 2: Dbms ii mca-ch3-er-model-2013

2

Using High-Level Conceptual Data Models for Database Design

Entity-Relationship (ER) model is a high-level conceptual data model.

overview of the database design process:

1st step - Requirements collection and analysis. - In this step, the database designers interviews database users to understand

and document their data requirements.

- The result of this step is a concisely written.

- These requirements should be specified in detailed and complete a form as possible.

- In parallel with the data requirements, the functional requirements are specified

for the application.

- The user defines the operations (or transactions) that will be applied to the database, including both retrievals and updates.Aruna (DSCASC)

Page 3: Dbms ii mca-ch3-er-model-2013

3

Using High-Level Conceptual Data Models for Database Design (Cont.,)

In software design, it is common to use data flow diagrams, sequence diagrams, scenarios, and other techniques to specify functional requirements.

It is explained in detail in software engineering.

2nd Step - Is to create a conceptual schema for the database, using a

high-level conceptual data model. - This step is called conceptual design.

- The conceptual schema is a concise description of the data requirements of the users and includes detailed descriptions of the entity types, relationships, and constraints.

Aruna (DSCASC)

Page 4: Dbms ii mca-ch3-er-model-2013

4

Using High-Level Conceptual Data Models for Database Design (Cont.,)

3rd step – The database design is the actual implementation of the database, using a commercial DBMS.

- The conceptual schema is transformed from the high-level data model into

the implementation data model.

- This step is called logical design or data model mapping.

4th step - The physical design phase, during which the internal storage

structures, file organizations, indexes, access paths, and physical design parameters for the database files are

specified.

Aruna (DSCASC)

Page 5: Dbms ii mca-ch3-er-model-2013

5

A diagram to illustrate the main phases of database design.

Aruna (DSCASC)

Page 6: Dbms ii mca-ch3-er-model-2013

6

An Example Database Application An example database application, called COMPANY. We list the data requirements for the database. The COMPANY database keeps track of a company’s employees,

departments, and projects.

The Company database:1. The company is organized into departments.

Each department has a unique name, a unique number, and a particular employee who manages the department.

We keep track of the start date when that employee began managing the department.

A department may have several locations.

2. A department controls a number of projects, each of which has a unique name, a unique number, and a single location.

Aruna (DSCASC)

Page 7: Dbms ii mca-ch3-er-model-2013

7

An Example Database Application (Cont.,)

3. We store each employee’s name, Social Security number,2 address, salary, sex (gender), and birth date.

An employee is assigned to one department, but may work on several projects, which are not necessarily controlled by the same department.

We keep track of the current number of hours per week that an employee works on each project.

We also keep track of the direct supervisor of each employee (who is another employee).

4. We want to keep track of the dependents of each employee for insurance purposes.

We keep each dependent’s first name, sex, birth date, and relationship to the employee.

Aruna (DSCASC)

Page 8: Dbms ii mca-ch3-er-model-2013

8

An Example Database Application (Cont.,)

Aruna (DSCASC)

Page 9: Dbms ii mca-ch3-er-model-2013

9

Entity - Relationship Model (E-R Model)

An E-R diagram can express the overall logical structure of a database graphically.

The ER model describes data as:

EntityAttributesRelationship

Aruna (DSCASC) 9

Page 10: Dbms ii mca-ch3-er-model-2013

10Aruna (DSCASC) 10

Entities and Attributes:

Entities are specific objects or things in the world that are represented in the database.

Example: Specific person, employee, company, student, event.

Attributes are properties used to describe an entity. Example: An EMPLOYEE entity may have Name, SSN, Address,

Designation, Salary. A specific entity will have a value for each of its attributes. For example a specific employee entity may have Name='John

Smith', SSN='123456789', Address ='731, Houston, TX', BirthDate='09-JAN-75‘

Each attribute has a value set (or data type) associated with it – e.g. integer, string, …

DefinitionsDefinitions

Page 11: Dbms ii mca-ch3-er-model-2013

11Aruna (DSCASC) 11

EntitiesEntity set is a set of entities of the same type that share the same

properties. Example: set of all bank customers, companies, trees, holidays.

Entity Type defines a set of entities that have the same attributes.

Name of Entity: Employee Company

Attributes: Name, Age, Salary Name, Place

Entity set: E1: Rani, 22, 15000E2: Vinay, 30, 20000

C1: ABC, BangaloreC2: XYZ, Bangalore

Entity type, Attributes, Entity set

Page 12: Dbms ii mca-ch3-er-model-2013

12Aruna (DSCASC) 12

Entity Sets Entity Sets customercustomer and and loanloan

customer customer customer customer loan amount id name street city no.

Page 13: Dbms ii mca-ch3-er-model-2013

13Aruna (DSCASC) 13

Key Attributes of an entity type A key attribute is a minimal set of attributes of an entity set, which

uniquely identifies an entity in an entity set.

An entity type usually has one or more attributes whose values are distinct for each individual entity in the entity set. Such an attribute is called a key attribute

A key may be a single attribute or may be more than one attribute.

Example:

* For the student entity the RegNo can be the key attribute. * For a person entity the SSN can be the key attribute.

Some times key may be formed by the combination of several attributes – a composite attribute. (ie., the combination of the attribute values must be distinct for each individual entity.

Example: The registration no. for a vehicle with two two simple attributes, i.e., state number. and registration number

Page 14: Dbms ii mca-ch3-er-model-2013

14Aruna (DSCASC) 14

Value Sets (Domains) of Attributes

Value Sets (Domains) of Attributes:

A set of values that may be assigned to the attributes of each individual entity, in an entity set is called the value set or domain.

Example:

* For employee entity, if age limit is 20 to 58 then the value set (domain) of attribute age consists of integer from 20 to 58. Age: Domain [ 20 – 58]

* The value set for name attribute is a set of alphabets and some special characters. Name: Domain [a – z], [A – Z], blank space, dot

Page 15: Dbms ii mca-ch3-er-model-2013

15Aruna (DSCASC) 15

Relationship:

A relationship relates two or more distinct entities with a specific meaning. For example: CUSTOMER ‘Jones’ has Loan ‘L-23’ ….

Relationship Set:

Relationships of the same type are grouped or typed into a relationship set / type.

For example: the ‘Borrower’ relationship type in which CUSTOMER and Loan

participate.

The degree of a relationship type is the number of participating entity sets.

Relationships and Relationship Set

Page 16: Dbms ii mca-ch3-er-model-2013

16Aruna (DSCASC) 16

Relationship Set borrower

RelationshipsRelationships

Page 17: Dbms ii mca-ch3-er-model-2013

18Aruna (DSCASC) 18

Refers to number of entity sets that participate in a relationship set.

Relationship sets that involve two entity sets are binary (or degree two). Generally, most relationship sets in a database system are binary.

Relationship sets may involve more than two entity sets.

Relationships between more than two entity sets are rare. Most relationships are binary.

Relationship types of degree 3 are called ternary and of degree n are called n-ary

Degree of a Relationship Set

Page 18: Dbms ii mca-ch3-er-model-2013

19Aruna (DSCASC) 19

An entity is represented by a set of attributes, that is descriptive.

Properties possessed by all members of an entity set.

Example: customer = (customer_id, customer_name,

customer_street, customer_city )

loan = (loan_number, amount )

Domain – the set of permitted values for each attribute.

Attributes

Page 19: Dbms ii mca-ch3-er-model-2013

20Aruna (DSCASC) 20

Attribute types Simple and Composite attributes: If the attributes are not divided into subparts is called simple If the attributes are divided into subparts is called composite. Example: Simple- SSN or Gender composite - Name

Single-valued and Multi-valued attributes:

Example: Single valued - Age Multi-valued attribute - phone-numbers, college degree Employee dependant (1,or 2 or 3)

Derived attributes:

Can be computed from other attributesExample: Age given date of birth. Experience giving date of join

Null Attributes:

Page 20: Dbms ii mca-ch3-er-model-2013

21Aruna (DSCASC) 21

Composite Attributes

If the attributes are not divided into subparts is called simple.

If the attributes are divided into subparts then composite.

Page 21: Dbms ii mca-ch3-er-model-2013

22Aruna (DSCASC) 22

Express the number of entities to which another entity can be associated via a relationship set.

Most useful in describing binary relationship sets.

For a binary relationship set the mapping cardinality must be one of the following types:

One to One One to Many Many to One Many to Many

Mapping Cardinality Constraints

Page 22: Dbms ii mca-ch3-er-model-2013

23Aruna (DSCASC) 23

One to One: An entity in A is associated with at most one entity in B, and

an entity in B is associated with at most one entity in A.One to Many: An entity in A is associated with any number of entities

in B.

An entity in B, however can be associated with at most one entity in A.

Page 23: Dbms ii mca-ch3-er-model-2013

24Aruna (DSCASC) 24

Many to One: An entity in A is associated with at most one entity in B, and an

entity in B is associated with any number of entities in A.Many to Many: An entity in A is associated with any number of entities

in

B. and n entity in B, is associated with any number of entities in A.

Page 24: Dbms ii mca-ch3-er-model-2013

25

Basic E-R Notation

Page 25: Dbms ii mca-ch3-er-model-2013

26

Mapping cardinality symbols

Aruna (DSCASC) 26

R

R

R

many-to-many relationship

many-to-one relationship

one-to-one relationship

Page 26: Dbms ii mca-ch3-er-model-2013

27

Summary of Notation for ER Diagrams

In ER diagrams the emphasis is on representing the schemas rather than the instances.

This is more useful in database design because a database schema changes rarely, whereas the contents of the entity sets change frequently.

The Example COMPANY ER database schema as an ER diagram.

ER diagram notation: Entity types such as EMPLOYEE, DEPARTMENT, and PROJECT are

shown in rectangular boxes.

Relationship types such as WORKS_FOR, MANAGES, CONTROLS, and WORKS_ON are shown in diamond-shaped boxes attached to the participating entity types with straight lines.

Page 27: Dbms ii mca-ch3-er-model-2013

28

Summary of Notation for ER Diagrams (cont.,)

Attributes are shown in ovals, and each attribute is attached by a straight line to its entity type or relationship type.

Composite attribute are attached to the oval representing the composite attribute, as illustrated by the Name attribute of EMPLOYEE.

Multi valued attributes are shown in double ovals, as illustrated by the Locations attribute of DEPARTMENT.

Key attributes have their names underlined.

Derived attributes are shown in dotted ovals, as illustrated by the Number_of_employees attribute of DEPARTMENT.

Page 28: Dbms ii mca-ch3-er-model-2013

29

Summary of Notation for ER Diagrams (cont.,)

Weak entity types are distinguished by being placed in double rectangles and by having their identifying relationship placed in double diamonds, as illustrated by the DEPENDENT entity type and the DEPENDENTS_OF identifying relationship type.

The partial key of the weak entity type is underlined with a dotted line.

The cardinality ratio of each binary relationship type is specified by attaching a 1, M, or N on each participating edge.

The cardinality ratio of DEPARTMENT:EMPLOYEE in MANAGES is 1:1, whereas it is 1:N for DEPARTMENT: EMPLOYEE in WORKS_FOR, and M:N for WORKS_ON.

The participation constraint is specified by a single line for partial participation and by double lines for total participation.

Page 29: Dbms ii mca-ch3-er-model-2013

30

Summary of Notation for ER Diagrams (cont.,)

The role names for the SUPERVISION relationship type because the same EMPLOYEE entity type plays two distinct roles in that relationship.

Notice that the cardinality ratio is 1:N from supervisor to supervisee because each employee in the role of supervisee has at most one direct supervisor, whereas an employee in the role of supervisor can supervise zero or more employees.

Example - Slide 8

Page 30: Dbms ii mca-ch3-er-model-2013

31

Proper Naming of Schema Constructs

When designing a database schema, the choice of names for entity types, attributes, relationship types, and (particularly) roles is not always straightforward.

Choose names that convey, as much as possible, the meanings attached to the different constructs in the schema.

Choose a singular names for entity types, rather than plural ones.

In the ER diagrams, we should use the convention that entity type and relationship type names are uppercase letters, attribute names have their initial letter capitalized, and role names are lowercase letters.

Choose binary relationship names to make the ER diagram of the schema readable from left to right and from top to bottom.

Example: Company Database.

Page 31: Dbms ii mca-ch3-er-model-2013

32Aruna (DSCASC) 32

Entity Set with a multi valued attribute, derived attribute & Primary Key

Derived from date employed and current date

Primary Key

Multivalued: an employee can have more than one skill

Primary Key: An attribute or group of attributes that uniquely identifies each row in a relation.

Page 32: Dbms ii mca-ch3-er-model-2013

33Aruna (DSCASC) 33

E-R Diagram With Composite, Multi valued, and Derived Attributes

• Rectangles represent entity sets.

• Diamonds represent relationship sets.

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

• Ellipses represent attributes

• Double ellipses represent multi valued attributes.

• Dashed ellipses denote derived attributes.

• Underline indicates primary key attribute.

Page 33: Dbms ii mca-ch3-er-model-2013

34Aruna (DSCASC) 34

E-R Diagrams

Page 34: Dbms ii mca-ch3-er-model-2013

35Aruna (DSCASC) 35

Relationship Sets with Attributes

Page 35: Dbms ii mca-ch3-er-model-2013

36Aruna (DSCASC) 36

One-To-Many Relationship

Many-To-One Relationship

Page 36: Dbms ii mca-ch3-er-model-2013

37Aruna (DSCASC) 37

Many-To-Many Relationship

One-To-One Relationship

Page 37: Dbms ii mca-ch3-er-model-2013

38

Relationship Types of Degree Higher than Two

Page 38: Dbms ii mca-ch3-er-model-2013

39Aruna (DSCASC) 39

E-R Diagram with a Ternary Relationship

We allow at most one arrow out of a ternary (or greater degree) relationship to indicate a cardinality constraint.

E.g. an arrow from works_on to job indicates each employee works on at most one job at any branch.

Page 39: Dbms ii mca-ch3-er-model-2013

41Aruna (DSCASC) 41

An entity set that does not have a primary key is referred to as a weak entity set.

The existence of a weak entity set depends on the existence of a Strong entity set

Identifying weak entity set is represented with double-line rectangle. Identifying relationship is the relationship between Strong and weak entity

sets and it is represented by double diamond.

The discriminator (or partial key) of a weak entity set is the set of attributes that distinguishes among all the entities of a weak entity set.

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 is existence dependent, plus the weak entity set’s discriminator.

Weak Entity & Strong Entity SetsWeak Entity & Strong Entity Sets

Page 40: Dbms ii mca-ch3-er-model-2013

42Aruna (DSCASC) 42

Strong VS Weak Entities, and Identifying RelationshipsStrong entities:

Exist independently of other types of entitiesHas its own unique identifierRepresented with single-line rectangle

Weak entity:Dependent on a strong entity…cannot exist on its ownDoes not have a unique identifierRepresented with double-line rectangle

Identifying relationship:Links strong entities to weak entitiesRepresented with double line diamond

Page 41: Dbms ii mca-ch3-er-model-2013

43Aruna (DSCASC) 43

We represent weak entity sets by double rectangles. We underline the discriminator of a weak entity set with a

dashed line.

payment_number – discriminator of the payment entity set Primary key for payment – (loan_number, payment_number)

Identifying relationship

Page 42: Dbms ii mca-ch3-er-model-2013

44Aruna (DSCASC) 44

Strong entity Weak entityIdentifying relationship

Identifying Relationship

Page 43: Dbms ii mca-ch3-er-model-2013

45Aruna (DSCASC) 45

Entity sets of a relationship need not be distinct The labels “manager” and “worker” are called roles;

they specify how employee entities interact via the works_for relationship set.

Roles are indicated in E-R diagrams by labeling the lines that connect diamonds to rectangles.

Roles

Page 44: Dbms ii mca-ch3-er-model-2013

47

ER diagram college

Page 45: Dbms ii mca-ch3-er-model-2013

48

E-R Diagram – Hospital with patients and doctors and log of tests

Aruna (DSCASC) 48

Name

Appointment Patient Doctor

Patient-id

Name

ph doctor-id Address

Salary

Phone No

Address

P-D Test

Test-name Price

Test-date

Page 46: Dbms ii mca-ch3-er-model-2013

49Aruna (DSCASC) 49

E-R Diagram for a Banking Enterprise

Page 47: Dbms ii mca-ch3-er-model-2013

50Aruna (DSCASC) 50

E-R Diagram for books purchase

Page 48: Dbms ii mca-ch3-er-model-2013

51Aruna (DSCASC) 51

E-R Diagram

Page 49: Dbms ii mca-ch3-er-model-2013

52

E-R Diagram for Airlines reservation

Page 50: Dbms ii mca-ch3-er-model-2013

53

E-R Diagram forElectronic Distributor and Reseller Network Administration

Page 51: Dbms ii mca-ch3-er-model-2013

54Aruna (DSCASC) 54

Specialization and Generalization

Extended E-R featuresSpecializationGeneralization

Page 52: Dbms ii mca-ch3-er-model-2013

55

Specialization:

An entity set may include sub groupings of entities that are distinct in some way from other entities. (i.e a subset of entities within an entity set may have attributes that are not shared by all entities in the entity set.)

Generalization: Generalization is an abstraction mechanism, we can think of the

reverse of the specialization process. Several classes with common features are generalized into a super

class; original classes become its subclasses. Example: CAR, TRUCK generalized into VEHICLE; both CAR,

TRUCK become subclasses of the super class VEHICLE. We can view {CAR, TRUCK} as a specialization of VEHICLE Alternatively, we can view VEHICLE as a generalization of CAR

and TRUCK Aruna (DSCASC) 55

Specialization and Generalization

Page 53: Dbms ii mca-ch3-er-model-2013

56Aruna (DSCASC) 56

ACCOUNT

account-no balance

SAVINGS ACCOUNT CURRENT ACCOUNT

High level entity

low level entity

interest rateoverdraft amount

STANDARD

GOLD

SENIOR

num checks

interest payment

date of birth

min balance

ISA

ISA

Savings A/C & Current A/C are specialization of Account

A/C as Generalization of S A/C & C A/C