DBMS Chapter 5
DBMS Chapter 5
• Definition of terms
• List five properties of relations
• State two properties of candidate keys
• Define first, second, and third normal form Key Fields
• Describe problems frommerging relations
• Transform E-R and EER diagrams to relations
• Create tables with entity and relational integrity • Keysare special fields that serve two main purposes:
constraints – Primarykeysare unique identifiers of the relation in question.
Examples include employee numbers, social security numbers, etc.
• Use normalization to convert anomalous tables to Thisis how we can guaranteethat all rows are unique
well-structured relations – Foreign keysare identifiers that enable a dependent relation (on the
many side of a relationship) to refer to itsparent relation (on the one
side of the relationship)
Chapter 5 2
• Keyscan besimple(a single field) or composite(more than Integrity Constraints
one field)
• Keysusually are used as indexes to speed up the response to
user queries (More on this in Ch. 6) • 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)
Chapter 5 5
– For example: Delete Rules
• Restrict–don’t allow delete of “parent” side if related rows exist in
Relation “dependent” side
• Cascade–automatically delete“dependent” side rows that correspond with
• Definition: A relation is anamed, two-dimensional table of data the “parent” side row to be deleted
• Set-to-Null–set the foreign key in the dependent side to null if deleting
• Table consists of rows (records) and columns (attribute or field) fromthe parent side not allowed for weak entities
• Requirements for a table to qualify as a relation:
– It must have a uniquename
– Every attribute value must be atomic (not multivalued, not composite) Chapter 5 9
– Every row must be unique (can’t have two rows with exactly the same Figure 5-3 Schema for four relations (Pine Valley Furniture Company)
valuesfor 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
Primary Key
NOTE: all relationsare in 1st Normal form Foreign Key
(implements 1:N relationship
between customer and order)
Chapter 5 6
Referential
integrity
Integrity Constraints constraints are
drawn via arrows
from dependent to
parent table
• Domain Constraints
– Allowable values for an attribute. See Table 5-1
• Entity Integrity
Chapter 5
– No primary key attribute may be null. All primary 10
Chapter 5 11
Chapter 5 8
Transforming EER Diagrams into
Relations
Chapter 5 19
Chapter 5 16
Note the mandatory one
(a) CUSTOMER
entity type with
composite
attribute
Chapter 5 17
(b) CUSTOMER relation with address detail Figure 5-13 Example of mapping an M:N relationship
a) Completes relationship (M:N)
Chapter 5 18
Figure 5-13 Example of mapping an M:N relationship (cont.)
b) Three resulting relations
Chapter 5 25
Chapter 5 29
Figure 5-15 Example of mapping an associative entity
a) An associative entity
Often in 1:1 relationships, one direction is optional.
Chapter 5 23
Composite primary key for med from the two foreign keys
Chapter 5 27
(b) EMPLOYEE
relation with
recursive foreign
key
Chapter 5 31
Chapter 5 28
Figure 5-18 Mapping a unary M:N relationship Figure 5-20 Supertype/subtype relationships
Anomalies in this Table
(a ) Bill-of-materials
relationships (M:N) • 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
First
Well-Structured Normal Form
Relations
• Modification–giving a salary increase to employee 100
• No multivalued
• A relation that contains minimal dataredundaattributes
ncy and
forces us to update multiple records allows users to insert, delete, and update rows without
(b) ITEM and
Every attribute value is atomic
causingdata inc•onsistencies
Why do these anomalies exist?
CO MPONENT
relations Because there are two themes (entity types) in this
5-25 is not in 1st Normal Form
Fig.lies
• Goal is to avoid•anoma
– Insertion Anomaly–addingnew rows forces user to create
one relation. This results in data duplication and an duplicate data (multivalued attributes) it is not a
unnecessary dependency between the entities relation
– Deletion Anomaly–deletingrows may cause a loss of data that
would be needed for other future rows
Chapter 5 Chapter 5 Chapter 5
stforceschangesto
• Fig. 5-26 isin 1 Normal form
– ModificationAnomaly–changingdata in a row
32 37 42 other rows because of duplication
• All relationsare in 1st Normal Form
General rule of thumb: A table should not pertain to
more than one entity type
Chapter 5 Chapter 5 40 45
Chapter 5 Chapter 5 41 46
Chapter 5 Chapter 5 39 44
Chapter 5 36
Figure 5-27 Functional dependency diagram for INVOICE
Getting it into
Second Normal
Form
Enterprise Keys
Partial dependencies are removed, but there
are still transitive dependencies
• Primary keys that are unique in the whole
Chapter 5 51
database, not just within a single relation
Note: this is relation, but not a well-stru ctured one • Corresponds with the concept of an object ID
in object-oriented systems
Chapter 5 47
Chapter 5 56
Getting it into
Third Normal
Form
Chapter 5 53
Chapter 5 49