Data base slldes

Embed Size (px)

Citation preview

  • 7/29/2019 Data base slldes

    1/46

    Database Systems

    LECTURE #:09

    M. Shafiq

    FACULTY OF CS & IT

    UNIVERSITY OF GUJRAT

  • 7/29/2019 Data base slldes

    2/46

    Data Modeling

    A detailed model that captures overall structure of

    organizational data while being independent of

    any implementation considerations

    Typically data modelling is carried out parallelwith other requirements analysis and structuring

    steps during the systems analysis phase.

    Following this, the data model supporting the

    scope and requirements of the proposed system isdeveloped.

    2

  • 7/29/2019 Data base slldes

    3/46

    Entity Relationship Diagram Model:

    A Data Modeling Approach. Proposed by Peter Chen in 1976

    Entity Relationship Diagram is a form of data modelling

    A logical representation of the data of an organization or

    business area in graphical form

    Data modelling involves examining the data objects in a

    system and identifying the relationship between these

    objects

    Top-down approach to database design

    The primary purpose of an ERD is to document the logical

    structure of a database

    3

  • 7/29/2019 Data base slldes

    4/46

    Why ERD ?

    Conceptual design: (ER Model is used atthis stage.)What are the entitiesand relationshipsin the

    enterprise?

    What information about these entities and relationshipsshould we store in the database?

    What are the integri ty constraintsor business rulesthat hold?

    A database `schema in the ER Model can berepresented pictorially (ER diagrams).

    An ER diagram can be mapped into a relationalschema.

    4

  • 7/29/2019 Data base slldes

    5/46

    Basic Symbols

    5

    Components

    Name

    Symbol

    Entity

    Attribute

    Relationship

    Department

    Staff No.

    employsemploys

    OR

  • 7/29/2019 Data base slldes

    6/46

    An Entity

    An entity is a thing of interest to a systemabout which information is kept

    For example in a Hospital AdministrationSystem, some likely entities would include:Patient, Doctor, Operation, Ward

    Each of these things are of interest to thesystem and would have data stored aboutthem

    6

  • 7/29/2019 Data base slldes

    7/46

    Choosing Entities

    Example

    A sale always starts with a customer

    receiving an estimate. The estimate then

    becomes an order. An order can be forone or more stock items. Each stock item

    belongs to a certain stock category (e.g.

    taps, sinks, cupboards etc.)

    7

  • 7/29/2019 Data base slldes

    8/46

    An Attribute

    An attribute is an item of data held about an

    entity

    In computer systems an attribute is a field

    of information

    Example : Sales System

    Entity : Customer

    Attributes : Customer Name, Customer

    Address, Customer Phone Number

    8

  • 7/29/2019 Data base slldes

    9/46

    For Example

    In a Sales System, customer name and

    address are attributesof the entity

    customer.

    The primary keyis likely to be Customer

    Code or Customer ID as the unique

    identifier for each customer in the system.

    9

  • 7/29/2019 Data base slldes

    10/46

    Entity

    symbols

    Relationship

    symbols

    Attribute

    symbols

    A special

    entity that isalso a

    relationship

    Basic E-R Notation

  • 7/29/2019 Data base slldes

    11/46

    Associative Entity

    The database relational model doesn't offer direct supportto many-to-many relationships.

    The solution to this problem is the creation of another table

    to hold the necessary information for this relationship. This

    new table is called an associative entity. An Associativeentity becomes an entity and a relationship since it

    encapsulates properties from both.

    11

  • 7/29/2019 Data base slldes

    12/46

    Multivalued Attribute

    Multivalue attribute is the practice of maintaining more than a singlevalue in a database column.

    If you want to know the hobbies for a client. The proper way to handle

    this is to maintain the data in a one - to - many child table.

    Another way is a multivalue field that will contain one or more values,

    separated by a comma.

    The eMailAddress field can store a variable number of email address

    values in the single record.

    So the list [[email protected],[email protected],

    [email protected]] can be stored and accessed via a single

    query / disk read when accessing the associated record.

    phone number = {617-555-1234, 617-333-4321}

  • 7/29/2019 Data base slldes

    13/46

    Derived Attribute

    13

    Derived attributes are attributes whose

    values are generated from other attributes

    using calculations, algorithms or procedures

    For Example Employee Age derived from Date of Birth

    Account Balance derived from ??

    Work Experience derived from ??

  • 7/29/2019 Data base slldes

    14/46

    Entity with a multivalued attribute (Skill) and derived attribute

    (Years_Employed)

    Derivedfrom date employed and current date

    Whats wrong with this?

    Multivalued:an employee can have

    more than one skill

  • 7/29/2019 Data base slldes

    15/46

    Composite Attribute

    A composite attribute consists of a group of valuesfrom more than one domain. For example, the

    Address attribute consists of several domains such

    as house number, street number, city, country, etc.

    15

  • 7/29/2019 Data base slldes

    16/46

    Strong and Weak Entity

    A Weak Entity is an entity that cannot be uniquelyidentified by its attributes alone; therefore, it must use a

    foreign key in conjunction with its attributes to create a

    primary key. The foreign key is typically a primary key of

    an entity it is related to. A weak entity is part of an ER diagram that relies on other

    entities (Strong Entities). Without the strong entity, the

    weak entity wouldn't exist.

    An entity transaction has attributes transaction-number,date and amount. Different transactions on different

    accounts could share the same number.

    These are not sufficient to form a primary key

    (uniquely identify a transaction).

    Thus transaction is a weak entity set. 16

  • 7/29/2019 Data base slldes

    17/46

    Identifying Relationship

    and Relationship An identifying relationship describes a

    situation in which the existence of a row in

    the child table depends on a row in the

    parent table. An example of this that isgiven is,

    An author can write many books (1-to-n

    relationship), but a book cannot existwithout an author.

    17

  • 7/29/2019 Data base slldes

    18/46

    Scenario Example of ERD

    An Organization wants to run a homeappliances service center. Customers call to

    request a technician and the center

    Coordinator assigns an employee to sit forthe customer from a list of employees

    available for the particular day requested.

    18

  • 7/29/2019 Data base slldes

    19/46

    Service Center System

    Data is used to:

    Assign employee to job

    Determine availability

    Data to be captured in database:

    Employee data

    Job dataCustomer data

    19

  • 7/29/2019 Data base slldes

    20/46

    ERD - Service Center System

    Employee

    Job

    Customer

    assigned to Related to

  • 7/29/2019 Data base slldes

    21/46

    Service Center System

    Employee

    Attributes: EmpID, Name, Address, Phone

    Number, Available Hours, JobNo

    Job Attributes: JobNo, JobDate, JobTime,

    EmpID

    Customer Attributes: CusNo, Name, Address, Phone

    Number, JobNo

    21

  • 7/29/2019 Data base slldes

    22/46

    Entity Instance

    An entity instance is a single occurrence of anentity type.

    An entity type is described just once in a data

    model while many instances of that data typemay be represented by data stored in the

    system.

    There is one EMPLOYEE entity type inmost organisations but there may be

    hundred or thousands of instances of this

    entity type in the company.

  • 7/29/2019 Data base slldes

    23/46

    Example

    A sale always starts with a customer

    receiving an estimate.The estimate then

    becomes an order. An order can be forone or more stock items. Each stock item

    belongs to a certain stock category (e.g.

    taps, sinks, cupboards etc.)

  • 7/29/2019 Data base slldes

    24/46

    From the narrative we can deduce

    An Estimate becomes and Order

    An Estimate is issued to a particular

    Customer

    An Estimate refers to a Stock Item(s)

    An Order is placed by the Customer

    An Order refers to a Stock Item(s)

    A Stock Item refers to a particular Stock

    Category

  • 7/29/2019 Data base slldes

    25/46

    Customer

    Order

    Stock Item

    Stock

    Category

    Estimate

    Places

    Refers to

    Becomes

    Issued to

    Refers to

    Refers to

    Order System

  • 7/29/2019 Data base slldes

    26/46

    26

    Entity Primary Key Other

    Attributes

    Estimate Estimate Number Date

    Customer Number

    Stock Item Code

    Order Order Number Same as Estimate

    Customer Customer Number Customer Name

    Customer Address

    Credit Limit

    Stock Item Stock Item Code DescriptionNumber in Stock

    Supplier Code

    Stock Category Category Code Category

    Description

  • 7/29/2019 Data base slldes

    27/46

    ERD Relationships

    A relationship is an association betweentwo entities that is important to the system.

    We must also consider the extent to which

    each entity is related to another. There maybe:

    Unary Relationship

    Binary Relationship

    Ternary Relationship

    27

  • 7/29/2019 Data base slldes

    28/46

    Cardinality defines the minimum and maximumnumber of occurrences of one entity for asingle occurrence of the other

    From the previous example we can say that:

    A customer can place several orders if they

    wishBut an order can only be placed by one

    customer

    28

    The Cardinality of a Relationship

  • 7/29/2019 Data base slldes

    29/46

    Cardinality is bi-directional

    This means that it must be defined for both

    directions of the entity relationship.

    29

    Cardinality

  • 7/29/2019 Data base slldes

    30/46

    A one-to-one relationship occurs where asingle occurrence of one entity relates to one

    occurrence of a second entity.

    30

    One-to-One Relationship (1:1)

    Estimate Order

    An estimate can only become one order and an

    order is derived from one estimate.

    becomes

  • 7/29/2019 Data base slldes

    31/46

    One-to-Many relationships occur when asingle entity instance is related to many

    occurences of a second entity.

    One-to-Many Relationship(1:N)

    Customer Estimate

    One customer can be issued with many estimates (the

    relationship is described as one-to-many)

    is issued

    with

  • 7/29/2019 Data base slldes

    32/46

    Many-to-many relationships occur where manyinstances of an entity relate to many instances of a

    second entity

    Usually N:M relationships are broken into oneor more pairs of one-to-many relationships

    Many-to-Many Relationship (N:M)

  • 7/29/2019 Data base slldes

    33/46

    Many estimates refers to many stock items (or many stock

    items are referred to by many estimates)

    N:M

    Estimate Stock ItemRefers

    to

  • 7/29/2019 Data base slldes

    34/46

    Sales ERD

    Customer

    Order

    Stock Item

    Stock

    Category

    Estimate

    Places

    Refers to

    Becomes

    Issued to

    Refers to

    Refers to

  • 7/29/2019 Data base slldes

    35/46

    Optional Cardinality Where the analyst considers whether an entity

    occurrence at one end of the relationship can ever be

    present in the system without the presence of the

    corresonding occurrence of the entity at the otherend of the relationship

    Mandatory Cardinality

    When the occurrence of the entity at either end of

    the relationship must be present in the system

    35

    Cardinality

  • 7/29/2019 Data base slldes

    36/46

    Some relationships are mandatory and someare optional

    Mandatory - Mandatory

    Doctor PatientIs responsible

    for

    A doctor must be responsible for one or more patients

    and a patient must be registered with one and only one

    doctor

    Registered with

  • 7/29/2019 Data base slldes

    37/46

    37

    OptionalMandatory

    Doctor PROJECTIs responsible

    for

    A doctor may be responsible for one or more patients

    and a patiente must be registered with one and only

    one doctor

    Registered with

    d i l

  • 7/29/2019 Data base slldes

    38/46

    A doctor must be responisble for one ofmore patients and a patient may be

    registered with one and only one doctor

    MandatoryOptional

    Doctor Patient

    Is

    responsible

    for Registered with

    O i l O i l

  • 7/29/2019 Data base slldes

    39/46

    A doctor may be responsible for one ormore patients and a patient may be

    registered with one and only one doctor

    39

    OptionalOptional

    Doctor Patient

    Is

    responsible

    for Registered with

  • 7/29/2019 Data base slldes

    40/46

    Relationship CardinalityGraphic Cardinality

    Exactly 1

    0 or 1

    1 or more

    0 or many

    More than 1

    40

  • 7/29/2019 Data base slldes

    41/46

    Note

    || denotes 1 and only

  • 7/29/2019 Data base slldes

    42/46

    EE-R Data Model

    Most common feature is representation of

    supertypes and subtypes

    A popular feature of Object Oriented paradigm

  • 7/29/2019 Data base slldes

    43/46

    Super/Subtypes

    Also called generalization/ specialization

    Supertype is called a General Entity type

    whereas subtypes are the specializations

    For Example

    Wahical is Supertype and Car,Van and Bus arethe sub types.

  • 7/29/2019 Data base slldes

    44/46

    Super/Subtypes

    PERSON

    EMP FAC

    ST

    ST1 ST3ST2

    General Entity Types

    Specialized Entity Types

  • 7/29/2019 Data base slldes

    45/46

    Super/Subtypes

    EMP

    SALARIED HOURLY

    Grade

    AnalSal

    NoOfHrs

    HourlyRate

  • 7/29/2019 Data base slldes

    46/46

    Super/Subtypes

    PERSON

    STD FAC

    StId

    CGPA

    Qual

    GradeClsName

    Name

    Id

    Address