04 / 19 · Models and Integrity
Functional dependencies
Identify the determinants of line, order and catalog facts, then use attribute closure to explain keys and update anomalies.
A report can join order lines and catalog items on demand. Suppose we store that result as the authoritative table. The code of item 7 now appears in three rows. Correcting only one copy from TEA50 to TEA-50 gives the same product two current codes. The line key still distinguishes all rows. It cannot protect the separate fact about the product.
An agreement rule between rows#
Start with the order and catalog example and add the current order state. Write for the flattened relation. The attributes are order ID, line number, product ID, quantity, agreed unit price, current catalog code and order state. All are present in this model.
| Order | Line | Item | Quantity | Price | Code | State |
|---|---|---|---|---|---|---|
| 101 | 1 | 7 | 2 | 350 | TEA50 | open |
| 101 | 2 | 9 | 1 | 600 | COFFEE | open |
| 106 | 1 | 7 | 1 | 350 | TEA50 | paid |
| 106 | 2 | 7 | 1 | 300 | TEA50 | paid |
A functional dependency says that any two tuples that agree on all attributes of agree on all attributes of . It is a rule for every valid state, with and sets of attributes. It is also a test we can apply to one state. We write for and use the same shorthand for other sets. (Armstrong, 1974, §§4, 7 and 10, Silberschatz et al., ch. 7)
The shop gives each line a unique number within its order, so . Every order has one current state, so . The catalog gives each item one current code, so . Each current code names only one item, so . These rules hold across future insertions and corrections if the data remains valid. The prices 350 and 300 for item 7 in order 106 already refute . The two states happen to identify two different orders in this instance. An additional paid order would break without breaking a shop rule.
Three levels of facts in one table
Here “current” matters. A catalog code may change from one valid state to the next. A table that keeps several versions at once would need enough attributes to identify the intended version before we could claim that it determines the historical code.
Why a key can imply more than it names#
Reflexivity gives whenever . Augmentation adds a common set to both sides of a dependency, giving from . Transitivity gives from and . Each rule follows from the agreement test. If two tuples agree on , for example, they agree on and hence on , while their agreement on is already given. These rules are sound and complete for functional dependencies. (Armstrong, 1974, §§4, 7 and 10, Silberschatz et al., ch. 7)
The attribute closure is the set of attributes determined by under the given rules. Begin with . Whenever the attributes collected so far contain a determinant, add its dependent attributes. Stop when another pass adds nothing. This procedure computes and lets us test a proposed superkey. (Silberschatz et al., ch. 7, Berkeley CS 186, DB Design)
Starting with , add using , add using , and add using . All seven attributes are present, so is a superkey. Starting with adds only . Starting with adds nothing. Thus is a candidate key. Neither nor appears on a right-hand side of the listed dependencies. Any candidate key must include both, so this one is unique. Starting with gives and leaves line-specific facts outside the closure.
One catalog fact repeated three times#
Update anomaly. Renaming item 7 consistently requires changing three copies of its code. If we update just the first line, a query over the resulting non-null codes can detect the broken rule.
WITH flat_lines(product_id, catalog_code) AS (
VALUES (7, 'TEA-50'), (9, 'COFFEE'),
(7, 'TEA50'), (7, 'TEA50')
)
SELECT product_id, COUNT(DISTINCT catalog_code) AS code_count
FROM flat_lines
GROUP BY product_id
HAVING COUNT(DISTINCT catalog_code) > 1
ORDER BY product_id;
The result is (7, 2). A primary key on order and line number still accepts these four distinct lines. The bad state violates .
Insertion anomaly. The catalog already contains the 100 g pack with code TEA-100. It has no sales in the example. A table with one row per order line cannot hold the fact about the pack without inventing a line. The same problem occurs for a draft order with no lines.
Deletion anomaly. Order 101 line 2 is the only row carrying item 9’s code. Deleting that line would delete the only copy of a still-valid catalog fact.
The duplicated fact is the current code. The two prices of item 7 record distinct agreements and must both be retained. Deciding what can be stored once begins with the dependencies, rather than with repeated values found in a sample. (Silberschatz et al., ch. 7, Berkeley CS 186, DB Design)
Next#
The dependencies tell us which facts can move into separate relations. Normalization and decomposition tests whether their join recovers the original information and whether the rules can still be checked locally.
References#
- William W. Armstrong. Dependency Structures of Data Base Relationships. IFIP Congress, 1974, §§4, 7, 10. Defines dependency families and their axiomatic properties.
- Abraham Silberschatz, Henry F. Korth and S. Sudarshan. Database System Concepts, ch. 7, 7th edition. Develops redundancy, the inference rules and attribute closure.
- UC Berkeley CS 186. DB Design. Connects redundancy, anomalies and closure to schema design.