44
UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

Embed Size (px)

Citation preview

Page 1: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 1

DATABASE MANAGEMENT SYSTEM[DBMS]

2620003 [Unit: 2]

Prepared ByLavlesh Pandit

SPCE MCA, Visnagar

Page 2: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 2

The Entity-Relationship (E-R) data model perceives the real world as consisting of basic objects, called entities, and relationships among these objects.

An entity-relationship (ER) diagram is a specialized graphic that illustrates the interrelationships between entities in a database.

It was developed to facilitate database design by allowing specification of an enterprise schema, which represents the overall logical structure of a database.

It Represent of the meaning of the data.

ER diagrams often use symbols to represent Information

Entity-Relationship Model

Page 3: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 3

2.1 Basic Concepts

•The E-R data model employs three basic notions:

I. Entity setsII AttributesIII Relationship sets

2.1.1 Entity Sets

•An Entity is a “thing” or “object” in the real world that is distinguishable from all other objects.

•Individual objects are called entities

•Groups of the same type of objects are called entity types or entity sets

•Entities are represented by rectangles

Page 4: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 4

Attributes

•An attribute is a specification that defines a property of an object.

•Each attribute can have any value from its domain.

•Attributes can be simple or composite

•Having single-valued or multi-valued

Fname

STUDENT

ADD_1

Page 5: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 5

An attribute, as used in the E-R model, can be characterized by the following attribute types.

•Simple and composite attributes

•Single-valued and Multivalued attributes

•Derived attribute

•Attributes have been simple; that is, they are not divided into subparts.

Simple Attributes

Page 6: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 6

Composite Attributes

• Composite attributes, on the other hand, can be divided into subparts.

•For example, an attribute name could be structured as a composite attribute consisting of first-name, middle-initial, and last-name.

•Composite attributes help us to group together related attributes, making the modeling cleaner.

Page 7: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 7

Page 8: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 8

Single-valued Attributes

•The attributes in our examples all have a single value for a particular entity.

•For instance, the loan-number attribute for a specific loan entity refers to only one loan number.

•An employee‘s social security number might be the employee's key attribute.

•Such attributes are said to be single valued and key attributes.

Attribute

Page 9: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 9

•A Multivalued attribute can have more than one value.

•For example, an employee entity can have multiple skill values.

•Consider an employee entity set with the attribute phone-number. An employee may have zero, one, or several phone numbers.

Multivalued attributes

Attribute

Page 10: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 10

Derived attribute

•The value for this type of attribute can be derived from the values of other related attributes or entities.

•A derived attribute is based on another attribute.

•For example: an employee's monthly salary is based on the employee's annual salary.

Page 11: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 11

Relationship Sets

•The relationship is the interaction between the entities.

•For example, we can define a relationship that associates customer Gitesh with loan no. L-15, This relationship specifies that Gitesh is a customer with loan number L-15.

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

•Consider the two entity sets loan and branch. We can define the relationship set loan-branch to denote the association between a bank loan and the branch in which that loan is maintained.

Page 12: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 12

Constraints

There are two types of constraints

1. Mapping cardinalities 2. Participation constraints

Page 13: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 13

•Mapping cardinalities, or cardinality ratios, express the number of entities to which•another entity can be associated via a relationship set.

•Mapping cardinality must be one of the following

I. 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.

Mapping Cardinalities

Page 14: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 14

II. One to many: An entity in A is associated with any number (zero or more) of entities in B. An entity in B, however, can be associated with at most one entity in A.

Page 15: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 15

III. Many to one

An entity in A is associated with at most one entity in B. An entity in B, however, can be associated with any number (zero or more) of entities in A.

Page 16: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 16

An entity in A is associated with any number (zero or more) of entities in B, and an entity in B is associated with any number (zero or more) of entities in A.

IV. Many to many:

Page 17: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 17

Participation Constraints

•The participation of an entity set E in a relationship set R is said to be total if every entity in E participates in at least one relationship in R.

•If only some entities in E participate in relationships in R, the participation of entity set E in relationship R is said to be partial.

Page 18: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 18

Keys

•No two entities in an entity set are allowed to have exactly the same value for all attributes.

•A key allows us to identify a set of attributes that suffice to distinguish entities from each other.

• Keys also help uniquely identify relationships, and thus distinguish relationships from each other.

Page 19: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 19

Types of Keys

I. Super KeyII. Candidate keyIII. Primary keyIV. Unique KeyV. Foreign Key

Super key is a set of one or more than one keys that can be used to identify a record uniquely in a table.

Example : Primary key, Unique key, Alternate key are subset of Super Keys.

I. Super Key

Page 20: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 20

II. Candidate Key

•A Candidate Key is a set of one or more fields/columns that can identify a record uniquely in a table. There can be multiple Candidate Keys in one table. Each Candidate Key can work as Primary Key.

•Example: In below diagram ID, RollNo and EnrollNo are Candidate Keys since all these three fields can be work as Primary Key.

Page 21: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 21

•Primary key is a set of one or more fields/columns of a table that uniquely identify a record in database table.

•It can not accept null, duplicate values. Only one Candidate Key can be Primary Key.

III. Primary Key

•Uniquekey is a set of one or more fields/columns of a table that uniquely identify a record in database table.

•It is like Primary key but it can not accept null value and it can not have duplicate values.  

IV. Unique Key

Page 22: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 22

•Foreign Key is a field in database table that is Primary key in another table.

•Example : We can have a DeptID column in the Employee table which is pointing to DeptID column in a department table where it a primary key.

V. Foreign Key

Page 23: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 23

Practically in database, we have only three types of keys Primary Key, Unique Key and Foreign Key.

Other types of keys are only concepts of RDBMS that we need to know.

NOTE:

Page 24: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 24

Entity-Relationship Diagram

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

•Such a diagram consists of the following major components:

• Rectangles: which represent entity sets

• Ellipses: which represent attributes

• Diamonds: which represent relationship sets

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

• Double ellipses: which represent multivalued attributes

• Dashed ellipses: which denote derived attributes

Page 25: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 25

• Double lines: which indicate total participation of an entity in a relationship set

• Double rectangles: which represent weak entity sets

Page 26: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 26

Page 27: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 27

Page 28: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 28

Weak Entity Sets•An entity set may not have sufficient attributes to form a primary key. Such an entity set is named as a weak entity set.

•Weak entity set does not have a primary key, we underline the discriminator of a weak entity set with a dashed line

•We depict a weak entity set by double rectangles in E-R diagram.

•A weak entity is an entity that depends on the existence another entity.

Page 29: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 29

Page 30: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 30

•An entity set that has a primary key is termed a strong entity set.

Strong Entity Set

Page 31: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 31

Extended E-R Features(Generalization, Specialization & Aggregation)

•An entity set may include subgroupings of entities that are distinct in some way from other entities in the set.

•For instance, a subset of entities within an entity set may have attributes that are not shared by all the entities in the entity set.

•Consider an entity set person, with attributes name, street, and city. A person may be further classified as one of the following: customer employee

Specialization

Page 32: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 32

Generalization

• A bottom-up design process – combine a number of entity sets that share the same features into a higher-level entity set.

Specialization and generalization are simple inversions of each other; they are represented in an E-R diagram in the same way.

• It only differs in the design process. 

•Specialization is a top-down design process, Whereas Generalization is a bottom-up design process.

•The concept of generalization is similar to inheritance in Java. 

Page 33: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 33Figure :Specialization and generalization

Page 34: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 34

Aggregation• A feature of the entity relationship model that allows a relationship set to participate in another relationship set.

•This is represented in a ER diagram by a diamond shape.

•For example, an 'Employee' 'works-on' a 'branch‘. An 'Employee' 'manages' some tasks. The 'Works-on' entity here can be abstracted via the use of aggregation. 

Page 35: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

Aggregation• Suppose we want to record managers for tasks performed by an

employee at a branch

Page 36: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 36

• Relationship sets works-on and manages represent overlapping information– Every manages relationship corresponds to a works-on relationship– However, some works-on relationships may not correspond to any

manages relationships • So we can’t discard the works-on relationship

• Eliminate this redundancy via aggregation– Treat relationship as an abstract entity– Allows relationships between relationships – Abstraction of relationship into new entity

• Without introducing redundancy, the following diagram represents:– An employee works on a particular job at a particular branch – An employee, branch, job combination may have an associated manager

Aggregation (Cont.)

Page 37: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 37Fig:E-R diagram with Aggregation

Page 38: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 38

Attribute Inheritance

•A crucial property of the higher- and lower-level entities created by specialization and generalization is attribute inheritance.

•The attributes of the higher-level entity sets are said to be inherited by the lower-level entity sets.

•For example, customer and employee inherit the attributes of person. Thus, customer is described by its name, street, and city attributes, and additionally a customer-id attribute; employee is described by its name, street, and city attributes, and additionally employee-id and salary attributes.

•A lower-level entity set (or subclass) also inherits participation in the relationship sets in which its higher-level entity (or superclass) participates.

Page 39: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 39

Constraints on Generalizations•To model an enterprise more accurately, the database designer may choose to place certain constraints on a particular generalization.

• Constraint on which entities can be members of a given lower-level entity set.

1. Condition-definedE.g. all customers over 65 years are members of senior-citizen

entity set; senior-citizen ISA person.

• Constraint on whether or not entities may belong to more than one lower-level entity set within a single generalization

2. User-defined

Page 40: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 40

3. Disjoint

•An entity can belong to only one lower-level entity set.

4. Overlapping

An entity can belong to more than one lower-level entity set.

•Completeness constraint: specifies whether or not an entity in the higher-level entity set must belong to at least one of the lower-level entity sets within a generalization.

total : an entity must belong to one of the lower-level entity setspartial: an entity need not belong to one of the lower-level entity sets

Page 41: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 41

Summary of Symbols Used in E-R Notation

Page 42: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 42

Summary of Symbols (Cont.)

Page 43: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 43

Alternative E-R Notations

Page 44: UNIT_2 1 DATABASE MANAGEMENT SYSTEM[DBMS] 2620003 [Unit: 2] Prepared By Lavlesh Pandit SPCE MCA, Visnagar

UNIT_2 44

Example of E-R Diagram