smartyfy.mrindigi.com

Databases & SQL Master Course — 075. Functional Dependencies

Lesson 075 Functional Dependencies

Prerequisites and objectives

You understand row grain, candidate keys, and first normal form. You will now express functional dependencies, distinguish a domain rule from a pattern in a sample, find counterexamples with SQL, and trace a simple implication chain. These ideas prepare the next lessons on normalization beyond first normal form.

State the domain assumptions

In this fictional current catalog, each product has one current supplier and one current list price, regardless of warehouse. Each supplier code has one current supplier name. The extract records product availability by warehouse and repeats those descriptive facts. The assumptions apply to a current snapshot, not to historical prices or multiple sourcing arrangements.

Create the practice fixture

Use a fresh disposable SQLite database. All relevant dependency attributes are NOT NULL so the examples avoid the additional interpretation issues of unknown values. The extract deliberately enforces only its row number as a key; the dependency rules are stated requirements to investigate, not constraints already guaranteed by this table.

CREATE TABLE catalog_extract (
 row_no INTEGER PRIMARY KEY,
 product_code TEXT NOT NULL,
 warehouse_code TEXT NOT NULL,
 supplier_code TEXT NOT NULL,
 supplier_name TEXT NOT NULL,
 price_cents INTEGER NOT NULL
);
INSERT INTO catalog_extract VALUES
 (1,'P1','N','S1','North Supply',200),
 (2,'P1','S','S1','North Supply',200),
 (3,'P2','N','S1','North Supply',500),
 (4,'P3','N','S2','South Supply',900);

Read determines as a consistency rule

A functional dependency from X to Y means that whenever two valid rows agree on X, they must agree on Y. X can be one column or a set of columns. The rule does not require different X values to have different Y values. Several products may legitimately share one supplier.

Under our assumptions, product_code determines supplier_code and price_cents. Supplier_code determines supplier_name. Product_code does not determine warehouse_code because P1 appears in both N and S. The arrow describes consistency of values, not causation, physical processing order, or a formula that computes a supplier code from a product’s characters.

SELECT product_code,COUNT(DISTINCT supplier_code) AS suppliers,COUNT(DISTINCT price_cents) AS prices FROM catalog_extract GROUP BY product_code ORDER BY product_code;
SELECT product_code FROM catalog_extract GROUP BY product_code HAVING COUNT(DISTINCT warehouse_code)>1 ORDER BY product_code;

The current rows contain no product with several supplier codes or prices, while P1 demonstrably has several warehouses. A counterexample can disprove a proposed dependency in the data. Absence of a counterexample cannot prove that the dependency is a permanent business rule; that claim comes from the domain contract.

Follow implications carefully

If product_code determines supplier_code and supplier_code determines supplier_name, then product_code also determines supplier_name under these assumptions. This is a transitive implication. Starting from a product code, the stated rules let us determine supplier code, supplier name, and current price, but not warehouse.

Adding warehouse to product code would identify a product/warehouse availability fact if the domain also guarantees at most one such row. The extract’s row_no is a technical identifier that distinguishes even inconsistent duplicates. Its presence does not remove the repeated supplier facts or enforce the intended dependencies among business attributes.

Arrows show product_code determines supplier_code and price_cents, and supplier_code determines supplier_name. The transitive consequence product→supplier_name is stated. A distinct counterexample panel shows P1 in both N and S, so product does not determine warehouse. Conflicting current S1 names would violate the declared supplier-name rule.
A functional dependency constrains permitted row values. It is directional, does not assert causation, and must come from the domain contract. Open full-size diagram

Use SQL to search for violations

Group by the proposed determinant and count distinct dependent values. More than one known dependent value identifies a violation for this non-NULL fixture. If NULL were allowed, COUNT DISTINCT would ignore it, so a separate missing-value policy and tests would be necessary. Do not silently treat a diagnostic query as a universal theorem prover.

A conflicting supplier name can mean bad data, a legitimate rename mixed across time, or an incorrect assumption that codes are globally scoped. Investigate the meaning before deciding to overwrite a value. Normalization depends on the right dependencies; cleaning data to force an unjustified rule can destroy valid information.

Connect dependencies to future design

Repeating North Supply in every product/warehouse row creates an update burden: a current-name change must stay consistent everywhere. A later decomposition can store supplier identity and name once, then reference it. This lesson stops at recognizing the dependency and its testable consequences; lossless decomposition and preservation deserve their own careful treatment.

Keep dependency statements versioned with the requirements. A future rule allowing several suppliers per product would invalidate product_code determines supplier_code. A historical price table would need an effective time or version in its grain. Dependencies are about permitted states of a particular model, not timeless properties of familiar column names.

Four worked exercises

Exercise 1

Insert a contradictory supplier name for S1 and detect the violation.

Worked solution
BEGIN;
INSERT INTO catalog_extract VALUES (5,'P4','E','S1','Other Name',700);
SELECT supplier_code FROM catalog_extract GROUP BY supplier_code HAVING COUNT(DISTINCT supplier_name)>1 ORDER BY supplier_code;
ROLLBACK;

S1 now maps to two names, violating the stated current-name rule. The table accepts the row because its row-number key alone does not enforce that dependency. The diagnostic identifies a conflict but does not decide which name is correct.

Exercise 2

Use the stated implication chain to find P2’s supplier name and explain why warehouse is not determined.

Worked solution
SELECT DISTINCT supplier_code,supplier_name FROM catalog_extract WHERE product_code='P2';

P2 determines S1, and S1 determines North Supply. The domain does not say that a product belongs to only one warehouse. P2 happens to appear only in N in this sample, but that coincidence does not establish a general product-to-warehouse dependency.

Exercise 3

Show that price does not determine product by adding another product priced at 200 cents.

Worked solution
BEGIN;
INSERT INTO catalog_extract VALUES (5,'P4','E','S2','South Supply',200);
SELECT price_cents FROM catalog_extract GROUP BY price_cents HAVING COUNT(DISTINCT product_code)>1 ORDER BY price_cents;
ROLLBACK;

P1 and P4 can share a price while remaining different products. A dependency is directional: product determines price does not imply price determines product. Reversing an arrow requires its own independent rule.

Exercise 4

Introduce a conflicting current price for P1 and locate the affected product.

Worked solution
BEGIN;
INSERT INTO catalog_extract VALUES (5,'P1','E','S1','North Supply',250);
SELECT product_code,MIN(price_cents),MAX(price_cents) FROM catalog_extract GROUP BY product_code HAVING COUNT(DISTINCT price_cents)>1 ORDER BY product_code;
ROLLBACK;

The result shows P1 with both 200 and 250. Under the stated current-price assumption, this is inconsistent. If warehouse-specific pricing were actually permitted, the model and determinant would need revision rather than automatically deleting one value.

Knowledge check

Question 1

What does X determines Y require?

  1. Every distinct X must have a distinct Y
  2. Y must be numerically calculated from X
  3. X must contain exactly one column
  4. Rows agreeing on X must agree on Y
Answer and explanation

D. Functional dependency is a consistency rule over matching determinant values.

Question 2

What can a clean sample prove about a proposed dependency?

  1. Only that no tested counterexample was found
  2. That the rule holds for every future valid row
  3. That the reverse dependency also holds
  4. That the table is automatically in every normal form
Answer and explanation

A. Domain requirements establish the general rule; samples can reveal violations.

Question 3

What follows from product to supplier and supplier to name?

  1. Supplier name determines every product
  2. Warehouse determines every price
  3. Price determines supplier code
  4. Product determines supplier name
Answer and explanation

D. Transitivity follows the stated direction of both dependencies.

Question 4

Why does row_no fail to solve the repeated-name problem?

  1. Its uniqueness automatically makes supplier_code unique
  2. A surrogate key removes dependencies among other columns
  3. It identifies rows without enforcing the business dependency
  4. It automatically removes duplicate business facts
Answer and explanation

C. A surrogate row identity can coexist with redundant and inconsistent descriptive facts.

Question 5

Why is COUNT DISTINCT used cautiously with NULL?

  1. It counts NULL once as a separate known value
  2. It ignores NULL dependent values
  3. NULL makes every group’s count unknown
  4. It counts each NULL as a different supplier
Answer and explanation

B. The fixture excludes NULL; a nullable diagnostic needs an explicit missing-value policy.

Pitfalls and summary

Do not infer universal dependencies from a small sample, reverse arrows without justification, or confuse technical row identity with business consistency. State the model’s scope and time meaning, use counterexample queries to test data, and trace implications deliberately. Reliable dependency analysis is the foundation for meaningful normalization.

SQLite behavior and references

Examples were checked with SQLite 3.53.1 on 10 October 2026. Use a SQLite SQL editor; Python is not a prerequisite. Verify dialect-specific functions, comparison rules, and numeric behavior before moving the examples to another engine.

Primary references