LEC 04 Logical Database Design and Relational Database
LEC 04 Logical Database Design and Relational Database
Relation
3
A relation is a named, two-dimensional table of data. A table consists of rows (records) and columns (attribute or field).
Relation
4 6
Key Fields
It must have a unique name. Every attribute value must be simple. Every row must be unique. Attributes in tables must have unique names. The order of the columns must be irrelevant. The order of the rows must be irrelevant.
Primary keys are unique identifiers of the rows in the relation. This is how we can guarantee that all rows are unique. Foreign keys are identifiers that enable a dependent relation to refer to its parent relation.
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.
2/16/2012
Integrity Constraints
Constraints or rules limiting acceptable values and actions, whose purpose is to facilitate maintaining the accuracy and integrity of data in the database. Domain Constraints
Allowable values for an attribute. Domain: set of values that may be assigned to an attribute No primary key attribute may be null. All primary key fields MUST have data
Entity Integrity
Integrity Constraints
10
Referential Integrity 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
Cascadeautomatically delete dependent side rows that correspond with the parent side row to be deleted Restrictdont allow delete of parent side if related rows exist in dependent side Set-to-Nullset the foreign key in the dependent side to null if deleting from the parent side is not allowed for weak entities
2/16/2012
Referential integrity constraints are drawn via arrows from dependent to parent table
Referential integrity constraints are implemented with foreign key to primary key references
11
12
Figure 4-8 Mapping a regular entity (a) CUSTOMER entity type with simple attributes
Simple attributes: E-R attributes map directly onto the relation Composite attributes: Use only their simple, component attributes Multi-valued attributes: Becomes a separate relation with a foreign key taken from the superior entity
14
2/16/2012
(b)
15
16
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)
18
2/16/2012
Figure 4-11 Example of mapping a weak entity (cont.) b) Relations resulting from weak entity
20
NOTE: the domain constraint for the foreign key should NOT allow null value if DEPENDENT is a weak entity Foreign key
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
19
Figure 4-12 Example of mapping a 1:M relationship a) Relationship between customers and orders
2/16/2012
Figure 4-13 Example of mapping an M:N relationship (cont.) b) Three resulting relations
Figure 4-14 Example of mapping a binary 1:1 relationship a) In charge relationship (1:1)
23
24
Figure 4-14 Example of mapping a binary 1:1 relationship (cont.) b) Resulting relations
26
Default primary key for the association relation is composed of the primary keys of the two entities (same as M:N relationship) It is natural and familiar to end-users Default identifier may not be unique
Identifier Assigned
Foreign key goes in the relation on the optional side, matching the primary key on the mandatory side
25
2/16/2012
Figure 4-15 Example of mapping an associative entity (cont.) b) Three resulting relations
Figure 4-16 Example of mapping an associative entity with an identifier a) SHIPMENT associative entity
Figure 4-16 Example of mapping an associative entity with an identifier (cont.) b) Three resulting relations
29
30
2/16/2012
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
One relation for each entity and one for the associative entity Associative entity has foreign keys to each entity in the relationship
33
2/16/2012
Figure 4-19 Mapping a ternary relationship a) PATIENT TREATMENT Ternary relationship with associative entity Figure 4-19 Mapping a ternary relationship (cont.) b) Mapping the ternary relationship PATIENT TREATMENT
35
36
Data Normalization
37 38
Well-Structured Relations
Primarily a tool to validate and improve a logical design so that it satisfies certain constraints that avoid unnecessary duplication of data Goal: decompose relations with anomalies to produce smaller, well-structured relations Anomaly: an error or inconsistency that may result when a user attempts to update a table that contains redundant data.
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
2/16/2012
Insertioncant enter a new employee without having the employee take a class Deletionif we remove employee 140, we lose information about the existence of a Tax Acc class Modificationgiving 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
Steps in normalization
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 tablein this case both are candidate keys
10
2/16/2012
No multi-valued attributes Every attribute value is atomic All relations are in 1st Normal Form
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 multiple records
45
Partial functional dependency: exists when a non-key attribute is functionally dependent on part of the primary key Every non-key attribute must be defined by the entire key, not by only part of the key
OrderID: OrderDate, CustomerID, CustomerName, CustomerAddress ProductID: ProductDescription, ProductFinish, ProductStandardPrice OrderID, ProductID: OrderQuantity
11
2/16/2012
Partial dependencies are removed, but there are still transitive dependencies
49
2NF PLUS no transitive dependencies (functional dependency between the primary key and one or more non-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
12