Course contents 05 / 19

05 / 19 · Models and Integrity

Normalization and decomposition

Separate line, order and catalog facts, prove when their join is lossless and find which dependencies remain local.

The flattened order table repeats the current state of order 106 and the current code of item 7. Splitting the table gives each fact a home. An arbitrary split can also create a receipt that never existed, or hide a rule that the original table made easy to check.

Detect which fact repeats#

Keep the previous notation R(o,n,p,q,c,k,s)R(o,n,p,q,c,k,s). The candidate key is onon. Let FF consist of on→pqcon\to pqc, o→so\to s, p→kp\to k and k→pk\to p. Write F+F^+ for all dependencies implied by FF. The normal forms below are properties of a schema under these rules, rather than of the four example rows.

First normal form uses simple values at the chosen grain. A value is treated as a unit in the relation’s operations. In this design the line number, product and quantity are ordinary fields. Packing a variable collection of line records into an order field would change the model and prevent the following line attributes from being ordinary columns of that relation. (Codd, 1970, §1.4)

Second normal form additionally requires that no non-prime attribute depend on a proper subset of a candidate key. A prime attribute belongs to at least one candidate key. Here ss is non-prime and o→so\to s uses only the order part of onon. Separate the order state into O(o,s)O(o,s). In the remaining schema R′(o,n,p,q,c,k)R'(o,n,p,q,c,k), neither oo nor nn determines a non-prime attribute by itself. It passes the second-normal-form test. (Bernstein, 1976, §2)

The remaining schema still repeats the product code. For third normal form (3NF), every nontrivial implied dependency X→AX\to A with one attribute AA on the right must have a superkey XX or a prime AA. A dependency is nontrivial when A∉XA\notin X. Boyce–Codd normal form (BCNF) requires a superkey XX in every such dependency, including those with a prime right-hand attribute. In R′R', p→kp\to k has a non-superkey determinant and a non-prime right-hand side, so it fails both tests. (Silberschatz et al., ch. 7)

Move the code to P(p,k)P(p,k) and keep L(o,n,p,q,c)L(o,n,p,q,c) for the line. Relative to the stated rules, OO, PP and LL are all in BCNF. Their nontrivial determinants contain a key. In OO the key is oo, in PP either pp or kk, and in LL the key is onon.

Figure

Three relations for three kinds of fact

Three relations for three kinds of factThe flattened schema splits into orders and lines with code. Lines with code split into lines and catalog items.R(o, n, p, q, c, k, s)oO(o, s)R′(o, n, p, q, c, k)pL(o, n, p, q, c)P(p, k)
Splitting on o puts state in O. Splitting on p puts the current code in P. L keeps the agreed price.

Reconstruct a valid relation#

A decomposition is lossless under FF if naturally joining the projections of every instance satisfying FF gives that instance back. For a binary decomposition into schemas UU and VV with union RR, under functional dependencies alone this is equivalent to U∩VU\cap V determining all of UU or all of VV in F+F^+. (Silberschatz et al., ch. 7, Berkeley CS 186, DB Design)

To see why the condition suffices, take a tuple in the join. It combines a UU-projection of one original tuple and a VV-projection of another. The originals agree on U∩VU\cap V. If that intersection determines VV, they agree on all of VV. The joined tuple is therefore the first original tuple. Every original tuple joins its own projections, so there are neither missing nor extra tuples.

Our first split shares oo, which determines OO. Our second shares pp, which determines PP. Each split is lossless, so the two joins in reverse reconstruct the flattened lines. SQL needs DISTINCT in the projections to give each projected tuple one copy.

WITH flat(order_id, line_no, product_id, quantity, cents, code, state) AS (
    VALUES (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')
),
lines AS (
    SELECT DISTINCT order_id, line_no, product_id, quantity, cents FROM flat
),
orders AS (
    SELECT DISTINCT order_id, state FROM flat
),
products AS (
    SELECT DISTINCT product_id, code FROM flat
)
SELECT order_id, line_no, product_id, quantity, cents, code, state
FROM lines
JOIN orders USING (order_id)
JOIN products USING (product_id)
ORDER BY order_id, line_no;

The query gives the original four rows. For order 106 it restores line 1 with code TEA50, price 350 and state paid, and line 2 with the same code and state at price 300. A real decomposed database also stores the draft order 105 and the unsold 100 g pack in the parent tables. The demonstration projects from lines, so those independent facts are absent from its input.

A different split of just the two order-106 lines into (n,p)(n,p) and (p,c)(p,c) shares pp. The first projection contains (1,7)(1,7) and (2,7)(2,7). The second contains (7,350)(7,350) and (7,300)(7,300). Both line numbers now match both prices, giving four tuples. In particular (1,7,300)(1,7,300) was never sold. The split discarded the association of line number with price. The join exposes the loss by inventing combinations.

Keep the rules enforceable on the parts#

For each resulting schema RiR_i, take FiF_i to be the dependencies from F+F^+ whose attributes all belong to RiR_i. The decomposition preserves dependencies if the union of those local dependency sets implies all of FF. Equivalently, the closure of that union is F+F^+. In the shop split, each of the four stated dependencies is already in one component, so the test passes. This means no join is needed to test these functional rules. References between tables still need foreign keys. (Silberschatz et al., ch. 7, Berkeley CS 186, DB Design)

Losslessness and dependency preservation can differ. Suppose T(o,v,b)T(o,v,b) records which counter bb serves order oo for service vv. An order uses at most one counter per service, so ov→bov\to b. Each counter handles one service, so b→vb\to v. These are the only generating dependencies. Starting with ovov or obob determines all three attributes. Starting with bb determines bvbv, and oo and vv alone determine only themselves. The candidate keys are ovov and obob. Every attribute is prime, so 3NF holds. Since bb is no superkey, b→vb\to v violates BCNF.

The split into (b,v)(b,v) and (o,b)(o,b) is in BCNF and is lossless because the common bb determines the first component. The local dependencies cannot imply ov→bov\to b. For example, store (7,pickup)(7,\text{pickup}) and (8,pickup)(8,\text{pickup}) in the first component and (106,7)(106,7) and (106,8)(106,8) in the second. Each counter has one service and neither component violates its local dependencies. Their join assigns two counters to the same order and service. Losslessness applies to projections of a valid original instance. It gives no guarantee that every independently edited pair of components represents a valid original instance.

A 3NF synthesis can preserve dependencies and give a lossless join. One standard construction starts from an equivalent minimal set of dependencies, builds relations from their determinants and dependents, and adds a relation for an original candidate key if none of the components already contains one. The proof and precise construction are in the references. Here retaining TT in 3NF lets the original rule ov→bov\to b be checked in one relation. The design then accepts some repetition of service across orders that use the same counter. (Bernstein, 1976, §§6–7, Silberschatz et al., ch. 7)

Next#

The decomposition explains what each logical relation stores and why its references are sufficient. Reading the same facts now involves accessing separate relations. Pages, records and the buffer pool explains what that access means in memory and on storage.

References#

  1. Edgar F. Codd. A Relational Model of Data for Large Shared Data Banks. Communications of the ACM 13(6), 1970, §1.4. Introduces the normal form of relations over simple domains.
  2. Philip A. Bernstein. Synthesizing Third Normal Form Relations from Functional Dependencies. ACM Transactions on Database Systems 1(4), 1976, §§2, 6–7. Gives the normal forms and a synthesis procedure.
  3. Abraham Silberschatz, Henry F. Korth and S. Sudarshan. Database System Concepts, ch. 7, 7th edition. Defines BCNF, 3NF, lossless join and dependency preservation, and proves the guarantees of the 3NF construction.
  4. UC Berkeley CS 186. DB Design. Gives the binary lossless-join test and examples of dependency checks after decomposition.