+ All Categories
Home > Documents > Logik Desain Dan Relasional

Logik Desain Dan Relasional

Date post: 05-Apr-2018
Category:
Upload: syahril-rukli
View: 228 times
Download: 0 times
Share this document with a friend

of 47

Transcript
  • 7/31/2019 Logik Desain Dan Relasional

    1/47

    1 Prentice Hall, 2002

    Chapter 5:Logical Database Designand the Relational Model

    Modern Database Management

    6th Edition

    Jeffrey A. Hoffer, Mary B. Prescott, Fred R.McFadden

  • 7/31/2019 Logik Desain Dan Relasional

    2/47

    Chapter 5 2 Prentice Hall, 2002

    Relation

    Definition: A relation is a named, two-dimensional table of data Table is made up of rows (records), and columns (attribute or field)

    Not all tables qualify as relations

    Requirements: Every relation has a unique name.

    Every attribute value is atomic (not multivalued, not composite)

    Every row is unique (cant have two rows with exactly the same valuesfor all their fields)

    Attributes (columns) in tables have unique names

    The order of the columns is irrelevant The order of the rows is irrelevant

    NOTE: all relations are in 1st Normal form

  • 7/31/2019 Logik Desain Dan Relasional

    3/47

    Chapter 5 3 Prentice Hall, 2002

    Correspondence with ER

    Model Relations (tables) correspond with entity and with

    many-to-many relationship

    Rows correspond with entity instances and with many-to-many relationship instances

    Columns correspond with attributes

    NOTE: The wordrelation (in relational database) isNOT the same same the wordrelationship (in ERmodel)

  • 7/31/2019 Logik Desain Dan Relasional

    4/47

    Chapter 5 4 Prentice Hall, 2002

    Key FieldsKeys are special fields that serve two main purposes:

    Primary keys are unique identifiers of the relation inquestion. Examples include employee numbers, socialsecurity numbers, etc. This is how we can guarantee thatall rows are unique

    Foreign keys are identifiers that enable a dependentrelation (on the many side of a relationship) to refer to itsparent relation (on the one side of the relationship)

    Keys can besimple (a single field) orcomposite(more than one field)

  • 7/31/2019 Logik Desain Dan Relasional

    5/47

    Chapter 5 5 Prentice Hall, 2002

    Figure 5-3 -- Schema for four relations (Pine Valley Furniture)

    Primary Key

    Foreign Key(implements 1:N relationship

    between customer and order)

    Combined, these are a composite

    primary key (uniquely identifies the

    order line)individually they are

    foreign keys (implement M:N

    relationship between order andproduct)

  • 7/31/2019 Logik Desain Dan Relasional

    6/47

    Chapter 5 6 Prentice Hall, 2002

    Integrity Constraints

    Domain Constraints

    Allowable values for an attribute.

    Entity Integrity

    No primary key attribute may be null. All primary

    key fields MUST have data

  • 7/31/2019 Logik Desain Dan Relasional

    7/47

    Chapter 5 7 Prentice Hall, 2002

    Integrity Constraints

    Referential Integrityrule that states that any foreign key value (on

    the relation of the many side) MUST match a primary key value in the

    relation of the one side. (Or the foreign key can be null)

    For example: Delete Rules

    Restrictdont allow delete of parent side if related rows exist in

    dependent side

    Cascadeautomatically delete dependent side rows that correspond with

    the parent side row to be deleted Set-to-Nullset the foreign key in the dependent side to null if deleting

    from the parent side not allowed for weak entities

  • 7/31/2019 Logik Desain Dan Relasional

    8/47

    Chapter 5 8 Prentice Hall, 2002

    Figure 5-5:Referential integrity constraints (Pine Valley Furniture)

    Referential

    integrityconstraints are

    drawn via arrows

    from dependent to

    parent table

  • 7/31/2019 Logik Desain Dan Relasional

    9/47

    Chapter 5 9 Prentice Hall, 2002

    Transforming EER Diagrams

    into RelationsMapping Regular Entities to Relations

    1. Simple attributes: E-R attributes map directly

    onto the relation

    2. Composite attributes: Use only their simple,

    component attributes

    3. Multi-valued Attribute - Becomes a separaterelation with a foreign key taken from the

    superior entity

  • 7/31/2019 Logik Desain Dan Relasional

    10/47

    Chapter 5 10 Prentice Hall, 2002

    (a) CUSTOMERentity type withsimpleattributes

    Figure 5-8: Mapping a regular entity

    (b) CUSTOMER relation

  • 7/31/2019 Logik Desain Dan Relasional

    11/47

    Chapter 5 11 Prentice Hall, 2002

    (a) CUSTOMER

    entity type withcompositeattribute

    Figure 5-9: Mapping a composite attribute

    (b) CUSTOMER relation with address detail

  • 7/31/2019 Logik Desain Dan Relasional

    12/47

    Chapter 5 12 Prentice Hall, 2002

    Figure 5-10: Mapping a multivalued attribute

    1tomany relationship between original entity and new relation

    (a)

    Multivalued attribute becomes a separate relation with foreign key

    (b)

  • 7/31/2019 Logik Desain Dan Relasional

    13/47

    Chapter 5 13 Prentice Hall, 2002

    Transforming EER Diagrams

    into RelationsMapping Weak Entities

    Becomes a separate relation with a

    foreign key taken from the superior entity

    Primary key composed of:

    Partial identifier of weak entity

    Primary key of identifying relation (strong

    entity)

  • 7/31/2019 Logik Desain Dan Relasional

    14/47

    Chapter 5 14 Prentice Hall, 2002

    Figure 5-11: Example of mapping a weak entity

    (a) Weak entity DEPENDENT

  • 7/31/2019 Logik Desain Dan Relasional

    15/47

    Chapter 5 15 Prentice Hall, 2002

    Figure 5-11(b) Relations resulting from weak entity

    NOTE: the domain constraint

    for the foreign key should

    NOT allow null value if

    DEPENDENT is a weak

    entity

    Foreign key

    Composite primary key

  • 7/31/2019 Logik Desain Dan Relasional

    16/47

    Chapter 5 16 Prentice Hall, 2002

    Transforming EER Diagrams

    into RelationsMapping Binary Relationships

    One-to-Many - Primary key on the one side

    becomes a foreign key on the many side

    Many-to-Many - Create anew relation with the

    primary keys of the two entities as its primary

    key One-to-One - Primary key on the mandatory

    side becomes a foreign key on the optional side

  • 7/31/2019 Logik Desain Dan Relasional

    17/47

    Chapter 5 17 Prentice Hall, 2002

    Figure 5-12: Example of mapping a 1:M relationship

    (a) Relationship between customers and orders

    Note the mandatory one

  • 7/31/2019 Logik Desain Dan Relasional

    18/47

    Chapter 5 18 Prentice Hall, 2002

    Figure 5-12(b) Mapping the relationship

    Again, no null value in theforeign keythis is because

    of the mandatory minimum

    cardinality

    Foreign key

  • 7/31/2019 Logik Desain Dan Relasional

    19/47

    Chapter 5 19 Prentice Hall, 2002

    Figure 5-13: Example of mapping an M:N relationship

    (a) ER diagram (M:N)

    The Supplies relationship will need to become a separate relation

  • 7/31/2019 Logik Desain Dan Relasional

    20/47

    Chapter 5 20 Prentice Hall, 2002

    Figure 5-13(b) Three resulting relations

    New

    intersection

    relationForeign key

    Foreign key

    Composite primary key

  • 7/31/2019 Logik Desain Dan Relasional

    21/47

    Chapter 5 21 Prentice Hall, 2002

    Figure 5-14: Mapping a binary 1:1 relationship

    (a) Binary 1:1 relationship

  • 7/31/2019 Logik Desain Dan Relasional

    22/47

    Chapter 5 22 Prentice Hall, 2002

    Figure 5-14(b) Resulting relations

  • 7/31/2019 Logik Desain Dan Relasional

    23/47

    Chapter 5 23 Prentice Hall, 2002

    Transforming EER Diagrams

    into RelationsMapping Unary Relationships

    One-to-Many - Recursive foreign key in the

    same relation

    Many-to-Many - Two relations:

    One for the entity type

    One for an associative relation in which theprimary key has two attributes, both taken

    from the primary key of the entity

  • 7/31/2019 Logik Desain Dan Relasional

    24/47

    Chapter 5 24 Prentice Hall, 2002

    Figure 5-17: Mapping a unary 1:N relationship

    (a) EMPLOYEE entity with

    Manages relationship

    (b) EMPLOYEE

    relation with

    recursive foreign

    key

  • 7/31/2019 Logik Desain Dan Relasional

    25/47

    Chapter 5 25 Prentice Hall, 2002

    Figure 5-18: Mapping a unary M:N relationship

    (a) Bill-of-materials

    relationships (M:N)

    (b) ITEM and

    COMPONENT

    relations

  • 7/31/2019 Logik Desain Dan Relasional

    26/47

    Chapter 5 26 Prentice Hall, 2002

    Transforming EER Diagrams

    into RelationsMapping Ternary (and n-ary)

    Relationships

    One relation for each entity and one

    for the associative entity

    Associative entity has foreign keysto each entity in the relationship

    Figure 5 19: Mapping a ternary relationship

  • 7/31/2019 Logik Desain Dan Relasional

    27/47

    Chapter 5 27 Prentice Hall, 2002

    Figure 5-19: Mapping a ternary relationship

    (a) Ternary relationship with associative entity

  • 7/31/2019 Logik Desain Dan Relasional

    28/47

    Chapter 5 28 Prentice Hall, 2002

    Figure 5-19(b) Mapping the ternary relationship

    Remember that the

    primary key MUST be

    unique

  • 7/31/2019 Logik Desain Dan Relasional

    29/47

    Chapter 5 29 Prentice Hall, 2002

    Transforming EER Diagramsinto Relations

    Mapping Supertype/Subtype Relationships

    One relation for supertype and for each subtype

    Supertype attributes (including identifier and

    subtype discriminator) go into supertype relation Subtype attributes go into each subtype; primary

    key of supertype relation also becomes primarykey of subtype relation

    1:1 relationship established between supertypeand each subtype, with supertype as primarytable

  • 7/31/2019 Logik Desain Dan Relasional

    30/47

    Chapter 5 30 Prentice Hall, 2002

    Figure 5-21:Mapping Supertype/subtype relationships to relations

  • 7/31/2019 Logik Desain Dan Relasional

    31/47

    Chapter 5 31 Prentice Hall, 2002

    Figure 5-20: Supertype/subtype relationships

  • 7/31/2019 Logik Desain Dan Relasional

    32/47

    Chapter 5 32 Prentice Hall, 2002

    Data Normalization

    Primarily a tool to validate and improve a

    logical design so that it satisfies certain

    constraints thatavoid unnecessaryduplication of data

    The process of decomposing relations with

    anomalies to produce smaller, well-structuredrelations

  • 7/31/2019 Logik Desain Dan Relasional

    33/47

    Chapter 5 33 Prentice Hall, 2002

    Well-Structured Relations

    A relation that contains minimal data redundancyand allows users to insert, delete, and update rowswithout causing data inconsistencies

    Goal is to avoid anomalies

    Insertion Anomalyadding new rows forces user tocreate duplicate data

    Deletion Anomalydeleting rows may cause a loss ofdata that would be needed for other future rows

    Modification Anomalychanging data in a row forceschanges to other rows because of duplication

    General rule of thumb: a table should not pertain to

    more than one entity type

    E l Fi 5 2b

  • 7/31/2019 Logik Desain Dan Relasional

    34/47

    Chapter 5 34 Prentice Hall, 2002

    Example Figure 5.2b

    QuestionIs this a relation?AnswerYes: unique rows and no multivalued

    attributes

    QuestionWhats the primary key? AnswerComposite: Emp_ID,Course_Title

  • 7/31/2019 Logik Desain Dan Relasional

    35/47

  • 7/31/2019 Logik Desain Dan Relasional

    36/47

    Chapter 5 36 Prentice Hall, 2002

    Functional Dependencies and Keys

    Functional Dependency: The value of oneattribute (thedeterminant) determines thevalue of another attribute

    Candidate Key:

    A unique identifier. One of the candidate keyswill become the primary key

    E.g. perhaps there is both credit card number andSS# in a tablein this case both are candidate keys

    Each non-key field is functionally dependent onevery candidate key

  • 7/31/2019 Logik Desain Dan Relasional

    37/47

    Chapter 5 37 Prentice Hall, 2002

    5.22 -Steps innormalization

  • 7/31/2019 Logik Desain Dan Relasional

    38/47

    Chapter 5 38 Prentice Hall, 2002

    First Normal Form

    No multivalued attributes

    Every attribute value is atomic

    Fig. 5-2a is notin 1st Normal Form(multivalued attributes) it is not arelation

    Fig. 5-2b is in 1st Normal form

    All relations are in 1st Normal Form

    Figure 5 2 a- Bukan relasi

  • 7/31/2019 Logik Desain Dan Relasional

    39/47

    Chapter 5 39 Prentice Hall, 2002

    Figure 5.2 a- Bukan relasi

    Figure 5.2 b- relasi

  • 7/31/2019 Logik Desain Dan Relasional

    40/47

    Chapter 5 40 Prentice Hall, 2002

    Second Normal Form

    1NFplusevery non-key attribute is fullyfunctionally dependent on the ENTIREprimary key

    Every non-key attribute must be defined by theentire key, not by only part of the key

    No partial functional dependencies

    Fig. 5-2b is NOT in 2nd Normal Form (seefig 5-23b)

    Fig 5 23(b) Functional

  • 7/31/2019 Logik Desain Dan Relasional

    41/47

    Chapter 5 41 Prentice Hall, 2002

    Fig 5.23(b)

    Functional

    Dependencies in EMPLOYEE2

    EmpID CourseTitle DateCompletedSalaryDeptNameName

    Dependency on entire primary key

    Dependency on onlypartof the key

    EmpID, CourseTitle DateCompletedEmpID Name, DeptName, Salary

    Therefore, NOT in 2nd Normal Form!!

  • 7/31/2019 Logik Desain Dan Relasional

    42/47

    Chapter 5 42 Prentice Hall, 2002

    Getting it into 2nd Normal Form

    decomposed into two separate relations

    EmpID SalaryDeptNameName

    CourseTitle DateCompletedEmpID

    Both are fullfunctional

    dependencies

  • 7/31/2019 Logik Desain Dan Relasional

    43/47

    Chapter 5 43 Prentice Hall, 2002

    Third Normal Form

    2NF PLUSno transitive dependencies(one attribute functionally determines a

    second, which functionally determines athird)

    Fig. 5-24, 5-25

  • 7/31/2019 Logik Desain Dan Relasional

    44/47

    Chapter 5 44 Prentice Hall, 2002

    Figure 5-24 -- Relation with transitive dependency

    (a) SALES relation with simple data

  • 7/31/2019 Logik Desain Dan Relasional

    45/47

    Chapter 5 45 Prentice Hall, 2002

    Figure 5-24(b) Relation with transitive dependency

    CustID Name

    CustID Salesperson

    CustID Region

    All this is OK

    (2nd NF)

    BUTCustID Salesperson Region

    Transitive dependency

    (not 3rdNF)

  • 7/31/2019 Logik Desain Dan Relasional

    46/47

    Chapter 5 46 Prentice Hall, 2002

    Figure 5.25 -- Removing a transitive dependency

    (a) Decomposing the SALES relation

  • 7/31/2019 Logik Desain Dan Relasional

    47/47

    Figure 5.25(b) Relations in 3NF

    Now, there are no transitive dependencies

    Both relations are in 3rd NF

    CustID Name

    CustID Salesperson

    Salesperson Region


Recommended