Database Chapter02-01

download Database Chapter02-01

of 51

Transcript of Database Chapter02-01

  • 8/10/2019 Database Chapter02-01

    1/51

    CHAPTER 2

    DATABASE DESIGN

    Database Application

    SAK 3408

    1

  • 8/10/2019 Database Chapter02-01

    2/51

    Learning Objectives (W2)

    Describe the database design phases.

    Explain on how to construct E-R diagram.

    Transform ERD into Relation.

    Use normalization to decompose a relationwith anomalies into well-structured relations.

    2

  • 8/10/2019 Database Chapter02-01

    3/51

    The goal of any information system is toadd value for the user.

    Reduce costs

    Increase sales/revenue

    Provide competitive advantage The need for database design:

    Flexible enough to handle future needs

    Future developers to understand the

    system To communicate with users and IT staff

    The important step of all methodologies isto build models of the system.

    Introduction

    Design

    SQL

    Program

    Best:

    Spend your time

    on design and SQL.

    3

  • 8/10/2019 Database Chapter02-01

    4/51

    Database System Design

    User views

    of data.

    Conceptual

    data model.

    Customer(CustomerID, Name, Address, )

    SalesPerson(EmployeeID, Name, Commission, )

    Order(OrderID, OrderDate, CustomerID, EmployeeID, )

    OrderItem(OrderID, ItemID, Quantity, Price, )

    Item(ItemID, Description, ListPrice, )

    Implementation

    (relational)

    data model.

    Physical

    data

    storage.

    Class diagram

    that shows

    businessentities,

    relationships,

    and rules.

    List of nicely-

    behaved tables.

    Use data

    normalization to

    derive the list.

    Indexes and

    storage methods

    to improve

    performance.

    4

  • 8/10/2019 Database Chapter02-01

    5/51

    Initiation Scope Feasibility Cost & Time estimates

    Requirements Analysis User Views & Needs

    Forms Reports

    Processes & Events Objects & Attributes

    Conceptual Design Models

    Data flow diagram Entity Relationships Objects

    User feedback

    Physical Design Table definitions Application development

    Queries Forms Reports Application integration

    Data storage Security Procedures

    Implementation

    Training Purchases Data conversion Installation

    Evaluation & Review

    Design Stages5

  • 8/10/2019 Database Chapter02-01

    6/51

    Initial Steps of Design

    1. Identify the exact goals of the system.

    2. Talk with the users to identify the basic forms

    and reports.

    3. Identify the data items to be stored.

    4. Design the classes (tables) and relationships.

    5. Identify any business constraints.

    6. Verify the design matches the business rules.

    6

  • 8/10/2019 Database Chapter02-01

    7/51

    E-R modela logical representation of the

    data for an organization or for a business

    area

    E-R diagrama graphical representation of

    an entity-relationship model

    The E-R Model7

  • 8/10/2019 Database Chapter02-01

    8/51

    Variations on ER Attribute

    Display8

    PATIENT

    PAT_idnumber

    PAT_firstname

    PAT_lastname

    PAT_addressPAT_city

    PAT_state

    PAT_birthdate

    PAT_zipcode

    PAT_homephone

    Attributes in Ellipse

  • 8/10/2019 Database Chapter02-01

    9/51

    9

    PATIENTPAT_idnumber

    PAT_firstname

    PAT_lastname

    PAT_address

    PAT_city

    PAT_state

    PAT_zipcode

    PAT_homephone

    PAT_birthdate

    Variations on ER Attribute

    DisplayPATIENT

    PAT_idnumber

    PAT_firstname

    PAT_lastname

    PAT_addressPAT_city

    PAT_state

    PAT_birthdate

    PAT_zipcode

    PAT_homephone

    Attributes in Rectangle

  • 8/10/2019 Database Chapter02-01

    10/51

    Variations on Level of Entity Attribute

    Displays10

    PatientPAT_idnumber

    PAT_firstname

    PAT_lastnamePAT_address

    PAT_city

    PAT_state

    PAT_zipcode

    PAT_homephone

    PAT_birthdate

    PatientPAT_idnumber PATIENT

    Entity with All Attributes

    Entity with Identifier

    Attribute OnlyEntity with No Attributes

  • 8/10/2019 Database Chapter02-01

    11/51

    Entity

    person, place, object, event or concept in the user

    environment about which data is to be maintained.

    Attribute property or characteristic of an entity type

    Relationship

    association between the instances of one or moreentity types i.e. link between entities

    E-R Model Constructs11

  • 8/10/2019 Database Chapter02-01

    12/51

    Entity Identity Example

    Person STAF, STUDENT, LECTURER

    Place DISTRICT, TOWN, STATE

    ObjectEvent

    Concept

    BUILDING, TOOL, PRODUCT

    COURSE, ACCOUNT

    REGISTRATION, APPLICATION

    Example of Entity types according to its identity

    Entity Type

    Entity type represents a set or collection of objects

    in the real world that share the same properties.

    12

  • 8/10/2019 Database Chapter02-01

    13/51

    ER VARIATIONS - CHEN

    MODEL

    PROFESOR CLASSteaches

    profID profName

    classID

    className

    EntityName Verb Phrase AttributeName

    Example:

  • 8/10/2019 Database Chapter02-01

    14/51

    ER VARIATIONS - CROWS FOOT

    MODEL14

    name>

    Entity

    Attribute

    Relationship

    Verb phr ase

    teaches

    PROFESSOR

    profName

    CLASS

    classID

    Example:

    profID

    className

  • 8/10/2019 Database Chapter02-01

    15/51

    Sample of E-R Diagram

    entity relationship

    identifier

    15

    attribute

  • 8/10/2019 Database Chapter02-01

    16/51

    Sample of E-R Diagram16

  • 8/10/2019 Database Chapter02-01

    17/51

    Entitysymbols

    Relationship

    symbols

    Attribute

    symbols

    A special entitythat is also a

    relationship

    Basic E-R Notation17

  • 8/10/2019 Database Chapter02-01

    18/51

    Entity Type vs. Entity Instance

    Entity Type- collection of entities (often

    corresponds to a table)

    Entity instance- a single occurrence of an entitytype(often corresponds to a row in a table)

    Strong vs. Weak Entity Types

    Strong Entity Typeentity that exists

    independently of other entity types Weak Entity Typeentity type whose existence

    depends on some other entity type

    Entity18

  • 8/10/2019 Database Chapter02-01

    19/51

    75115 Farazila Yusof

    75794 Wan Haniza Wan Hassim

    76740 Norwani Mohd Zain

    77388 Zarawahida Zakaria

    77512 Rozita Bt Bahamad

    77558 Armamorhana Ahmad

    77655 Siti Amrah Zakaria

    78436 Siti Nor Haiza Mad Janapi

    78447 Noriati Jaminah84781 Emilza Fuad

    84817 Azli Kamarudzaman

    84858 Faddly Mazlan

    84904 Balkhis Diba Mansor

    85013 Aina Najmi Shari

    85042 Ku Irdawaty Ku Yaakob

    Rows represented

    entity instances of

    the entity type

    Entity type: STUDENT

    Example of Entity Type vs. Entity

    Instance19

  • 8/10/2019 Database Chapter02-01

    20/51

    Naming and Defining Entity

    Use singular noun.

    Should be descriptive and specific. For example: PURCHASE_ORDER and

    CUSTOMER_ORDER cannot be named as ORDER

    Should be concise. For example:

    REGISTRATION_FOR_STUDENT_IN_CLASS

    Should be named for the result of the event, notthe activity or process of the event. Forexample: an event of a project managerassigning an employee to work on a project

    20

  • 8/10/2019 Database Chapter02-01

    21/51

    Simple vs. Composite Attribute

    Simple attributecannot broken into smaller

    components

    Composite attributecan broken into component parts

    Single-Valued vs. Multivalued Attribute

    Single-Valuedeach of the attributes has one value

    Multivaluedattribute more than one value

    Stored vs. Derived Attributes

    Stored attributedata input or set

    Derived Attributeattribute whose values can be

    calculated from related attribute values.

    Identifier Attributes

    Attribute21

  • 8/10/2019 Database Chapter02-01

    22/51

    An attribute

    broken into

    component parts

    An Example of Composite

    Attribute22

  • 8/10/2019 Database Chapter02-01

    23/51

    Entity with a multivalued attribute (Skill) and derivedattribute (Years_Employed)

    Derivedfrom date employed and current date

    Multivalued:an employee can have

    more than one skill

    23

  • 8/10/2019 Database Chapter02-01

    24/51

    Identifier (Primary Key)- An attribute (or

    combination of attributes) that uniquely

    identifies individual instances of an entity

    type

    Composite Identifieran identifier that

    consists of a composite attribute.

    Candidate Keyan attribute that could be akeysatisfies the requirements for being a

    key

    Identifier (Key)24

  • 8/10/2019 Database Chapter02-01

    25/51

    Will not change in value

    Will not be null

    No intelligent identifiers whose structure

    indicates classification, location etc. (e.g.containing locations or people that might

    change)

    Substitute new, simple keys for long,composite keys

    Characteristic of Identifier25

  • 8/10/2019 Database Chapter02-01

    26/51

    Simple Key Attribute

    The key is underlined

    26

  • 8/10/2019 Database Chapter02-01

    27/51

    Composite Key Attribute

    The key is composed

    of two subparts

    27

  • 8/10/2019 Database Chapter02-01

    28/51

    Relationship

    Author Book

    Relationship name:writes

    An author writes one or more books

    A book can be written by one or more authors.

    Associations between instances of one or

    more entity types that is of interest

    Given a name that describes its function.

    relationship name is an active or a passive verb.

  • 8/10/2019 Database Chapter02-01

    29/51

    Relationship Types vs. Relationship Instances The relationship type is modeled as the diamond and

    lines between entity types whereas the relationshipinstance is between specific entity instances

    Relationships can have attributes These describe features pertaining to the association

    between the entities in the relationship

    Two entities can have more than one type ofrelationship between them (multiplerelationships)

    Associative Entity = combination of relationship

    and entity

    Relationship29

  • 8/10/2019 Database Chapter02-01

    30/51

    Relationship type

    Entity and Relationship instances

    30

  • 8/10/2019 Database Chapter02-01

    31/51

    Degree of a Relationship is the number of

    entity types that participate in it

    Unary Relationship

    Binary Relationship Ternary Relationship

    Degree of Relationship31

  • 8/10/2019 Database Chapter02-01

    32/51

    Degree of Relationship ..cont.

    One entityrelated to

    another of

    the same

    entity type

    Entities of

    two differenttypes related

    to each otherEntities of three

    different types

    related to each

    other

    32

  • 8/10/2019 Database Chapter02-01

    33/51

    OnetoOne

    Each entity in the relationship will have exactly one

    related entity

    OnetoManyAn entity on one side of the relationship can have

    many related entities, but an entity on the other

    side will have a maximum of one related entity

    ManytoMany

    Entities on both sides of the relationship can have

    many related entities on the other side

    Cardinality of Relationship33

  • 8/10/2019 Database Chapter02-01

    34/51

    Degree of relationships and Cardinality

    (a) Unary relationships

    34

  • 8/10/2019 Database Chapter02-01

    35/51

    (b) Binary relationships35

  • 8/10/2019 Database Chapter02-01

    36/51

    A simultaneous relationship between instances of threeentity types.

    The above E-R diagram shows that: A student can register many courses and a course can be

    registered by many students.

    A student can have many lectures and a lecture can havemany students.

    A lecturer can teach many course and a course can be taughtby many lecturers.

    STUDENT COURSE

    LECTURER

    36

    (c) Ternary relationships (1)

    registers

  • 8/10/2019 Database Chapter02-01

    37/51

    Transforming a ternary relationships into a binary relationships

    STUDENT COURSE

    LECTURER

    37

    Binary relationships

    registers

    has teaches

  • 8/10/2019 Database Chapter02-01

    38/51

    (c) Ternary relationships (2)

    Note: a relationship can have attributes of its own

    38

  • 8/10/2019 Database Chapter02-01

    39/51

    Cardinality Constraints - the number of instances

    of one entity that can or must be associated with

    each instance of another entity.

    Minimum Cardinality If zero, then optional

    If one or more, then mandatory

    Maximum Cardinality The maximum number

    Cardinality Constraints39

  • 8/10/2019 Database Chapter02-01

    40/51

    Cardinality Mandatory and Optional40

  • 8/10/2019 Database Chapter02-01

    41/51

    (a) Basic relationship with only maximum cardinalities showing

    (b) Mandatory minimum cardinalities

    Cardinality Constraints41

  • 8/10/2019 Database Chapter02-01

    42/51

    Examples of multiple relationships

    Employees and departmentsentities can be related to one

    another in more than one way

    42

  • 8/10/2019 Database Chapter02-01

    43/51

    Strong vs. Weak Entities, and

    Identifying Relationships

    Strong entities

    exist independently of other types of entities

    has its own unique identifier

    represented with single-line rectangle

    Weak entity dependent on a strong entitycannot exist on its own

    Does not have a unique identifier

    represented with double-line rectangle Identifying relationship links strong entities to weak entities

    represented with double line diamond

    43

  • 8/10/2019 Database Chapter02-01

    44/51

    Strong and Weak Entity

    Strong entity Weak entityIdentifying relationship

    44

  • 8/10/2019 Database Chapter02-01

    45/51

    Its anentityit has attributes

    AND its arelationshipit links entities together When should a relationship with attributesinstead be an

    associative entity?

    All relationships for the associative entity should be many

    The associative entity could have meaning independent of

    the other entities

    The associative entity preferably has a unique identifier, andshould also have other attributes

    The associative may be participating in other relationships

    other than the entities of the associated relationship

    Ternary relationships should be converted to associative

    entities

    Associative Entity45

  • 8/10/2019 Database Chapter02-01

    46/51

    46

    An Example of Associative Entity

  • 8/10/2019 Database Chapter02-01

    47/51

    An Example of Associative Entity -

    CERTIFICATE

    Associative entity involves a rectangle with a diamond inside.

    Note that the many-to-many cardinality symbols face toward

    the associative entity and not toward the other entities

    47

  • 8/10/2019 Database Chapter02-01

    48/51

    (b)Ternary relationship as an associative entity

    48

  • 8/10/2019 Database Chapter02-01

    49/51

    For each pair of sentences, identify entitiesand the relationship representing thesentences. "Each student must take one or more modules"

    "Each module must be taken by one or morestudent(s)"

    "Each customer must receive at least onedelivery"

    "Each delivery must be only for one customer" "A client my have an account manager"

    Each account manager has only one client"

    ERD Exercise (1)49

  • 8/10/2019 Database Chapter02-01

    50/51

    An automobile insurance company needs tokeep track of information about vehiclepolicyholders. The company has to storeinformation of customer, car and accident.

    Customer information is License_no, nameand address. Customers can own one or morecars, where the car information is Plate_no,model and year. A car not involved or involvedin many accidents. If the car is involved in an

    accident the information that will be stored isReport_number, location and date. Each carthat is involved in accident the damageamount will be estimate.

    Draw the ERD.

    ERD Exercise (2)50

  • 8/10/2019 Database Chapter02-01

    51/51

    ERD Exercise (3)

    A small company that rents canoes needs a database to track basic informationabout the rental and the canoes. Eventually, the firm wants to identify customers who

    cause problems by damaging the canoes, but for now, the managers just want to

    track the costs. The managers have outlined the data as a form. Draw an ER

    diagram for this case.

    51