Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5: Logical Database Design and the Relational Model

Similar presentations


Presentation on theme: "Chapter 5: Logical Database Design and the Relational Model"— Presentation transcript:

1 Chapter 5: Logical Database Design and the Relational Model
Modern Database Management 8th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden By: Aatif Kamal Dated: March 2008

2 Objectives Definition of terms List five properties of relations
State two properties of candidate keys Define first, second, and third normal form Describe problems from merging relations Transform E-R and EER diagrams to relations Create tables with entity and relational integrity constraints Use normalization to convert anomalous tables to well-structured relations

3 Relation is NOT same as Relationship
4/20/2017 Relation is NOT same as Relationship INFORMAL DEFINITIONS RELATION: A table of values A relation may be thought of as a set of rows. A relation may alternately be though of as a set of columns. Each row represents a fact that corresponds to a real-world entity or relationship. Each row has a value of an item or set of items that uniquely identifies that row in the table. Sometimes row-ids or sequential numbers are assigned to identify the rows in the table. Each column typically is called by its column name or column header or attribute name.

4 FORMAL DEFINITIONS A Relation may be defined in multiple ways.
4/20/2017 FORMAL DEFINITIONS A Relation may be defined in multiple ways. The Schema of a Relation: R (A1, A2, .....An) Relation schema R is defined over attributes A1, A2, .....An For Example – CUSTOMER (Cust-id, Cust-name, Address, Phone#) Here, CUSTOMER is a relation defined over the four attributes Cust-id, Cust-name, Address, Phone#, each of which has a domain or a set of valid values. For example, the domain of Cust-id is 6 digit numbers.

5 FORMAL DEFINITIONS A tuple is an ordered set of values
4/20/2017 FORMAL DEFINITIONS A tuple is an ordered set of values Each value is derived from an appropriate domain. Each row in the CUSTOMER table may be referred to as a tuple in the table and would consist of four values. <632895, "John Smith", "101 Main St. Atlanta, GA ", "(404) "> is a tuple belonging to the CUSTOMER relation. A relation may be regarded as a set of tuples (rows). Columns in a table are also called attributes of the relation.

6 NOTE: all relations are in 1st Normal form
Definition: A relation is a named, two-dimensional table of data Table consists of rows (records) and columns (attribute or field) Requirements for a table to qualify as a relation: It must have a unique name Every attribute value must be atomic (not multi-valued, not composite) Every row/tuple must be unique (can’t have two rows with exactly the same values for all their fields) Attributes (columns) in tables must have unique names The order of the columns must be irrelevant The order of the rows must be irrelevant NOTE: all relations are in 1st Normal form

7 Correspondence with E-R Model
Relations (tables) correspond with entity types and with many-to-many relationship types Rows correspond with entity instances and with many- to-many relationship instances Columns correspond with attributes NOTE: The word relation (in relational database) is NOT the same as the word relationship (in E-R model)

8 DEFINITION SUMMARY Informal Terms Formal Terms Table Relation Column
4/20/2017 Informal Terms Formal Terms Table Relation Column Attribute/Domain Row Tuple Values in a column Domain Table Definition Schema of a Relation Populated Table Extension

9 4/20/2017 Relation Example

10 Key Fields Keys are special fields that serve two main purposes:
Primary keys are unique identifiers of the relation in question. Examples include employee numbers, social security numbers, etc. This is how we can guarantee that all rows are unique Foreign keys are identifiers that enable a dependent relation (on the many side of a relationship) to refer to its parent relation (on the one side of the relationship) Keys can be simple (a single field) or composite (more than one field) Keys usually are used as indexes to speed up the response to user queries

11 Foreign Key (implements 1:N relationship between customer and order)
Figure 5-3 Schema for four relations (Pine Valley Furniture Company) 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 and product)

12 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 Action Assertions Business rules.

13 Domain definitions enforce domain integrity constraints

14 Integrity Constraints
Referential Integrity – rule 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 Restrict: don’t allow delete of “parent” side if related rows exist in “dependent” side Cascade–automatically: delete “dependent” side rows that correspond with the “parent” side row to be deleted Set-to-Null: set the foreign key in the dependent side to null if deleting from the parent side  not allowed for weak entities

15 Figure 5-5 Referential integrity constraints (Pine Valley Furniture) Referential integrity constraints are drawn via arrows from dependent to parent table

16 Figure 5-6 SQL table definitions
Referential integrity constraints are implemented with foreign key to primary key references

17 Transforming EER Diagrams into Relations
Mapping Regular Entities to Relations Simple attributes: E-R attributes map directly onto the relation Composite attributes: Use only their simple, component attributes Multivalued Attribute: Becomes a separate relation with a foreign key taken from the superior entity

18 Figure 5-8 Mapping a regular entity
(a) CUSTOMER entity type with simple attributes (b) CUSTOMER relation

19 Figure 5-9 Mapping a composite attribute
(a) CUSTOMER entity type with composite attribute (b) CUSTOMER relation with address detail

20 Figure 5-10 Mapping an entity with a multivalued attribute
Multivalued attribute becomes a separate relation with foreign key (b) One–to–many relationship between original entity and new relation

21 Transforming EER Diagrams into Relations (cont….)
Mapping 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)

22 Figure 5-11 Example of mapping a weak entity
a) Weak entity DEPENDENT

23 Figure 5-11 Example of mapping a weak entity (cont.)
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

24 Transforming EER Diagrams into Relations (cont.)
Mapping Binary Relationships One-to-Many: Primary key on the one side becomes a foreign key on the many side Many-to-Many: Create a new 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

25 Figure 5-12 Example of mapping a 1:M relationship
a) Relationship between customers and orders Note the mandatory one b) Mapping the relationship Again, no null value in the foreign key…this is because of the mandatory minimum cardinality Foreign key

26 Figure 5-13 Example of mapping an M:N relationship
a) Completes relationship (M:N) The Completes relationship will need to become a separate relation

27 Figure 5-13 Example of mapping an M:N relationship (cont.)
b) Three resulting relations Composite primary key Foreign key New intersection relation

28 Figure 5-14 Example of mapping a binary 1:1 relationship
a) In_charge relationship (1:1) Often in 1:1 relationships, one direction is optional.

29 Figure 5-14 Example of mapping a binary 1:1 relationship (cont.)
b) Resulting relations Foreign key goes in the relation on the optional side, Matching the primary key on the mandatory side

30 Transforming EER Diagrams into Relations (cont.)
Mapping Associative Entities Identifier Not Assigned Default primary key for the association relation is composed of the primary keys of the two entities (as in M:N relationship) Identifier Assigned It is natural and familiar to end-users Default identifier may not be unique

31 Figure 5-15 Example of mapping an associative entity
a) An associative entity

32 Figure 5-15 Example of mapping an associative entity (cont.)
b) Three resulting relations Composite primary key formed from the two foreign keys

33 Figure 5-16 Example of mapping an associative entity with
an identifier a) SHIPMENT associative entity

34 Figure 5-16 Example of mapping an associative entity with
an identifier (cont.) b) Three resulting relations Primary key differs from foreign keys

35 Transforming EER Diagrams into Relations (cont.)
Mapping 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 the primary key has two attributes, both taken from the primary key of the entity

36 Figure 5-17 Mapping a unary 1:N relationship
(a) EMPLOYEE entity with unary relationship (b) EMPLOYEE relation with recursive foreign key

37 Figure 5-18 Mapping a unary M:N relationship
(a) Bill-of-materials relationships (M:N) (b) ITEM and COMPONENT relations

38 Transforming EER Diagrams into Relations (cont…)
Mapping Ternary (and n-ary) Relationships One relation for each entity and one for the associative entity Associative entity has foreign keys to each entity in the relationship

39 Figure 5-19 Mapping a ternary relationship
a) PATIENT TREATMENT Ternary relationship with associative entity

40 Figure 5-19 Mapping a ternary relationship (cont.)
b) Mapping the ternary relationship PATIENT TREATMENT Remember that the primary key MUST be unique It would be better to create a surrogate key like Treatment# This is why treatment date and time are included in the composite primary key But this makes a very cumbersome key…

41 Transforming EER Diagrams into Relations (cont.)
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 primary key of subtype relation 1:1 relationship established between supertype and each subtype, with supertype as primary table

42 Mapping EER Model Constructs to Relations
4/20/2017 Mapping EER Model Constructs to Relations Options for Mapping Specialization or Generalization. Convert each specialization with m subclasses {S1, S2,….,Sm} and generalized superclass C, where the attributes of C are {k,a1,…an} and k is the (primary) key, into relational schemas using one of the four following options: Option A: Multiple relations-Superclass and subclasses. Create a relation L for C with attributes Attrs(L) = {k,a1,…an} and PK(L) = k. Create a relation Li for each subclass Si, 1 < i < m, with the attributesAttrs(Li) = {k} U {attributes of Si} and PK(Li)=k. This option works for any specialization (total or partial, disjoint or over-lapping). Option B: Multiple relations-Subclass relations only Create a relation Li for each subclass Si, 1 < i < m, with the attributes Attr(Li) = {attributes of Si} U {k,a1…,an} and PK(Li) = k. This option only works for a specialization whose subclasses are total (every entity in the superclass must belong to (at least) one of the subclasses).

43 4/20/2017 EER diagram notation for an attribute-defined specialization on JobType.

44 4/20/2017 Generalization. (b) Generalizing CAR and TRUCK into the superclass VEHICLE.

45 Mapping EER Model Constructs to Relations (cont)
4/20/2017 Mapping EER Model Constructs to Relations (cont) Option C: Single relation with one type attribute. Create a single relation L with attributes Attrs(L) = {k,a1,…an} U {attributes of S1} U…U {attributes of Sm} U {t} and PK(L) = k. The attribute t is called a type (or discriminating) attribute that indicates the subclass to which each tuple belongs: Works for Disjoint Option D: Single relation with multiple type attributes. Create a single relation schema L with attributes Attrs(L) = {k,a1,…an} U {attributes of S1} U…U {attributes of Sm} U {t1, t2,…,tm} and PK(L) = k. Each ti, 1 < I < m, is a Boolean type attribute indicating whether a tuple belongs to the subclass Si. Works for overlapping as well

46 4/20/2017 EER diagram notation for an attribute-defined specialization on JobType.

47 EER diagram notation for an overlapping (nondisjoint) specialization.
4/20/2017 EER diagram notation for an overlapping (nondisjoint) specialization.

48 Mapping EER Model Constructs to Relations (cont)
4/20/2017 Mapping EER Model Constructs to Relations (cont) Mapping of Shared Subclasses (Multiple Inheritance) A shared subclass, such as STUDENT_ASSISTANT, is a subclass of several classes, indicating multiple inheritance. These classes must all have the same key attribute; otherwise, the shared subclass would be modeled as a category. We can apply any of the options discussed in for EER-inhertance to a shared subclass, subject to the restriction discussed mapping algorithm. Below both options C and D are used for the shared class STUDENT_ASSISTANT.

49 4/20/2017 A specialization lattice with multiple inheritance for a UNIVERSITY database.

50 4/20/2017 Mapping the EER specialization lattice in Figure 4.6 using multiple options.

51 Mapping EER Model Constructs to Relations (cont)
4/20/2017 Mapping EER Model Constructs to Relations (cont) Mapping of Union Types (Categories). For mapping a category whose defining superclass have different keys, it is customary to specify a new key attribute, called a surrogate key, when creating a relation to correspond to the category. In the example below we can create a relation OWNER to correspond to the OWNER category and include any attributes of the category in this relation. The primary key of the OWNER relation is the surrogate key, which we called OwnerId.

52 Two categories (union types): OWNER and REGISTERED_VEHICLE.
4/20/2017 Two categories (union types): OWNER and REGISTERED_VEHICLE.

53 Mapping the EER categories (union types) in to relations.
4/20/2017 Mapping the EER categories (union types) in to relations.

54 Mapping Exercise Exercise 4/20/2017
FIGURE An ER schema for a SHIP_TRACKING database.

55 Chapter Summary ER-to-Relational Mapping Algorithm
4/20/2017 Chapter Summary ER-to-Relational Mapping Algorithm Step 1: Mapping of Regular Entity Types Step 2: Mapping of Weak Entity Types Step 3: Mapping of Binary 1:1 Relation Types Step 4: Mapping of Binary 1:N Relationship Types. Step 5: Mapping of Binary M:N Relationship Types. Step 6: Mapping of Multivalued attributes. Step 7: Mapping of N-ary Relationship Types. Mapping EER Model Constructs to Relations Step 8: Options for Mapping Specialization or Generalization. Step 9: Mapping of Union Types (Categories).

56 Data Normalization Primarily a tool to validate and improve a logical design so that it satisfies certain constraints that avoid unnecessary duplication of data The process of decomposing relations with anomalies to produce smaller, well-structured relations

57 Well-Structured Relations
A relation that contains minimal data redundancy and allows users to insert, delete, and update rows without causing data inconsistencies Goal is to avoid anomalies Insertion Anomaly: adding new rows forces user to create duplicate data Deletion Anomaly: deleting rows may cause a loss of data that would be needed for other future rows Modification Anomaly: changing data in a row forces changes to other rows because of duplication General rule of thumb: A table should not pertain to more than one entity type

58 Example–Figure 5-2b Question–Is this a relation?
Answer–Yes: Unique rows and no multivalued attributes Question–What’s the primary key? Answer–Composite: Emp_ID, Course_Title

59 Anomalies in this Table
Insertion–can’t enter a new employee without having the employee take a class Deletion–if we remove employee 140, we lose information about the existence of a Tax Acc class Modification–giving a salary increase to employee 100 forces us to update multiple records Why do these anomalies exist? Because there are two themes (entity types) in this one relation. This results in data duplication and an unnecessary dependency between the entities

60 Functional Dependencies and Keys
Functional Dependency: The value of one attribute (the determinant) determines the value of another attribute Candidate Key: A unique identifier. One of the candidate keys will become the primary key E.g. perhaps there is both credit card number and SS# in a table…in this case both are candidate keys Each non-key field is functionally dependent on every candidate key

61 Figure 5.22 Steps in normalization

62 No multivalued attributes Every attribute value is atomic
First Normal Form No multivalued attributes Every attribute value is atomic Fig is not in 1st Normal Form (multivalued attributes)  it is not a relation Fig is in 1st Normal form All relations are in 1st Normal Form

63 Table with multivalued attributes, not in 1st normal form
Note: this is NOT a relation

64 Table with no multivalued attributes and unique rows, in 1st normal form
Note: this is relation, but not a well-structured one

65 Anomalies in this Table
Insertion–if new product is ordered for order 1007 of existing customer, customer data must be re-entered, causing duplication Deletion–if we delete the Dining Table from Order 1006, we lose information concerning this item's finish and price Update–changing the price of product ID 4 requires update in several records 1NF Anomalies Why do these anomalies exist? Because there are multiple themes (entity types) in one relation. This results in duplication and an unnecessary dependency between the entities

66 Second Normal Form 1NF PLUS every non-key attribute is fully functionally dependent on the ENTIRE primary key Every non-key attribute must be defined by the entire key, not by only part of the key No partial functional dependencies

67 Therefore, NOT in 2nd Normal Form
Figure 5-27 Functional dependency diagram for INVOICE Order_ID  Order_Date, Customer_ID, Customer_Name, Customer_Address Customer_ID  Customer_Name, Customer_Address Product_ID  Product_Description, Product_Finish, Unit_Price Order_ID, Product_ID  Order_Quantity Therefore, NOT in 2nd Normal Form

68 Figure 5-28 Removing partial dependencies
Getting it into Second Normal Form Partial dependencies are removed, but there are still transitive dependencies

69 Third Normal Form 2NF PLUS no transitive dependencies (functional dependencies on non-primary-key attributes) Note: This is called transitive, because the primary key is a determinant for another attribute, which in turn is a determinant for a third Solution: Non-key determinant with transitive dependencies go into a new table; non-key determinant becomes primary key in the new table and stays as foreign key in the old table

70 Transitive dependencies are removed
Figure 5-28 Removing partial dependencies Getting it into Third Normal Form Transitive dependencies are removed

71 Merging Relations View Integration–Combining entities from multiple ER models into common relations Issues to watch out for when merging entities from different ER models: Synonyms–two or more attributes with different names but same meaning Homonyms–attributes with same name but different meanings Transitive dependencies –even if relations are in 3NF prior to merging, they may not be after merging Supertype/subtype relationships –may be hidden prior to merging

72 Normalization – 1 to 3 NF revisited
Just another way of explaining normalizations rules

73 First Normal Form Disallows
composite attributes multivalued attributes nested relations; attributes whose values for an individual tuple are non-atomic Considered to be part of the definition of relation

74 First Normal Form Above two are examples of Multi-valued, non-atomic attributes So for one worst case you might require 1000 columns in your table…. Still not dynamic enough as what if 1001 person is hired? Means we will forever be changing the structure of the database table

75 First normal form “A relation is in first normal form if every attribute is Single valued for each tuple (record)” Each attribute in each row, or each “cell” of the table, contains only one value. No repeating fields or groups are allowed. The first rule dictates that we must not duplicate data within the same row of a table. Within the database community, this concept is referred to as the atomicity of a table. Tables that comply with this rule are said to be atomic.

76 First normal form Stu_i d Stu_Nam e Majo r1 Major 2 Credit s Status
Smith Math 90 Senior S1002 Lee CS, Math 15 Fresh S1003 Jon Art, English 63 Junior Stu_i d Stu_Nam e Majo r1 Major 2 Credit s Status S1001 Smith Math 90 Senior S1002 Lee CS 15 Fresh S1003 Jon Art Englis h 63 Junior Students table (students have more then one major). The rule of 1NF (every attribute is Single valued for each tuple) is violated in the records of S1002 and S1003, who have two values listed for Major.

77 Normalization into 1NF

78 Normalization of nested relations into 1NF

79 Second normal form A relation is in second normal form (2NF) if and only if It is in first normal form All the non-key attributes are fully functionally dependent on the key. If the relation is 1NF and the key consist of single attribute, then the relation is automatically in 2NF. nonkey attributes = attributes that don’t appear in any candidate key

80 3.3 Second Normal Form (1) Uses the concepts of FDs, primary key
Definitions Prime attribute: An attribute that is member of the primary key K Full functional dependency: a FD Y  Z where removal of any attribute from Y means the FD does not hold any more Examples: {SSN, PNUMBER}  HOURS is a full FD since neither SSN  HOURS nor PNUMBER  HOURS hold {SSN, PNUMBER}  ENAME is not a full FD (it is called a partial dependency ) since SSN  ENAME also holds

81 Second Normal Form (2) A relation schema R is in second normal form (2NF) if every non-prime attribute A in R is fully functionally dependent on the primary key R can be decomposed into 2NF relations via the process of 2NF normalization If the relation is 1NF and the key consist of single attribute, then the relation is automatically in 2NF. nonkey attributes = attributes that don’t appear in any candidate key

82 Normalizing into 2NF and 3NF

83 3.4 Third Normal Form (1) Definition: Examples:
Transitive functional dependency: a FD X  Z that can be derived from two FDs X  Y and Y  Z Examples: SSN  DMGRSSN is a transitive FD Since SSN  DNUMBER and DNUMBER  DMGRSSN hold SSN  ENAME is non-transitive Since there is no set of attributes X where SSN  X and X  ENAME

84 Third Normal Form A relation is in third normal form if It is in 2NF
No non-key attribute is transitively dependent on the key

85 Third Normal Form Transitive dependency (A  B  C)
Stu_id Stu_Name Credits Status S1001 Smith 90 Senior S1002 Lee 15 Fresh S1003 Jon 63 Junior Transitive dependency (A  B  C) Stu_id  Credits  Status If Stu_id  Credits and Credits  Status then we can write Stu_id  Status

86 Third Normal Form Stu_id Stu_Na me Credit s S1001 Smith 90 S1002 Lee
Student Status Stu_id Stu_Na me Credit s S1001 Smith 90 S1002 Lee 15 S1003 Jon 63 Credits Status 15 Fresh 63 Junior 90 Senior Transitive dependency is removed

87 Third Normal Form (2) A relation schema R is in third normal form (3NF) if it is in 2NF and no non-prime attribute A in R is transitively dependent on the primary key R can be decomposed into 3NF relations via the process of 3NF normalization NOTE: In X  Y and Y  Z, with X as the primary key, we consider this a problem only if Y is not a candidate key. When Y is a candidate key, there is no problem with the transitive dependency . E.g., Consider EMP (SSN, Emp#, Salary ). Here, SSN  Emp#  Salary and Emp# is a candidate key.

88 Normal Forms Defined Informally
1st normal form All attributes depend on the key -atomicity 2nd normal form All attributes depend on the whole key - 3rd normal form All attributes depend on nothing but the key – no transitivity

89 Successive Normalization of LOTS into 2NF and 3NF

90 SUMMARY OF NORMAL FORMS based on Primary Keys

91 Enterprise Keys Primary keys that are unique in the whole database, not just within a single relation Corresponds with the concept of an object ID in object-oriented systems

92 Figure 5-31 Enterprise keys
a) Relations with enterprise key b) Sample data with enterprise key


Download ppt "Chapter 5: Logical Database Design and the Relational Model"

Similar presentations


Ads by Google