0% found this document useful (0 votes)
9 views16 pages

CSE301 Lec4

This document outlines the objectives and components of logical database design and the relational model, including definitions of key terms, properties of relations, and normalization processes. It details the structure of relations, integrity constraints, and the transformation of E-R diagrams into relations, emphasizing the importance of well-structured relations to avoid data anomalies. Additionally, it covers functional dependencies, candidate keys, and the criteria for achieving first, second, and third normal forms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

CSE301 Lec4

This document outlines the objectives and components of logical database design and the relational model, including definitions of key terms, properties of relations, and normalization processes. It details the structure of relations, integrity constraints, and the transformation of E-R diagrams into relations, emphasizing the importance of well-structured relations to avoid data anomalies. Additionally, it covers functional dependencies, candidate keys, and the criteria for achieving first, second, and third normal forms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

OBJECTIVES

CHAPTER 4:  Define terms


LOGICAL DATABASE DESIGN AND THE  List five properties of relations
RELATIONAL MODEL  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

1 2

COMPONENTS OF RELATIONAL MODEL RELATION


 A relation is a named, two-dimensional table of data.
 Data structure  A table consists of rows (records or tuples) and columns
(attributes or fields).
 Tables (relations), rows, columns
 Requirements for a table to qualify as a relation:
 Data manipulation  It must have a unique name.
Every attribute value must be atomic (not multivalued, not composite).
 PowerfulSQL operations for retrieving and 

 Every row must be unique (can’t have two rows with exactly the same
modifying data values for all their fields).
Attributes (columns) in tables must have unique names.
 Data integrity 

 The order of the columns must be irrelevant.


 Mechanisms for implementing business rules  The order of the rows must be irrelevant.

that maintain integrity of manipulated data NOTE: all relations are in 1st Normal form
3 4
REMOVING MULTIVALUED ATTRIBUTES EXPRESSING A SCHEMA

Short text statements


EMPLOYEE1(EmpID, Name, DeptName, Salary)

A graphical representation

5 6

CORRESPONDENCE WITH E-R MODEL KEY FIELDS


 Keys are special fields that serve two main purposes:
 Relations (tables) correspond with entity types and  Primary keys. Unique identifiers of the relation. Examples
with many-to-many relationship types. include employee numbers, social security numbers, etc.
This guarantees that all rows are unique.
 Rows correspond with entity instances and with
many-to-many relationship instances.  Foreign keys .Identifiers that enable a dependent relation
(on the many side of a relationship) to refer to its parent
 Columns correspond with attributes. relation (on the one side of the relationship).

 NOTE: The word relation (in relational database) is  Keys can be simple (a single field) or composite (more
NOT the same as the word relationship (in E-R than one field).
model).  Keys usually are used as indexes to speed up the
response to user queries (more on this in Chapter 5).

7 8
Figure 4-3 Schema for four relations (Pine Valley Furniture Company)
INTEGRITY CONSTRAINTS
 Domain Constraints
 Allowable values for an attribute. See Table 4-1
Primary Key  Entity Integrity
Foreign Key  Entity Integrity Rule: No primary key attribute may be null. All
(implements 1:N relationship
between customer and order) primary key fields MUST have data
 Null: A value that may be assigned to an attribute when no
Combined, these are a composite
primary key (uniquely identifies the other value applies or when the applicable value is unknown.
 Referential Integrity
order line)…individually they are
foreign keys (implement M:N
relationship between order and product)  referential integrity constraint: A rule that states that either
each foreign key value must match a primary key value in
another relation or the foreign key value must be null.

9 10

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
Domain definitions enforce domain integrity constraints entities
11 12
Figure 4-5
Referential integrity constraints (Pine Valley Furniture) CREATING RELATIONAL TABLE
CREATE TABLE
statements of the SQL data definition language.

Referential NOT NULL,


integrity each attribute can be constrained from being assigned a
constraints are null value.
drawn via arrows
from dependent to PRIMARY KEY
parent table clause at the end of each table definition.

FOREIGN KEY REFERENCE:


statement corresponds to each of these arrows
13 14

Figure 4-6 SQL table definitions

WELL-STRUCTURED RELATIONS
Well-structured relation: A relation that contains minimal
redundancy and allows users to insert, modify, and delete
Referential
the rows in a table without errors or inconsistencies.
integrity
constraints are
Anomaly: An error or inconsistency that may result when a
implemented with
user attempts to update a table that contains redundant
foreign key to
data. The three types of anomalies are insertion, deletion,
primary key
and modification anomalies.
references
1. Insertion anomaly
2. Deletion anomaly
3. Modification anomaly
15 16
Figure 4-8 Mapping a regular entity
TRANSFORMING EER DIAGRAMS INTO
RELATIONS (a) CUSTOMER
entity type with
Mapping Regular Entities to Relations simple
attributes
 Simple attributes: E-R attributes map
directly onto the relation
 Composite attributes: Use only their
simple, component attributes (b) CUSTOMER relation

 Multivalued Attribute: Becomes a


separate relation with a foreign key
taken from the superior entity
17 18

Figure 4-9 Mapping a composite attribute Figure 4-10 Mapping an entity with a multivalued attribute

(a) CUSTOMER
entity type with (a)
composite
attribute

Multivalued attribute becomes a separate relation with foreign key

(b)
(b) CUSTOMER relation with address detail

One-to-many relationship between original entity and new relation

19 20
Figure 4-11 Example of mapping a weak entity
TRANSFORMING EER DIAGRAMS INTO
a) Weak entity DEPENDENT
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)
21 22

Figure 4-11 Example of mapping a weak entity (cont.)


TRANSFORMING EER DIAGRAMS INTO
b) Relations resulting from weak entity
RELATIONS (CONT.)
NOTE: the domain constraint
for the foreign key should
Mapping Binary Relationships
NOT allow null value if
 One-to-Many–Primary key on the one side
DEPENDENT is a weak
entity becomes a foreign key on the many side
Foreign key
 Many-to-Many–Create a new relation with the
primary keys of the two entities as its primary
Composite primary key key
Surrogate Primary Key: A serial number or other system assigned primary key  One-to-One–Primary key on mandatory side
for a relation.
becomes a foreign key on optional side
DEPENDENT(DependentID, EmployeeID, FirstName, MiddleInitial, LastName,
DateOfBirth, Gender) 23 24
Figure 4-12 Example of mapping a 1:M relationship Figure 4-13 Example of mapping an M:N relationship
a) Relationship between customers and orders a) Completes relationship (M:N)

Note the mandatory one

b) Mapping the relationship

Again, no null value in the


foreign key…this is because
of the mandatory minimum The Completes relationship will need to become a separate relation
cardinality
Foreign key

25 26

Figure 4-13 Example of mapping an M:N relationship (cont.) Figure 4-14 Example of mapping a binary 1:1 relationship
b) Three resulting relations a) In charge relationship (1:1)

Composite primary key

Foreign key
new
Foreign key
intersection
relation
Often in 1:1 relationships, one direction is optional

27 28
Figure 4-14 Example of mapping a binary 1:1 relationship (cont.)
b) Resulting relations
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
Foreign key goes in the relation on the optional side,
It
is natural and familiar to end-users
matching the primary key on the mandatory side
Default identifier may not be unique

29 30

Figure 4-15 Example of mapping an associative entity Figure 4-15 Example of mapping an associative entity (cont.)
a) An associative entity b) Three resulting relations

Composite primary key formed from the two foreign keys

31 32
Figure 4-16 Example of mapping an associative entity with Figure 4-16 Example of mapping an associative entity with
an identifier an identifier (cont.)
a) SHIPMENT associative entity b) Three resulting relations

Primary key differs from foreign keys

33 34

Figure 4-17 Mapping a unary 1:N relationship


TRANSFORMING EER DIAGRAMS INTO
RELATIONS (CONT.)
Mapping Unary Relationships
 One-to-Many–Recursive foreign key in the (a) EMPLOYEE
entity with unary
same relation relationship

 Many-to-Many–Two relations:

One for the entity type (b)


EMPLOYEE
One for an associative relation in which relation with
recursive
the primary key has two attributes, both foreign key
taken from the primary key of the entity
35 36
Figure 4-18 Mapping a unary M:N relationship
TRANSFORMING EER DIAGRAMS INTO
(a) Bill-of-materials
relationships (M:N) RELATIONS (CONT.)
Mapping Ternary (and n-ary)
Relationships
One relation for each entity and one for
the associative entity
(b) ITEM and
COMPONENT Associative entity has foreign keys to
relations
each entity in the relationship

37 38

Figure 4-19 Mapping a ternary relationship Figure 4-19 Mapping a ternary relationship (cont.)

a) PATIENT TREATMENT Ternary relationship with


associative entity b) Mapping the ternary relationship PATIENT TREATMENT

Remember This is why But this makes a It would be


that the treatment date very better to create a
primary key and time are cumbersome surrogate key
MUST be included in the key… like Treatment#
unique composite
primary key

39 40
Figure 4-20 Supertype/subtype relationships
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
41 42

Figure 4-21
Mapping supertype/subtype relationships to relations DATA NORMALIZATION
 Primarilya tool to validate and improve
a logical design so that it satisfies
certain constraints that avoid
unnecessary duplication of data
 Theprocess of decomposing relations
with anomalies to produce smaller,
These are implemented as one-to-one
relationships
well-structured relations

43 44
EXAMPLE–FIGURE 4-2B
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 Question–Is this a relation? Answer–Yes: Unique rows and no
changes to other rows because of duplication multivalued attributes

Question–What’s the primary key? Answer–Composite: EmpID, CourseTitle


General rule of thumb: A table should not pertain to
more than one entity type
45 46

Figure 4.22 Steps in normalization


ANOMALIES IN THIS TABLE
 Insertion–can’t enter a new employee without having
the employee take a class (or at least empty fields of
class information)
 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 3rd normal form is
forces us to update multiple records generally considered to be
sufficient, although higher
Why do these anomalies exist?
degrees of normalization
Because there are two themes (entity types) in this are possible.
one relation. This results in data duplication and an
unnecessary dependency between the entities.
47 48
FUNCTIONAL DEPENDENCIES AND KEYS FUNCTIONAL DEPENDENCIES AND KEYS
 Functional Dependency: The value of one  Candidate Key: An attribute, or combination of
attribute (the determinant) determines the attributes, that uniquely identifies a row in a relation.
value of another attribute
 Determinants: the attribute on the left side of A unique identifier. One of the candidate keys
the arrow in a functional dependency will become the primary key
 E.g.perhaps there is both credit card number and
 Examples: SS# in a table…in this case both are candidate
 EmpID,CourseTitle → DateCompleted keys
 Each non-key field is functionally dependent
 SSN → Name, Address, Birthdate
on every candidate key
 VIN → Make, Model, Color
 ISBN → Title, FirstAuthorName, Publisher

49 50

FUNCTIONAL DEPENDENCIES AND KEYS FIRST NORMAL FORM

 No multivalued attributes
 Every attribute value is atomic
 Fig. 4-25 is not in 1st Normal Form (multivalued
attributes)  it is not a relation
 Fig. 4-26 is in 1st Normal form
 All relations are in 1st Normal Form

51 52
Table with no multivalued attributes and unique rows, in 1st
Table with multivalued attributes, not in 1st normal form
normal form

Note: this is NOT a relation


Note: this is a relation, but not a well-structured one

53 54

ANOMALIES IN THIS TABLE SECOND NORMAL FORM


 Insertion–if new product is ordered for order 1007 of
existing customer, customer data must be re-entered,  1NFPLUS every non-key attribute is fully
causing duplication
 Deletion–if we delete the Dining Table from Order functionally dependent on the ENTIRE
1006, we lose information concerning this item's finish primary key
and price
 Every non-key attribute must be defined by
 Update–changing the price of product ID 4 requires
update in multiple records the entire key, not by only part of the key
 No partial functional dependencies
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.
55 56
Figure 4-27 Functional dependency diagram for INVOICE Figure 4-28 Removing partial dependencies

Getting it into
OrderID  OrderDate, CustomerID, CustomerName, CustomerAddress Second Normal Form
CustomerID  CustomerName, CustomerAddress
ProductID  ProductDescription, ProductFinish, ProductStandardPrice
OrderID, ProductID  OrderQuantity Partial dependencies are removed, but there
are still transitive dependencies
Therefore, NOT in 2nd Normal Form
58
57

THIRD NORMAL FORM Figure 4-29 Removing partial dependencies

 2NF PLUS no transitive dependencies


(functional dependencies on non-primary-key Getting it into
attributes) Third Normal
Form
 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 Transitive dependencies are removed
determinant becomes primary key in the new
table and stays as foreign key in the old table
59 60
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

61

You might also like