CSE301 Lec4
CSE301 Lec4
1 2
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
that maintain integrity of manipulated data NOTE: all relations are in 1st Normal form
3 4
REMOVING MULTIVALUED ATTRIBUTES EXPRESSING A SCHEMA
A graphical representation
5 6
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.
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
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
(b)
(b) CUSTOMER relation with address detail
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
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)
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
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
33 34
Many-to-Many–Two relations:
37 38
Figure 4-19 Mapping a ternary relationship Figure 4-19 Mapping a ternary relationship (cont.)
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
49 50
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
53 54
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
61