Lecture 2dfavfbagfbv

Embed Size (px)

Citation preview

  • 7/28/2019 Lecture 2dfavfbagfbv

    1/41

    Database ManagementSystems II

    Lecture 2

  • 7/28/2019 Lecture 2dfavfbagfbv

    2/41

    Last Lecture Introduction to unit

    Database Design Process

    Conceptual Modeling Enhanced ER Model

  • 7/28/2019 Lecture 2dfavfbagfbv

    3/41

    Any questions??? Class exercise?

    Practical 1?

    Tutorial 1?

  • 7/28/2019 Lecture 2dfavfbagfbv

    4/41

    This Lecture Logical Database Design

    Relational Model

    E-ER Relational Mapping

  • 7/28/2019 Lecture 2dfavfbagfbv

    5/41

    Relational Model - Review The basic construct of the relational

    model is the relation.

    A relation consists of

    relation schema

    relation instance

  • 7/28/2019 Lecture 2dfavfbagfbv

    6/41

    Relational Model Review

    (contd.) Relation Schema = relation name

    + {name of each field: domain of

    each field}

    COURSE (course_no: integer, title:

    string, credits:integer, year:integer,semester:integer)

    Relation Instance = set of tuples

  • 7/28/2019 Lecture 2dfavfbagfbv

    7/41

    Relational Model Review

    (contd.) Example

  • 7/28/2019 Lecture 2dfavfbagfbv

    8/41

    Relational Model Review

    (contd.)Arelational database is a collection

    of relations with distinctrelation names

    Integrity constraint is a rule (orcheck) that is enforced by the DBMS toensure that incorrect data is not storedin the database

  • 7/28/2019 Lecture 2dfavfbagfbv

    9/41

    Relational Model Review

    (contd.) Types of Integrity Constraints:

    Domain constraints (domain of a field)

    Key Constraints (UNIQUE & PRIMARYKEY)

    Foreign Key Constraints (FOREIGN KEY)

    Table Constraints (constraints withintables)

    Assertions (constraints between multipletables)

  • 7/28/2019 Lecture 2dfavfbagfbv

    10/41

    Relational Model Review

    (contd.) Domain constraints: specify that each

    attribute A must be an atomicvalue from the

    domain of A. Atomic value: That is, the value is non

    divisible into components

    Therefore, relational model doesnt allow multi-

    valued attributes or composite attributes!

    Relational database provides data types tospecify valid domains.

  • 7/28/2019 Lecture 2dfavfbagfbv

    11/41

    Relational Model Review

    (contd.)Key Constraints (UNIQUE & PRIMARY

    KEY)

    The minimal set of attributes thatuniquely identify a tuple is called thekey of a relation

    There may be multiple candidate keysin a relation, of which one is chosen asthe primary key.

    Primary key is underlined

  • 7/28/2019 Lecture 2dfavfbagfbv

    12/41

    Relational Model Review

    (contd.)

    LicenseNumber EngineSerialNumber

    Make Model Year

    32-3789 12342 Toyota Corolla 2000

    17-2489 22495 Nissan Sunny 1989

    54-5680 54356 Toyota Vannette 1990

    WP-1268 24322 Mitsubishi Galant 2001

    *

    VEHICLE

    Candidate keys, Super keys, Primary Key

  • 7/28/2019 Lecture 2dfavfbagfbv

    13/41

    Relational Model Review

    (contd.) Foreign Key Constraints

    We can designate an attribute(s) as a

    foreign key(s)

    Foreign key(s) attribute(s) always refer to

    attribute(s) of the same or another entity

    Foreign keys enforce referential integrity

    constraints

  • 7/28/2019 Lecture 2dfavfbagfbv

    14/41

    Relational Model Review

    (contd.)Foreign key attributes in R1 referring to R2 have the

    following rules:

    The FK attributes in R1 have the same domain(s)asthe primary key attributes if R2 The value of FK in tuple t1 in R1 must reference an

    existingPK value in tuple t2 of R2

    We can diagrammatically display the foreign keys by

    drawing an arrow from the foreign key to the primarykey

  • 7/28/2019 Lecture 2dfavfbagfbv

    15/41

    Relational Model Review

    (contd.)

    Referential Integrity Constraints displayed in the relational schema

  • 7/28/2019 Lecture 2dfavfbagfbv

    16/41

    Relational Model Review(contd.)

    Many relational DBMSs support most of the constraintsdescribed above.

    However, many applications require more advanced features for

    specifying integrity constraints.

    Table Constraints (constraints within tables) Balance of the account should be greater than 0

    In SQL, CHECK constraint can be used

    Assertions (constraints between multiple tables) For example: An employees salary should not exceed his/her

    managers salary

    Such constraints can be using specific language features such astriggers and assertions

  • 7/28/2019 Lecture 2dfavfbagfbv

    17/41

    Logical Database Design -Revisited

    Logical Database Design is the process

    of converting the E-ER Model to theRelational Model

    There are a set of conversion rules thatcan be applied

  • 7/28/2019 Lecture 2dfavfbagfbv

    18/41

    E-ER to Relational Mapping(Review)

    E-ER Model RelationalModel

    Entity Relation

    For example,STUDENT STUDENT

  • 7/28/2019 Lecture 2dfavfbagfbv

    19/41

    E-ER to Relational Mapping(Review contd.)

    E-ER Model RelationalModel

    Simple Attributes Attributes

    For example,

    STUDENT

    name

    nameSTUDENT

  • 7/28/2019 Lecture 2dfavfbagfbv

    20/41

    E-ER to Relational Mapping(Review contd.)

    E-ER Model RelationalModel

    Primary Key Primary Key

    For example,

    STUDENT

    name

    nameSTUDENT

  • 7/28/2019 Lecture 2dfavfbagfbv

    21/41

    E-ER to Relational Mapping(Review contd.)

    E-ER Model Relational Model

    Composite attributes Set of simple

    attributes

    For example,

    EMPLOYEE

    fullname

    surname

    firstname

    first-name

    sur-name

    EMPLOYEE

  • 7/28/2019 Lecture 2dfavfbagfbv

    22/41

    E-ER to Relational Mapping(Review contd.)

    E-ER Model Relational Model

    1:1 relationship Foreign key

    relationship

    For example,

    EMPLOYEE

    ADDRESShas

    id

    a

    idEMPLOYEE

    aeidADDRESS

  • 7/28/2019 Lecture 2dfavfbagfbv

    23/41

    E-ER to Relational Mapping(Review contd.)

    Usually, if there is a 1:1 relationship Rfrom entity A to B and if B is in total

    participation with A on R then theforeign key is placed in B

    For example,

    A

    B

    R

    A

    B

  • 7/28/2019 Lecture 2dfavfbagfbv

    24/41

    E-ER to Relational Mapping(Review contd.)

    If A and B are both in total participation with

    R then A & B are collapsed as 1 table

    For example,

    A

    BR

    AB

  • 7/28/2019 Lecture 2dfavfbagfbv

    25/41

    E-ER to Relational Mapping(Review contd.)

    E-ER Model Relational Model

    1:N Foreign key

    relationship

    For example,

    ARTIST

    OBJECTScreates

    id

    yr_

    created

    id

    yr_createdid

    ARTIST

    OBJECT

  • 7/28/2019 Lecture 2dfavfbagfbv

    26/41

    E-ER to Relational Mapping(Review contd.)

    E-ER Model Relational Model

    M:N Relationship

    relation and twoforeign keys

    ARTIST

    ART-OBJECTcreates

    name

    Date-

    Created

    id

    name

    id

    id Datecreated

    nameCREATES

    ART_OBJECT

    ARTIST

  • 7/28/2019 Lecture 2dfavfbagfbv

    27/41

    E-ER to Relational Mapping(Review contd.)

    E-ER Model Relational Model

    Multivalued attribute Relation &Foreign Key

    For example,

    STUDENT

    middle namesid

    id

    middle-nameid

    STUDENT

    MIDDLE_NAMES

  • 7/28/2019 Lecture 2dfavfbagfbv

    28/41

    E-ER Model Relational Model

    N-ary relationship Relationshiprelation and nforeign keys

    For example,

    A

    C

    B

    D

    r

    pkC

    pkA pkB

    pkD

    pkDpkCpkBpkA

    A B

    R

    C

    D

    E-ER to Relational Mapping

    (Review contd.)

  • 7/28/2019 Lecture 2dfavfbagfbv

    29/41

    E-ER to Relational Mapping(contd.)

    E-ER Model Relational Model

    Subclass/superclass Multi relation options:

    - Option 1- Option 2

    Single relation options:

    - Option 3- Option 4

  • 7/28/2019 Lecture 2dfavfbagfbv

    30/41

    E-ER to Relational Mapping(contd.)

    C is a super class with primary key, PK(C) = k

    Attributes of C = Attrs(C) = {k, a1, , an}

    S1

    , S2

    , , Sm

    are subclasses of C

    C

    k a1 a2 an

    S1 S2 Sm

    ISA

  • 7/28/2019 Lecture 2dfavfbagfbv

    31/41

    E-ER to Relational Mapping(contd.)

    Option 1:

    Create a relation L for superclass C suchthat Attrs(L) = {k, a1, a2, , an} andPK(L) = k

    For each Si, create a relation Li suchthat Attrs(Li) = {k} U Attrs(Si) andPK(Li) = k

  • 7/28/2019 Lecture 2dfavfbagfbv

    32/41

    E-ER to Relational Mapping(contd.)

    Relation L for class C

    Relation Li for each subclass Si

    Foreign key link from PK(Li) to PK(L)

    EQUIJOIN between Li and L to obtain all attributes ofLi

    Option 1 works for all constraints disjoint,overlapping, total and partial.

  • 7/28/2019 Lecture 2dfavfbagfbv

    33/41

    E-ER to Relational Mapping(contd.)

    Option 2:

    For each subclass Si, create a newrelation Li such that Attrs(Li) = Attrs(C)U Attrs(Li) and PK(Li) = k

  • 7/28/2019 Lecture 2dfavfbagfbv

    34/41

    E-ER to Relational Mapping(contd.)

    The ISA relationship must be total (i.e.subclasses must cover the super class)

    If the subclasses are not disjoint, wekeep multiple copies of Cs attributesvalues

    To obtain all entities in superclass C, weneed to perform a full outer join withall relations Li

  • 7/28/2019 Lecture 2dfavfbagfbv

    35/41

    E-ER to Relational Mapping(contd.)

    Option 3:

    Create a single relation L with Attrs(L) =Attrs(C) U Attrs(S1) U U Attrs(Sm) U {t}such that PK(L) = k where t is type specifyingwhich subclass the entity belongs if any.

    The specialization/generalization relationshipmust be disjoint

  • 7/28/2019 Lecture 2dfavfbagfbv

    36/41

    E-ER to Relational Mapping(contd.)

    Good if subclasses have fewattributes

    No EQUIJOIN or OUTER JOIN operationneeded

    If subclasses have a lot of attributes,potential for many null values

  • 7/28/2019 Lecture 2dfavfbagfbv

    37/41

    E-ER to Relational Mapping(contd.)

    Option 4:

    Create a single relation L with Attrs(L) =Attrs(C) U Attrs(S1) U U Attrs(Sm) U {t1, t2,, tm} such that PK(L) = k where ti is aBoolean attribute indicating whether each

    tuple belongs to subclass Si.

    This relation allows overlapping constraintsfor specialization/generalization relationship

  • 7/28/2019 Lecture 2dfavfbagfbv

    38/41

    E-ER to Relational Mapping(contd.)

    When having multilevel specialization(or generalization) hierarchy, we do not

    need to follow the same mappingoption.

    We can use different mapping optionsfor different parts of the hierarchy

  • 7/28/2019 Lecture 2dfavfbagfbv

    39/41

    E-ER to Relational Mapping(contd.)

    Aggregation

    Relationship has M:N cardinality

    Entity A Entity BR1

    R2 Entity C

    PKA

    A

    PKB

    B

    PKC

    C

    PKBPKA

    R1

    PKB PKcPKA

    R2

  • 7/28/2019 Lecture 2dfavfbagfbv

    40/41

    E-ER to Relational Mapping(contd.)

    Relationship has 1:M cardinality

    Entity A Entity BR1

    R2 Entity C

    PKA

    A

    PKAPKB

    B

    PKC

    C

    PKB PKc

    R2

  • 7/28/2019 Lecture 2dfavfbagfbv

    41/41

    Summary

    Relational Model

    Logical Database Design

    Enhanced ER Model Relational Model

    Practicals and Tutes will cover exercisesin these topics