67
1 ER Model

ER_Model Presenatation

Embed Size (px)

DESCRIPTION

ER model

Citation preview

  • 1ER Model

  • 2Database design- steps

    Requirement Analysis

    Conceptual Database Design ER Diagram

    Logical Database Design

    Additional design components like views,

    stored procedures, triggers etc.

    Converting ER Diagram

    into RDBMS tables etc.

    Later

  • 3ER Model

    ER data model allows us to conceptually understand the data of the real world enterprise which we want to store in terms of their relationships.

    This model is realized by diagrammatically representing the collection of data and their relationship. There are two established versions for entity relationship diagram (ERD):

    Chens notation and Crows foot notation. As stated earlier, this is done in conceptual database design phase. As stated earlier, this is done in conceptual database design phase.

    Our focus

  • 4Defining Entity

    Entity: It is an object that is distinct and unique. Examples: a student, a keyboard, a college etc.

    Entity set: A collection of similar entities is entity set. Examples : Students, Colleges

    Attributes: Properties that describe an entity is attribute. All entities in the entity set have same attributes (but the values of the attribute may be different). For example, a student will have the attribute may be different). For example, a student will have registration number, name and address as attributes.

  • 5Primary Key A minimal set of attributes that uniquely identifies an entity is called a

    candidate key.

    An entity can have many candidate keys. For example, for a student entity, if we assume that students have unique names, both registration no. and name are candidate keys.

    One among the candidate keys is chosen to be a primary key. A primary key should not be null.

    A composite key is a primary key with more than one attribute.

    A super key is a set of attributes which contains the primary key.

  • 6Representing an entity in Chens notation

    Students

    regNo

    name

    Attributes

    Primary key

    address

    Courses

    streamId

    title

    noOfSems

    Entity Set

  • 7Relationship

    Relationship is the association between entities or entity sets. For example between two entities student and course, the relationship is a course enrolls for many students.

    Relationships may or may not also have attributes. For example, if we were to maintain enrollment date, then the relation enrolls will we were to maintain enrollment date, then the relation enrolls will have an attribute enrollment-date.

    Relationship set is a collection of similar relationships.

  • 8Binary Relationship Example

    Subject Is Taught Teacher

    teacherIdtitle

    1 1

    Mapping/Connectivity

    nameaddresssubId

    Relationship SetOne to One

  • 9Binary Relationship Example

    Courses Has Students

    regNonoOfSems

    1 N

    Mapping/Connectivity

    nameaddressstreamId title

    Relationship SetOne to Many

  • 10

    Binary Relationship Example

    Customer Bank Account

    acctNoaddress

    M N

    Mapping/Connectivity

    owns

    acctTypebalancecustId name

    Relationship Set

    Many to Many

  • 11

    Relationship with attributes

    Courses Has Students

    regNonoOfSems

    1 N

    Mapping/Connectivity

    nameaddressstreamId title

    Relationship Set

    enrollment-date

    Attribute of relationship

    Binary Relationship

  • 12

    Cardinality

    regNoCardinality

    Cardinality is used to specify number of entity occurrences with one occurrence of related entity. A course can have a minimum of 1 and a maximum of 35 students. A student can take a maximum of 6 and a minimum of 1 course.

    Courses Has Students

    regNo

    nameaddressstreamId title

    noOfSems

    enrollment-date

    (1,35)(1,6)

    Cardinality

  • 13

    Example of a Ternary relationship

    Employees DepartmentsWorks-In

    dateOfJoining

    deptIdempId

    N M

    Location

    namename

    addresscapacity

    locId

    P

  • 14

    Example of a Unary relationship

    CustomercustId

    address

    referred CustomercustId

    name

    referred byN

    1

  • 15

    Key Constraints on relationship Consider a restriction where a student can join only one course. This is an

    example of a key constraint.

    In the below diagram the directional arrow ensures that there is only one instance of student in the relationship set Has. In other words, given a student, has relationship can be uniquely determined.

    Courses Has Students

    regNo

    name

    addressstreamId

    title

    noOfSems

    1 N

    enrollment-date

    Directional arrow

  • 16

    Customer buy ArtPiecetitle

    custIdaddress

    1 N

    Directional arrow

    itemId

    price

    Customer buy ArtPiecename

    date-of-purchaselastprice

  • 17

    Participation constraints

    Total participation: If all entities in an entity-set need to be in the relationship set then the entity is in total participation with respect to relationship.

    Example: Every employee works in at least one department. Each department has at least one employee. Therefore both Employee and Department entities in Works-In relationship is total.

    Partial participation: A participation that is not total is partial .

  • 18

    Example of a total participation

    Employees DepartmentsWorks-In

    dateOfJoining

    deptIdempId

    location

    namename

    addresscapacity

    locIdIndicate total participation

    or

  • 19

    Modeling Attributes

  • 20

    Single-Valued Attributes

    An attribute which has only one value.

    All the attributes that we have seen so far were single valued attribute.

    Employees

    name single-valued attribute

  • 21

    Composite Attribute

    A composite attribute is an attributes that is made up of more than one attribute.

    For example address can be thought of as having more than one attribute.

    address

    house number street number

    colony or area

    city or towndistrict

    house number

    state

    country

    Employees

    composite attribute

  • 22

    Multi-Valued Attribute

    A multi-value attribute is an attribute which can have multiple values.

    phone_numbers

    Employeesmulti-valued attribute

  • 23

    Derived Attribute

    A derived attribute is an attribute whose value is determined or computed using existing values of the other attributes (or otherwise also called stored attribute).

    While the value of the composite attribute for a row is computed as concatenation of the values of a set of attributes of the same row, the concatenation of the values of a set of attributes of the same row, the derived attribute can be calculated based on values of attributes in multiple rows also( example sum of gross_salary of all entities in Employee entity set)

  • 24

    Example

    Employee

    gross_salary

    net_salary

    derived attributedeductionsgross_salary

    stored attribute

    derived attribute

  • 25

    Weak Entities

    A weak entity is an entity that cant be uniquely identified by its own attributes alone, and therefore must use as its primary key both its own attributes and the primary key of an entity it is related to.

    In other words, a weak entity cannot exist without the entity it is related to (owner entity).The relationship between owner entity and weak entity is one-to-many.

  • 26

    Weak Entity example

    Assume a shopping application. A customer orders for many items. We have 2 entities to maintain customer order. Orders entity: records the order details like order number, order

    date and customer id Order-Items: records the items pertaining to the order. Order-Items: records the items pertaining to the order. Order-Items entity is a weak entity.

  • 27

    Orders Order-Itemscontains

    A weak entity set diagram

    itemNocustId

    partial key

    1 n

    Invariably the entity

    will be in total

    participation

    orderDateorderNo

    noOfPieces

    contains

    Order-Items

    or or

    Identifying

    relationship

  • 28

    Hierarchy

    The entities can be classified as general type and specific type.

    IS-A relationship.

    For example, an Employee can be a programmer or a manager.

    IS-A relationship.

    Employeename empId

    IS-A

    Programmer Marketing

    Current-Project

    Main-Role

    area

    num-of-contracts

    specia

    lizatio

    ng

    e

    n

    e

    r

    a

    l

    i

    z

    a

    t

    i

    o

    n

    IS-A relationship.

  • 29Redundant relationship problem and Aggregation

    Consider a relationship where employee works on an assignment and may use machinery.

    namejoiningDateempId

    Employee works Project

    usesMachinery

    machineID

    purchaseDateredundant relationships

    hours

  • 30

    Aggregation

    Solution is to use aggregation. Aggregation is a relationship between collection of entities and relationships.

    Aggregation allows us to indicate that a relationship set participates in another relationship set.

    Use aggregation when we need to have a relationship among Use aggregation when we need to have a relationship among relationship.

  • 31

    Employee

    namejoiningDate

    works Project

    aggregation

    empId

    uses Machinery machineID

    purchaseDate

    aggregation

    hours

  • 32

    Entering Into Relational ModelEntering Into Relational Model

  • 33

    The main construct for relation model rests on the relation (table). A relational database is a collection of relations.

    The degree ( or arity) of a relation is the number of fields in a relation. The cardinality of a relation is number of tuples in it.

    More terms

    The cardinality of a relation is number of tuples in it.

  • 34

    Integrity Constraints

    An integrity constraint is a condition or a rule specified on the table which restricts the kind of data that can be stored in a table.

    These rules thereby ensures that data entered in the database is accurate and valid.accurate and valid.

    DBMS ensures that the constraints defined in the schema is strictly adhered to when ever or however data enters into the database.

  • 35

    Types of Integrity Constraints Entity integrity allows no two rows with the same identity in a table.

    Primary Key and Unique constraint are used to enforce entity integrity.

    Referential integrity allows only the consistent values for certain fields across the related tables. Foreign Key is used to enforce referential integrity.integrity.

    Domain integrity allows only predefined values for the attributes.

    User-defined integrity allows only what you predefine. Example: Null constraint.

    Define in the next slide

    Later

    Constraints for candidate keys which are not primary key

  • 36

    Foreign Key A foreign key is a reference to a primary key in another table.

    Therefore referential integrity is ensuring that the foreign key has only those data values that correspond to the primary key data values of another table.

    DeptName Manager

    Marketing EMP100

    Training EMP200

    DepartmentID Name

    EMP100 Rama

    EMP101 Rahim

    EMP200 Raghu

    EmployeeForeign key

    Primary key

    Enforcing referential integrity

  • 37

    Other advantages of foreign key Specifying foreign key also allows us to specify what action should be

    taken when the corresponding primary key data is updated or deleted. Action could be either

    NO ACTION: Dont allow update or deletion of the primary whose value is referenced by the foreign key.

    CASCADE: Delete or Update foreign key also when the corresponding primary key is changed.

    SET NULL: set null values to foreign key values when the corresponding primary key value is changed.primary key value is changed.

  • 38

    Null values and Keys

    A column which does not have any value has Null Value.

    For attributes which should not have null value, NOT NULL constraints can be specified.

    For a foreign key null value is allowed.

    A primary key cannot be null.

    Unique key can be null.

  • 39

    Translating E-R Diagram into Relational Model

  • 40

    Modeling Entity Set

    Model entity sets to tables with primary key constraints.

    noOfSems

    streamId title noOfSems

    Courses

    Courses

    streamId

    title streamId title noOfSems

    123 B.Tech 8

    124 B.E. 8

  • 41

    Modeling Relationship-set

    Modeling One-to-One relationship

    Modeling One-to-Many relationship

    Modeling Many-to-Many relationship

    Modeling relationship with attributes

  • 42

    One-to-One into Relational Model

    Primary Key of any of the entity set can become foreign Key of the other entity set.

    teacherIdtitle

    Subject Is Taught Teacher

    nameaddresssubId

    title

    1 1

  • 43One-to-One into Relational Model

    subId title teacherId

    S101 Java EMP103

    S102 C EMP102

    S103 OOPS and UML EMP101

    Subject

    S103 OOPS and UML EMP101

    teacherId name address

    EMP101 Mona Lisa XXX YY

    EMP102 Lora Nillie AAA BB

    EMP103 Peter Patter MMM NN

    Teacher Foreign key

  • 44

    One-to-Many into Relational Model

    Primary Key of the entity set with one mapping becomes foreign key of the entity set with many mapping.

    regNonoOfSems

    Courses Has Students

    regNo

    nameaddressstreamId title

    noOfSems

    1 N

  • 45

    One-to-Many into Relational Model

    regNo name address streamId

    ST100 Bill Smith Xxx yyy 123

    ST200 Rani Raja Mmm nnn 123

    ST202 Neeta Roy Aaa bbb 124

    Students

    streamId title noOfSems

    123 B.Tech 8

    124 B.E. 8

    Courses

    ST202 Neeta Roy Aaa bbb 124

    Foreign key

  • 46

    Many-to-Many into Relational Model

    A new relation for relationship between the two entity sets is created. This relation will contain the attributes as primary keys of both the

    entity sets. Combination of these two attributes become the primary key of this relation.

    Customer owns Bank Account

    acctNo

    balancecustId name

    address

    M N

  • 47

    custId name address

    C100 Bill Smith Xxx yyy

    C200 Rani Raja Mmm nnn

    C202 Neeta Roy Aaa bbb

    Customer

    acctNo balance

    BankAccount

    custId acctNo

    Owns

    Primary Key

    acctNo balance

    4343434 10000

    4343435 20000

    4343436 25000

    custId acctNo

    C100 4343434

    C200 4343435

    C202 4343436

    C202 4343434

  • 48

    Relationships with attribute

    Modeling relationship set with attributes: Like entity set, relationship is also translated into table.

    The primary key attributes of each participating entity set, as foreign key fields.

  • 49

    Employees DepartmentsWorks-In

    dateOfJoiningdeptId

    nameempId

    Relationships with attribute

    Employees DepartmentsWorks-In

    Location

    nameaddress

    capacity

    locId

  • 50

    Works-InempId deptId locId dateOfJoining

    EMP101 MKT BLR1 7-APR-2006

    EMP102 TRA BLR2 21-JUN-2006

    empId name

    EMP101 Mona Lisa

    EmployeesdeptId name

    MKT Marketing

    Departments

    Together form primary key

    Foreign key

    EMP101 Mona Lisa

    EMP102 Lora Nillie

    EMP103 Peter Patter

    MKT Marketing

    TRA Training

    DEV Development

    locId address capacity

    BLR1 Banashankari 100

    BLR2 MG Road 50

    Location

  • 51

    Modeling Key Constraints

    Key Constraints is modeled as a Unique constraint.

    regNoaddress

    streamIdnoOfSemsDirectional arrow

    Courses Has Studentsname

    title

    1 N

    enrollment-date

    Directional arrow

  • 52

    regNo name address

    ST100 Bill Smith 8

    Students

    regNo streamId enrollment-date

    ST100 123 8

    ST200 124 8

    ST100

    Has

    UNIQUE(regNo)

    streamId title noOfSems

    123 B.Tech 8

    124 B.E. 8

    Courses

    ST100 Bill Smith 8

    ST200 Rani Raja 8

    ST202 Neeta Roy 6

  • 53

    Modeling Participation Constraint

    Total participation constraint is modeled as foreign key with CASCADE on deletion or updation or NO ACTION is specified.

    Partial participation constraint is modeled as foreign key with SET NULL on deletion or updation.

  • 54

    Employees DepartmentsWorks-In

    dateOfJoining

    deptId

    name

    empId

    location

    namename

    addresscapacity

    locId

  • 55

    Works-InempId deptId locId dateOfJoining

    EMP101 MKT BLR1 7-APR-2006

    EMP102 TRA BLR2 21-JUN-2006

    empId name

    EMP101 Mona Lisa

    EmployeesdeptId name

    MKT Marketing

    Departments

    Foreign key with

    CASCADE on delete

    or update

    COR

    EMP101 Mona Lisa

    EMP102 Lora Nillie

    EMP103 Peter Patter

    MKT Marketing

    TRA Corporate Training

    DEV Development

    locId address capacity

    BLR1 Banashankari 100

    BLR2 MG Road 50

    Location

    COR

  • 56

    Modeling Weak Entity

    Weak entity is modeled as foreign key with CASCADE on deletion or updation with NOT NULL constraint (if the foreign key attribute is not part-key).

  • 57

    Modeling Hierarchy

    Each of entity set in the hierarchy is modeled as distinct relation.

    The primary key of the child entity set is same as the primary key of the parent entity set.

    The primary key of the child entity set is also foreign key referencing to its parent entity set.to its parent entity set.

    The foreign key must be with cascade delete and update constraint.

  • 58

    Employeename empId

    IS-A

    Programmer Marketing

    Current-Project

    Main-Role

    area

    contracts

    EmpId Name

    Employee

    EmpId contracts area

    Marketing

    EmpId Name

    EMP1 K.Narayanan

    EMP2 M.Joshna

    EmpId Current-Project Main-Role

    EMP1 ABC Designing

    EMP2 XYZ Coding

    Programmer

    EmpId contracts area

    EMP1 XXX Training

    Foreign key

    Foreign key

  • 59

    Modeling Aggregation

    The aggregation relationship contains the primary key attributes as combination of all keys of the entity sets and relationships it is linked with.

    The key attributes are also the foreign key. The key attributes are also the foreign key.

  • 60

    Employee

    namejoiningDate

    works Project

    aggregation

    empId

    pid

    startDate

    uses Machinery machineID

    purchaseDate

    hours

    Uses(empId, pid, machineID, hours)Primary Key

    Foreign Key

  • 61

    Question for you?

    How will you model unary relationship into relational model?

    address

    CustomercustId

    name

    referred by

  • 62

    Modeling composite attribute Each of the attributes of the composite attribute is modeled as columns

    of the table. The composite attribute does not appear as a column in the table. Its

    value is obtained by concatenating the values of the attributes. It appears as a column in the view.

    address

    house number street number

    colony or area

    city or towndistrict

    state

    country

    Employees

    composite attribute

    empId

  • 63

    EmpId house no

    street number

    area or town

    dist state country

    EMP1 12 8th main MG rd. BLR Karnataka India

    EMP2 50 2nd Main MG rd. BLR Karnataka India

    Employee

    EmployeeAddressView

    EmpId Address

    EMP1 12, 8th main, MG rd., BLR, Karnataka, India

    EMP2 50, 2nd main, MG rd., BLR, Karnataka, India

    EmployeeAddressView

  • 64

    Modeling Multi-Valued Attribute

    A table is created for multi-value attribute containing the primary key of the entity, multi-value attribute and its corresponding attributes.

    phone_numbers

    Employeesmulti-valued attribute empId

    name

  • 65

    EmpId Name

    EMP1 Nelson Mac

    EMP2 Kate Dell

    Employee

    EmpId Phone_Numbers

    PhoneNumbers

    Table for multi-valued attribute

    EmpId Phone_Numbers

    EMP1 9831610763

    EMP1 54783990

    EMP2 9831688789

    EMP2 5575390

  • 66

    Modeling Derived Attribute

    Like composite attribute, derived attribute is also implemented as a view.

    Employee

    deductionsgross_salary

    net_salary

    derived attribute

    empId

  • 67

    EmpId Gross_Salary Deduction

    EMP1 20000 2000

    EMP2 15000 1000

    Employee

    EmpId Net_Salary

    EmployeeNetSalaryView

    derived attribute

    Gross_Salary - Deduction

    EmpId Net_Salary

    EMP1 18000

    EMP2 14000

    derived attribute