2.ER Diagram

Embed Size (px)

Citation preview

  • 8/2/2019 2.ER Diagram

    1/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 11

    ChapterChapter--22

  • 8/2/2019 2.ER Diagram

    2/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 22

    Requirement Analysis:Requirement Analysis: CollectingCollecting

    information from the users what theyinformation from the users what they

    want to store in the database.want to store in the database. Conceptual Database Design:Conceptual Database Design: High levelHigh level

    description of data to be stored in thedescription of data to be stored in the

    database. ER model is the ideal one.database. ER model is the ideal one.

    Logical Database Design:Logical Database Design: Selecting anSelecting an

    appropriate DBMS for implementation.appropriate DBMS for implementation.

  • 8/2/2019 2.ER Diagram

    3/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 33

    Schema Refinement:Schema Refinement: This is done throughThis is done through

    normalization theories.normalization theories.

    Physical Database Design:Physical Database Design: Low levelLow levelimplementation like indexing and clusteringimplementation like indexing and clustering

    of tables.of tables.

    Application and Security Design:Application and Security Design: DevelopingDeveloping

    a complete application using fronta complete application using front--end toolsend tools

    like VB, Java, etc. Unified Modelinglike VB, Java, etc. Unified Modeling

    Language (UML) may be used.Language (UML) may be used.

  • 8/2/2019 2.ER Diagram

    4/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 44

    Entities:Entities:An entity is anything that exists inAn entity is anything that exists in

    a real world with an independent existence.a real world with an independent existence.EgEg.. MarutiMaruti Car, Accounts Department , DBMSCar, Accounts Department , DBMS

    Book, etc.Book, etc.

    Entity Set:Entity Set: Collection of similar entities.Collection of similar entities.

    EgEg. Employees. EmployeesCollection of AccountsCollection of Accounts

    department employees and HR departmentdepartment employees and HR departmentemployees.employees.

  • 8/2/2019 2.ER Diagram

    5/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 55

    Attributes:Attributes: The properties of an entity can beThe properties of an entity can bedescribed by what is known as attributes.described by what is known as attributes.

    EgEg. SSN, Name, Age may be the attributes of. SSN, Name, Age may be the attributes of

    the entity set Employees. These attributesthe entity set Employees. These attributesare common for all the entities in this entityare common for all the entities in this entityset.set.

    Attribute Types: Simple or AtomicAttribute Types: Simple or Atomic -- holdhold

    single value and not composed of any othersingle value and not composed of any otherattributes.attributes.

    Example:Example: RegNoRegNo, Age,, Age, Date_of_BirthDate_of_Birth,,Dept_NoDept_No, Sex, etc., Sex, etc.

  • 8/2/2019 2.ER Diagram

    6/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 66

    Composite:Composite: attributes that can be subattributes that can be sub--dived intodived intosome more attributes are called as compositesome more attributes are called as composite

    attributes.attributes. EgEg. Address. Address

    Single valued:Single valued: hold only one value for a particularhold only one value for a particular

    entity.entity. EgEg. Age, Sex. Age, Sex

    MultiValuedMultiValued:: attributes having more than oneattributes having more than one

    value.value. EgEg.. DLocationDLocation, Qualification, Qualification

    Stored:Stored: nonnon--derivablederivableEgEg. Sex, DOB. Sex, DOB

    Derived:Derived: can be derived from othercan be derived from other attribute(sattribute(s).).

    EgEg. Age, Total. Age, Total

  • 8/2/2019 2.ER Diagram

    7/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 77

    Key:Key:A key is a minimal set of attributes whoseA key is a minimal set of attributes whosevalues uniquely identify an entity in an entity set.values uniquely identify an entity in an entity set.EgEg. SSN. SSN

    Candidate Key:Candidate Key: There could be more than oneThere could be more than onecandidate key for each entity set.candidate key for each entity set.

    Primary Key:Primary Key: One of the candidate keys may beOne of the candidate keys may beselected as the primary key.selected as the primary key.

    Domain of Attributes:Domain of Attributes: Each attribute is associatedEach attribute is associatedwith a set of values called aswith a set of values called as domdom(Ai(Ai). If the). If theattribute Age takes, say, values between 18 and 58attribute Age takes, say, values between 18 and 58years, the domain of this attribute is 18years, the domain of this attribute is 18--58.58.

  • 8/2/2019 2.ER Diagram

    8/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 88

    SingleSingle partialpartial

    DoubleDouble --totaltotal

    ParticipationParticipation

    ConstraintsConstraints

    88

    Diamond boxDiamond boxRelationship SetRelationship Set77

    UnderlinedUnderlinedPrimary keyPrimary key66

    OvalOval -- thickthickDerivedDerivedAttbAttb55

    OvalOval doubledoubleMulti valuedMulti valuedAttbAttb44

    OvalOval singlesingleAttributesAttributes33

    Rectangular BoxRectangular Box

    Double LineDouble Line

    Weak Entity SetWeak Entity Set22

    Rectangular BoxRectangular Box

    Single lineSingle lineStrong Entity SetStrong Entity Set11

    MeaningMeaningER NotationER NotationEntity SetEntity SetSlSl. No. No

    Name

    Employees

    Dependents

    DLocation

    SSN

    Age

    Manages

  • 8/2/2019 2.ER Diagram

    9/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 99

    RelationshipRelationship -- is an association among twois an association among two

    or more entities.or more entities.

    Relationship setRelationship set is a collection of similaris a collection of similarrelationships.relationships.

    WorksFor

    SSN

    Name

    Addr

    Employees

    DNo DLocation

    Departments

    DName

  • 8/2/2019 2.ER Diagram

    10/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 1010

    Employees Works_For Departments

  • 8/2/2019 2.ER Diagram

    11/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 1111

    The degree of a relationship type isThe degree of a relationship type is

    defined as the number of participatingdefined as the number of participating

    entities. If the number of participatingentities. If the number of participating

    entities is only two, then it is called asentities is only two, then it is called asbinary relationshipbinary relationship..

    Suppose if the number of participatingSuppose if the number of participating

    entities are three, then it is called asentities are three, then it is called as

    ternary relationshipternary relationship..

  • 8/2/2019 2.ER Diagram

    12/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 1212

    Ternary Relationship SetTernary Relationship Set

    Sid

    SName

    SAddr PNo

    PName

    location

    PtNo PtName

  • 8/2/2019 2.ER Diagram

    13/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 1313

  • 8/2/2019 2.ER Diagram

    14/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 1414

    Cardinality ratio:Cardinality ratio: TheThe cardinality ratiocardinality ratio for afor a

    binary relationship specifies the number ofbinary relationship specifies the number of

    relationship instances that an entity canrelationship instances that an entity can

    participate in.participate in.

    Example: oneExample: one--toto--one (1:1) means that aone (1:1) means that a

    Department can have only one Manager.Department can have only one Manager.

    TypesTypes of cardinalityof cardinality mappings:mappings:

    (a) one(a) one--toto--one (1:1)one (1:1)

    (b) one(b) one--toto--many (1:N)many (1:N)

    (c) many(c) many--toto--many (M:N)many (M:N)

  • 8/2/2019 2.ER Diagram

    15/42

  • 8/2/2019 2.ER Diagram

    16/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 1616

  • 8/2/2019 2.ER Diagram

    17/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 1717

  • 8/2/2019 2.ER Diagram

    18/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 1818

    Total Participation:Total Participation: assume that the company policy isassume that the company policy isthat every department should have a manager. This impliesthat every department should have a manager. This implies

    that there will be a full or total participation betweenthat there will be a full or total participation between

    Departments and Manages. Therefore, this type ofDepartments and Manages. Therefore, this type of

    participation is called asparticipation is called as total participationtotal participation (represented as(represented asthick or double line).thick or double line).

    Total Participation

    since

  • 8/2/2019 2.ER Diagram

    19/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 1919

    When an entity set E does not participateWhen an entity set E does not participate

    fully with a relationship and in turn to thefully with a relationship and in turn to the

    participating entity, such a constraint isparticipating entity, such a constraint is

    called as partial participation.called as partial participation.

    Partial Participationsince

  • 8/2/2019 2.ER Diagram

    20/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 2020

    AAweak entityweak entity set is an entity set such that itsset is an entity set such that its

    key is composed of attributes some or all are ofkey is composed of attributes some or all are of

    which belong to another entity set.which belong to another entity set.

    Weak entity sets do not have their own keyWeak entity sets do not have their own keyattributes (only partial key allowed).attributes (only partial key allowed).

    A weak entity will always have total participationA weak entity will always have total participation

    with the identifying owner entity.with the identifying owner entity.

    The owner entity set and the weak entity setThe owner entity set and the weak entity set

    must participate in 1:M relationship.must participate in 1:M relationship.

  • 8/2/2019 2.ER Diagram

    21/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 2121

    DepNameDepName partial keypartial key

    SSN

    Name

    Addr DepName

    BirthDate

    Sex

  • 8/2/2019 2.ER Diagram

    22/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 2222

    Strong entity set:

    Has sufficient attributes to form a primary key

    Weak entity set:

    Lacks sufficient attributes to form a primary key

    Hence, lacks sufficient attributes to form any key

    But every entity set needs a key; What to do?

    Must import attributes from strong entity set(s) A weak entity set member is subordinate to the

    dominant entity from strong entity set providingattributes to complete its key

  • 8/2/2019 2.ER Diagram

    23/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 2323

    Loan-payment

    Payments

    loanno

    amount

    Payment-amount

    Payment-no Payment-date

    Employees

  • 8/2/2019 2.ER Diagram

    24/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 2424

    AnAn abstractionabstraction of some system is a model ofof some system is a model ofthat system in which certain details arethat system in which certain details aredeliberately omitted.deliberately omitted.

    In some applications a system may have tooIn some applications a system may have toomany details that need to be managed andmany details that need to be managed andprovide abstraction. During such instances, itprovide abstraction. During such instances, itcan be managed by decomposing the model intocan be managed by decomposing the model intohierarchy of abstractions.hierarchy of abstractions.

    A relation inA relation in Codd'sCodd's relational schema supportsrelational schema supportstwo types of abstractionstwo types of abstractions generalizationgeneralization andandaggregationaggregation..

  • 8/2/2019 2.ER Diagram

    25/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 2525

    AAgeneralizationgeneralization is an abstraction whichis an abstraction which

    enables a class of objects to be thought ofenables a class of objects to be thought of

    generically as a single named object.generically as a single named object.

    We can think of generalization as aWe can think of generalization as a

    relationship between an entity (or class)relationship between an entity (or class)

    and one or more refined versions of it.and one or more refined versions of it.

    The generalization is represented throughThe generalization is represented throughwhat is known aswhat is known as ISA relationshipISA relationship

  • 8/2/2019 2.ER Diagram

    26/42

    S. Nandagopalan, BITS. Nandagopalan, BIT

    2626

    AccNo Balance

    Account

    ISA

    Int_amt

    Dep_amt Rec_amt Od

  • 8/2/2019 2.ER Diagram

    27/42

    S. Nandagopalan, BITS. Nandagopalan, BIT

    2727

    Employee(SSN, Name, Addr, Sex)

    Fulltime_Employee(Days_worked, Salary )

    Contract_Employee( Contract_period, Contract_amount )

  • 8/2/2019 2.ER Diagram

    28/42

    S. Nandagopalan, BITS. Nandagopalan, BIT

    2828

    Aggregation is an abstraction throughAggregation is an abstraction throughwhich relationships are treated as higherwhich relationships are treated as higher--level entities.level entities.

    The ER diagram lacks in establishing aThe ER diagram lacks in establishing arelation between relationships sets. Usingrelation between relationships sets. Usingour knowledge of ER diagram, we canour knowledge of ER diagram, we canconstruct the relationship among the threeconstruct the relationship among the threeentities Employees, Projects and Machinesentities Employees, Projects and Machineswithwith Works_onWorks_on and Uses relationships.and Uses relationships.

  • 8/2/2019 2.ER Diagram

    29/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 2929

    OS

    Type

    SSN

    Name

    AddrHrs

    PNo PName

    Mid OS

    From

    To

  • 8/2/2019 2.ER Diagram

    30/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 3030

    Why not use a ternary relationship?Why not use a ternary relationship?

    CombineCombine Works_OnWorks_On and Uses to a singleand Uses to a single

    relationship set? No! Because, each onerelationship set? No! Because, each onehas its onehas its one attribute(sattribute(s).).

    Works_OnWorks_On has Hrs and Uses has From andhas Hrs and Uses has From and

    To (dates) attributes.To (dates) attributes.

    Hence, the solution is aggregation.Hence, the solution is aggregation.

  • 8/2/2019 2.ER Diagram

    31/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 3131

    Hrs

    AggregationAggregation

    FromTo

  • 8/2/2019 2.ER Diagram

    32/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 3232

    BranchOffice

    SSN

    Name

    Addr

    BranchID BranchName

    Staff

    Aggregation represents a "hasAggregation represents a "has--a" or "parta" or "part--of"of"

    relationship between entity sets where onerelationship between entity sets where one

    represents the "whole" and the other therepresents the "whole" and the other the

    "part"."part".Example:Example: eacheach BranchOfficeBranchOffice ("whole") has some Staff ("part("whole") has some Staff ("part--of").of").

  • 8/2/2019 2.ER Diagram

    33/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 3333

    StepStep--1:1: I dent ify t he St rong and WeakI dent ify t he St rong and WeakEnt it y SetsEnt it y Sets

    StepStep--2:2: I dent i fy theI dent i fy the relevantrelevant Attr ibutesAttr ibutes

    StepStep--3:3: I dent ify t he Relationship SetsI dent ify t he Relationship Sets

    StepStep--4:4: I dent ify t he Cardinality Rat io andI dent ify t he Cardinalit y Rat io andPart icipat ion Const raint sPart icipat ion Const raint s

    StepStep--5:5: I dent i fy the I SI dent i fy the I S--A and HasA and Has--AARelationship SetsRelationship Sets

  • 8/2/2019 2.ER Diagram

    34/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 3434

    Case St udy

    Insurance Policy Company Database

    Library System

    Case St udy

    Insurance Policy Company Database

    Library System

  • 8/2/2019 2.ER Diagram

    35/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 3535

    WorkFor

    BranchID Addr

    BranchNamePhone

    Hold

    Date

    Po li cy Name Pr emAmoun t

    Tenure

    Bonus

    MatAmountPolicyNo

    Name

    Phone

    AddrAge

    Policy

    Branches

    Customers

    Staff

    Interact

    Managers SectAssts DevOfficers FullTime Agents

    StaffID

    StaffName

    Age

    Phone

    M

    1

    M1

    M 1

  • 8/2/2019 2.ER Diagram

    36/42

    S. Nandagoapalan, BIT

    Start date

    Employees

    NameSSN

    Bdate

    Sex

    Addr

    SalaryDName

    WorksFor Departments

    Manages

    DNumber

    Locations

    Supervises

    Name

    Has

    Dependents

    Sex

    Birth_date

    WorksOn

    Hours

    No. of Employees

    CONTROLS

    PName

    PNumberLocation

    Projects

    N 1

    11

    NM

    1

    NRelationship

    N

    1

  • 8/2/2019 2.ER Diagram

    37/42

    ISBNTitle

    Author

    PublPrice

    AquisNo DatePur

    Date

    Custid

    Name

    BirthDate

    ResNo ResDate

    Priority

    Dept Desig

    Salary

    Branch Fee

    1 M

    M

    M1

    1

    Library System

  • 8/2/2019 2.ER Diagram

    38/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 3838

    Design an ER diagram for the following problem:Design an ER diagram for the following problem:

    It is required to store the information regarding Students, TeacIt is required to store the information regarding Students, Teachers,hers,and Subjects in an Engineering Department of a College orand Subjects in an Engineering Department of a College orUniversity.University.

    Every student belongs to a particular semester. There are eightEvery student belongs to a particular semester. There are eight

    semester in each discipline.semester in each discipline. A minimum of 15 students are required to opt for an electiveA minimum of 15 students are required to opt for an elective

    subject.subject.

    A teacher may handle more than one subject in any givenA teacher may handle more than one subject in any givensemester. However, for any given semester he/she can handle asemester. However, for any given semester he/she can handle amaximum of two theory and four practical subjects. It is requiremaximum of two theory and four practical subjects. It is required tod tostore the salary and leave details of the teachers.store the salary and leave details of the teachers.

    We need to maintain the attendance and class marks of everyWe need to maintain the attendance and class marks of everystudent in each semester.student in each semester.

    You may assume any other details not supplied with this questionYou may assume any other details not supplied with this question..

  • 8/2/2019 2.ER Diagram

    39/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 3939

    A Bank wants to computerize all of its transactions. ItA Bank wants to computerize all of its transactions. Itoffers the following account types: Savings bank (SB),offers the following account types: Savings bank (SB),Recurring deposit (RD), Fixed deposit (FD)Recurring deposit (RD), Fixed deposit (FD)

    The Bank also wishes to keep track of loans given to theThe Bank also wishes to keep track of loans given to the

    customers. Identify the entities and its attributes with allcustomers. Identify the entities and its attributes with allpossible relationships. Write the ER diagram and statepossible relationships. Write the ER diagram and stateclearly the assumptions that you make. The followingclearly the assumptions that you make. The followingassumptions may be considered:assumptions may be considered:

    A customer can have only one type of account.A customer can have only one type of account.

    Joint accounts are not allowed. Loans can be taken onlyJoint accounts are not allowed. Loans can be taken onlywhen the customer has at least one of the accountwhen the customer has at least one of the accounttypes.types.

  • 8/2/2019 2.ER Diagram

    40/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 4040

    Amazon.comAmazon.com has decided to reorganize its database. Informationhas decided to reorganize its database. Informationabout users, books and sales are stored. Amazon stores as muchabout users, books and sales are stored. Amazon stores as muchinformation as possible on user activity, in order to analyze aninformation as possible on user activity, in order to analyze anddimprove its site. Below are few requirements:improve its site. Below are few requirements:

    A user has a unique id, name, password and a single email addresA user has a unique id, name, password and a single email address.s.

    Amazon contacts users periodically by email, so it is importantAmazon contacts users periodically by email, so it is important totoknow whether a userknow whether a users email has been bouncing back messages ands email has been bouncing back messages andwhether the user is willing to be spammed.whether the user is willing to be spammed.

    The last date of a userThe last date of a users visit is stored, so that Amazon can displays visit is stored, so that Amazon can displayto the user a list of items that are new to the site since his lto the user a list of items that are new to the site since his last visit.ast visit.

    Books have an ISBN number, title, author's name, publisher's namBooks have an ISBN number, title, author's name, publisher's nameeand cost.and cost.

    For each sale, Amazon stores the date of sale, the items bought,For each sale, Amazon stores the date of sale, the items bought, thethecustomer (that has to be a user), his/her address (street, numbecustomer (that has to be a user), his/her address (street, number,r,city, state, country,city, state, country, zipcodezipcode), telephone number, and credit), telephone number, and credit--cardcardnumber.number.

  • 8/2/2019 2.ER Diagram

    41/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 4141

    Users who have bought at least one book, can place commentsUsers who have bought at least one book, can place commentsabout every book (although it is not a book that he has bought aabout every book (although it is not a book that he has bought att

    Amazon), by giving a rate to the book from 1 to 10.Amazon), by giving a rate to the book from 1 to 10.

    Amazon stores, for each comment, the content of the comment andAmazon stores, for each comment, the content of the comment andthe percentage of users who were helped by this comment.the percentage of users who were helped by this comment.

    A book can be on a "A book can be on a "wishwish--listlist" of a user. This is a book that the user" of a user. This is a book that the userwould like to buy at Amazon. Books from awould like to buy at Amazon. Books from a wishwish--listlist can be boughtcan be boughtfor the user by himself, or by a friend. The friend has also tofor the user by himself, or by a friend. The friend has also to be abe auser of Amazon. Amazon wants to keep track of whether books on auser of Amazon. Amazon wants to keep track of whether books on awishwish--listlist were bought, and by whom they were bought.were bought, and by whom they were bought.

    Draw an entity relationship diagram to model the informationDraw an entity relationship diagram to model the informationdescribed above. Remember to put constraints, key attributes, etdescribed above. Remember to put constraints, key attributes, etc.c.

    If you use the ISA relationship, state any covering and overlapIf you use the ISA relationship, state any covering and overlapconstraints that hold. Make any necessary and logical assumptionconstraints that hold. Make any necessary and logical assumptions.s.State any such assumptions clearly.State any such assumptions clearly.

  • 8/2/2019 2.ER Diagram

    42/42

    S. Nandagopalan, BITS. Nandagopalan, BIT 4242