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 . The candidate key is . Let consist of , , and . Write for all dependencies implied by . 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 is non-prime and uses only the order part of . Separate the order state into . In the remaining schema , neither nor 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 with one attribute on the right must have a superkey or a prime . A dependency is nontrivial when . Boyce–Codd normal form (BCNF) requires a superkey in every such dependency, including those with a prime right-hand attribute. In , 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 and keep for the line. Relative to the stated rules, , and are all in BCNF. Their nontrivial determinants contain a key. In the key is , in either or , and in the key is .
Three relations for three kinds of fact
Reconstruct a valid relation#
A decomposition is lossless under if naturally joining the projections of every instance satisfying gives that instance back. For a binary decomposition into schemas and with union , under functional dependencies alone this is equivalent to determining all of or all of in . (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 -projection of one original tuple and a -projection of another. The originals agree on . If that intersection determines , they agree on all of . 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 , which determines . Our second shares , which determines . 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 and shares . The first projection contains and . The second contains and . Both line numbers now match both prices, giving four tuples. In particular 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 , take to be the dependencies from whose attributes all belong to . The decomposition preserves dependencies if the union of those local dependency sets implies all of . Equivalently, the closure of that union is . 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 records which counter serves order for service . An order uses at most one counter per service, so . Each counter handles one service, so . These are the only generating dependencies. Starting with or determines all three attributes. Starting with determines , and and alone determine only themselves. The candidate keys are and . Every attribute is prime, so 3NF holds. Since is no superkey, violates BCNF.
The split into and is in BCNF and is lossless because the common determines the first component. The local dependencies cannot imply . For example, store and in the first component and and 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 in 3NF lets the original rule 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#
- 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.
- 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.
- 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.
- UC Berkeley CS 186. DB Design. Gives the binary lossless-join test and examples of dependency checks after decomposition.