Relational Databases

The relational model and the database the SQL articles query. Codd's 1970 proposal and the problem of data independence it set out to solve; relations, tuples and domains, and how SQL tables differ from them; primary, foreign and surrogate keys; integrity constraints and the ones SQLite does not enforce by default; normalization to first, second and third normal form, worked on the NHANES prescription file, whose 19,643 rows store one diagnosis 2,483 times; and the several kinds of missing value that SQL's single NULL has to represent. Built on eleven tables of NHANES 2017-2018 in SQLite.

The relational model (Codd, 1970) stores data as relations, sets of tuples over named domains with no access paths, so queries keep their meaning when storage changes. Keys tie tables together, and a database enforces entity, referential and domain integrity, though SQLite enforces foreign keys only after PRAGMA foreign_keys = ON and treats types as advisory outside STRICT tables. Normalized to third normal form, the NHANES 2017-2018 prescription file (19,643 rows, 13 columns) becomes four tables that store each fact once: "Essential (primary) hypertension", stored 2,483 times, is stored once, and by Heath's theorem the split itself loses nothing. No set of the file's columns is a key, so a surrogate key is needed. SQL's one NULL covers several kinds of missing value.

The prescription medicines file of the National Health and Nutrition Examination Survey (NHANES) for 2017 to 2018 has 19,643 rows and 13 columns. Each row is either one medicine reported by one person or, for the 5,209 people who reported none or did not answer, a single row saying so. A person who takes 22 medicines has 22 rows, and each of them repeats that person's answer to "In the past 30 days, have you used or taken medication for which a prescription is needed?" and the count of medicines, 22. The reasons for use are coded in three columns, of which the second is filled in 1,070 rows and the third in 187. The phrase "Essential (primary) hypertension" is stored 2,483 times.

None of this is wrong. The file is a convenient way to publish the data in one piece. But it is an awkward way to keep data that will be updated, checked and queried by many people, and the reasons are the subject of the relational model of data, proposed by Edgar F. Codd in 1970 and still the basis of almost every database that holds structured records. This article covers that model: what a relation is, how keys tie tables together, which rules a database can enforce, and how a table like the prescription file is split into tables that each store a fact once. The SQL articles that follow query the result, a database of eleven NHANES tables in SQLite, the database engine built into Python.

Why a database

Files are enough for one analyst working on a fixed dataset; Introduction to Data Wrangling works that way. A database adds what files lack when data are shared and changing: many users reading and writing at once without corrupting each other's work, rules that reject data violating the declared structure, transactions that apply a group of changes completely or not at all, recovery after a crash, and control over who may read or change what. Data Engineering places databases among the other systems that store data, such as warehouses and data lakes. This article is about the model of data that most databases share.

Codd's relational model

The problem: data independence

Codd's paper opens with a requirement: "Future users of large data banks must be protected from having to know how the data is organized in the machine (the internal representation)." In the systems of the 1960s, programs depended on the representation in three ways he named: ordering dependence (programs assumed records came back in the order they were stored), indexing dependence (programs referred to indexes by name and failed when an index was removed) and access path dependence. The last was the most serious. When data were stored as trees or networks of records, a program had to follow the paths of the structure, from a part to its projects, say, and a program written for one arrangement failed on another. Codd's example is a data bank of parts, projects and the quantity of each part committed to each project, which can be arranged as five different tree structures; a program that prints the parts used by one project, written for any one of them, "will fail on at least three of the remaining structures" (Codd, 1970).

His answer was to give users a model of the data that has no paths at all: a collection of tables, which he called relations, whose only structure is their values. How the tables are stored, sorted and indexed is the system's business, and it can change without changing what any query means. This separation is called data independence.

Relations, tuples and domains

Codd used "relation" in its mathematical sense. Given sets $S_1, S_2, \ldots, S_n$, a relation $R$ on them is a set of $n$-tuples whose first element comes from $S_1$, second from $S_2$, and so on; that is,

$$R \subseteq S_1 \times S_2 \times \cdots \times S_n,$$

a subset of the Cartesian product. The sets $S_j$ are the domains of the relation, the pools of values a column may take (all possible ages, all drug identifiers), and $n$ is its degree. Codd drew a relation as an array with these properties: each row represents one tuple, "the ordering of rows is immaterial", "all rows are distinct", and each column is labeled with the name of its domain. Because users should not have to remember which column comes first, he proposed that they work with columns identified by name rather than position, with a role name added where two columns draw on the same domain.

The everyday vocabulary maps onto Codd's like this:

Relational model SQL Data analysis
relation table dataset
tuple row observation, record
attribute (named domain) column variable, feature
domain data type, constraints set of possible values

Where SQL tables differ from relations

SQL, the language that grew out of the model, does not follow it exactly, and the differences matter in practice.

Tables may contain duplicate rows. A relation is a set, so its rows are distinct, and Codd's projection operation (keeping some columns) removes the duplicates it creates. An SQL table without a key is a multiset. Copying the sex column of the 9,254 people into a table of its own gives a table of 9,254 rows, 4,697 of them 'female'; the relation it represents has two tuples. SQL removes duplicates only when asked, with SELECT DISTINCT, which the Special Operators and Clauses article covers.

Rows have no order, but columns do. SQL agrees with Codd about rows: the SQLite documentation states that if a query "does not have an ORDER BY clause, the order in which the rows are returned is undefined." The same query can return the same rows in a different order when the storage changes. Asking for the four examined people with a body mass index (BMI) above 70 returns them in the order of their survey numbers, 94117, 97938, 100082 and 102778; after an index on BMI is created, the identical query returns them in the order 100082, 94117, 102778, 97938, sorted by BMI, because SQLite now reads them through the index. The set of rows is the same, which is all the query asked for. This is data independence at work, and it is why a program must never rely on an order it did not request. Columns, on the other hand, have a fixed order in SQL, which SELECT * exposes.

Values can be missing. The 1970 paper has no notion of a missing value. SQL has NULL, discussed below and, with its three-valued logic, in Simple SQL Queries.

Keys

Primary keys

Codd (1970) defined the key that identifies each row: "Normally, one domain (or combination of domains) of a given relation has values which uniquely identify each element (n-tuple) of that relation. Such a domain (or combination) is called a primary key." A key should also be minimal, with no column that could be dropped while keeping it unique; Codd called such a key nonredundant. A table can have several minimal keys, the candidate keys, and one is chosen as the primary key.

In the NHANES database, every person has a respondent sequence number, seqn, which is the primary key of person. The blood pressure table has up to four readings per person, so its key is the pair (seqn, reading), a composite key. The prescription table shows why the choice of key is a question about the data and not a formality. The natural candidate is the pair of person and drug, on the assumption that a person reports a drug once. The data say otherwise: 60 person and drug pairs occur twice, 34 of them for albuterol, the asthma inhaler. The survey documentation explains such repeats: a participant may have reported two brand names with the same generic name, different forms or dosages of one product (its example is albuterol as an oral tablet and as an inhalation solution), or different lengths or reasons of use (National Center for Health Statistics, 2020). Because no combination of the published columns identifies a row, the table is given a surrogate key, rx_id, a number with no meaning beyond identity. Surrogate keys are common in practice for exactly this reason: natural keys that look unique often are not, and they can change. The database that holds this site's own page statistics makes the same choice for the opposite reason. Its table content.page (SQL Server; migration 0002_content_core.sql in the site's repository) gives each page an integer surrogate page_id for joins and a permanent identifier page_uid, because the natural key, the slug in a page's address, is unique but may change; former addresses are kept in a table content.page_alias. The same table declares a domain constraint, CHECK (slug <> '' AND slug NOT LIKE '%[^a-z0-9-]%'), which in SQL Server's pattern syntax rejects an empty slug and any slug containing a character outside the class [a-z0-9-], such as a space or a slash. Whether a capital letter falls inside [a-z] depends on the column's collation. The slug column takes the database's case-insensitive default, under which a range such as [a-z] can match capitals too, so the check does not by itself guarantee lowercase slugs; the migration gives tag names, where case matters, a binary collation instead.

Foreign keys

A key also lets one table refer to rows of another. Codd's definition: a column or combination of columns of relation $R$ is a foreign key "if it is not the primary key of R but its elements are values of the primary key of some relation S (the possibility that S and R are identical is not excluded)." The last clause allows a table to refer to itself, as an employee table refers to each employee's manager.

The eleven tables of the NHANES database used by the SQL articles. Each arrow runs from a foreign key to the primary key it refers to; for example, prescription.seqn must be the seqn of a person and rxreason.icd10 the code of a condition. Tables that share a key one-to-one, such as person and questionnaire, could be merged; they are kept apart because they come from different survey files with different coverage.
The eleven tables of the NHANES database used by the SQL articles. Each arrow runs from a foreign key to the primary key it refers to; for example, prescription.seqn must be the seqn of a person and rx_reason.icd10 the code of a condition. Tables that share a key one-to-one, such as person and questionnaire, could be merged; they are kept apart because they come from different survey files with different coverage.

The diagram is the database's schema: the tables, their columns and the links between them. Each link has a cardinality. A person has at most one row in exam (one-to-one, or rather one to zero-or-one, since 550 people were interviewed but not examined), but any number of rows in prescription (one-to-many). A prescription can have up to three reasons, and a condition can be the reason for many prescriptions, so rx_reason links prescriptions and conditions many-to-many. Knowing these cardinalities is the condition for writing correct joins, as Join Queries shows with a join that counts some people dozens of times.

Integrity constraints

A database can enforce rules that a file cannot. The common ones:

  • Entity integrity: a primary key is unique and never null, so every row can be identified.
  • Referential integrity: every value of a foreign key matches an existing primary key, or is null where that is allowed. No prescription may name a person who does not exist.
  • Domain constraints: a column's values have the declared type, may be required (NOT NULL), and may be restricted by a CHECK condition.

Codd (1970) framed the general idea as consistency: the stored data should satisfy a declared set of constraint statements, which the system can check on each change or periodically. In SQL, constraints are declared with the table. Two tables of the NHANES database:

CREATE TABLE drug (
    drug_id  TEXT PRIMARY KEY NOT NULL,
    name     TEXT NOT NULL
);
CREATE TABLE prescription (
    rx_id    INTEGER PRIMARY KEY,
    seqn     INTEGER NOT NULL REFERENCES person (seqn),
    drug_id  TEXT REFERENCES drug (drug_id),
    days     INTEGER
);

drug_id in prescription may be null: 134 reported medicines are recorded only as the codes 55555 (unknown), 77777 (refused) or 99999 (don't know), with no drug identifier, and a foreign key that is null refers to nothing and violates nothing.

With the constraints switched on, the database rejects bad data at the moment it arrives. Inserting a second drug with the identifier d00749 fails with UNIQUE constraint failed: drug.drug_id; a drug without a name fails with NOT NULL constraint failed: drug.name; a prescription for person number 1, who does not exist, fails with FOREIGN KEY constraint failed.

The phrase "with the constraints switched on" is where SQLite differs from most databases, and the differences are worth knowing because SQLite, which ships with Python, is often the first database a data scientist meets. Its documentation lists them as quirks (SQLite, n.d.-a):

  • Foreign keys are not enforced by default. SQLite parsed foreign key clauses long before it enforced them, and to avoid breaking existing databases, enforcement must be switched on for each connection with PRAGMA foreign_keys = ON. With it off, the prescription for the nonexistent person 1 is accepted without complaint; the database is now inconsistent, and only an explicit PRAGMA foreign_key_check reports the orphan row.
  • A primary key may contain NULLs unless it is an INTEGER PRIMARY KEY, is declared NOT NULL, or belongs to a WITHOUT ROWID or STRICT table, a behavior the documentation calls "a bug" kept for compatibility. A test table with a TEXT PRIMARY KEY accepted two rows whose key was null. The tables above say NOT NULL for this reason.
  • Types are advisory. SQLite stores what it is given: setting a person's weight, declared REAL, to the text 'not weighed' succeeds, and the value is stored as text. Since version 3.37.0 a table declared STRICT rejects it ("cannot store TEXT value in REAL column", followed by the table and column name). PostgreSQL, SQL Server and Oracle enforce declared types, and enforce declared foreign keys by default.

Normalization

Codd's normal form

Codd (1970) also proposed a "normal form" for relations. Some domains in the data systems of his time were themselves relations: an employee record held a job history, which held a salary history, and a list of children. He called such domains nonsimple, roughly what programmers called repeating groups, and gave a procedure for eliminating them: copy the primary key of the parent into each subordinate relation, make the copied key part of the subordinate's key, and remove the nonsimple domain from the parent. His employee example ends as four flat relations, employee, job history, salary history and children, each keyed by the employee number together with its own identifying columns. A relation whose domains are all simple can be stored as a plain two-dimensional array, and Codd noted that this form also suits the exchange of data between systems, being "devoid of pointers". "Further operations of a normalizing kind are possible", he wrote, but "are not discussed in this paper"; he defined the second and third normal forms in a later paper (Codd, 1972).

Kent (1983) summarized the first three normal forms in a sentence each, and his phrasing is easier to apply than the formal definitions:

  • First normal form: "all occurrences of a record type must contain the same number of fields". There are no repeating groups and no variable-length lists inside a row.
  • Second normal form "is violated when a nonkey field is a fact about a subset of a key." It matters only when the key has more than one column.
  • Third normal form "is violated when a nonkey field is a fact about another nonkey field".

Kent's summary of the second and third together is that "a nonkey field must provide a fact about the key, the whole key, and nothing but the key" (the quotations follow the printed article; the author's web copy spells "non-key" with a hyphen). The design goal behind them is that each fact is stored in exactly one place. These one-sentence forms are informal. The formal definitions are stated in terms of functional dependencies (a column $Y$ depends on a set of columns $X$ when rows that agree on $X$ always agree on $Y$) and candidate keys, and this article uses them only through checks on the data (Worked problem 2 below). Boyce–Codd normal form, the fourth and fifth normal forms that Kent goes on to describe, algorithms that reach a normal form, and proofs that a decomposition loses no information belong to database design theory, which is not covered here; Worked problem 3 proves the one case this article needs.

Worked example: the prescription file

The published file, RXQ_RX_J, has these columns for each row: the person (SEQN), whether they took any prescription medicine (RXDUSE), the generic drug name and its identifier (RXDDRUG, RXDDRGID), whether the container was seen (RXQSEEN), the number of days taken (RXDDAYS), up to three reason codes from the ICD-10-CM classification of diseases (RXDRSC1 to RXDRSC3) with their descriptions (RXDRSD1 to RXDRSD3), and the number of medicines the person reported (RXDCOUNT).

First normal form. The three reason columns are a repeating group laid out horizontally: a medicine has from zero to three reasons, and the file reserves three slots for every row, mostly empty. A question such as "how many medicines are taken for hypertension" must look in three places. The repair is Codd's procedure: a table rx_reason with one row per reason, keyed by the prescription and the position of the reason (1, 2 or 3), which holds 15,565 rows. Whether a value is atomic depends on the questions to be asked of it. 1,073 rows name combination products, such as ENALAPRIL; HYDROCHLOROTHIAZIDE. The survey treats each as one product with one identifier, and so does this database; an analysis of individual ingredients would need to split the name into a table of ingredients, and until then a search for one ingredient must use pattern matching, as Special Operators and Clauses does.

Second normal form. Once the rows of the file have an identifier, the medicine within the person, any column that describes the person alone is a fact about part of the key. RXDUSE and RXDCOUNT are such columns: one person with 22 medicines repeats the same two values 22 times, and nothing prevents two of the copies from disagreeing. RXDUSE moves to a table with one row per person (here questionnaire). RXDCOUNT can be dropped altogether, because it is derivable: for every one of the 4,045 people who reported medicines, it equals the number of their rows. Codd (1970) called a set of relations redundant when part of it can be derived from the rest by relational operations; a count of rows is derived by aggregation instead, but the risk is the same, since each stored copy of a derivable value is a chance for inconsistency.

Third normal form. The drug name is a fact about the drug identifier, not about the prescription; the description of a reason is a fact about its code. These are dependencies between non-key columns. They can be checked on the data rather than assumed: each of the 666 drug identifiers has exactly one name, and each of the 241 reason codes exactly one description (the special codes 55555, 77777 and 99999 have none). So drug names move to a drug table keyed by identifier, and descriptions to a condition table keyed by code. The text "Essential (primary) hypertension", stored 2,483 times in the file, is now stored once.

The result is the right-hand part of the schema above: prescription (one row per reported medicine), drug, rx_reason and condition. The four tables hold 106,245 values, and the person-level answers moved to questionnaire another 9,254, where the file held 255,359. The comparison flatters the design somewhat, because the database also leaves out two things the file records: whether the interviewer saw the container (RXQSEEN), and which of the three codes stands in the name of the 134 medicines without an identifier. Keeping both, as a column of prescription and a small table of coded names, brings the four tables to 120,947 values, and 130,201 with the person-level answers in questionnaire, about half (51%) of the file (Worked problem 3). The saving in space is the least of the gains. The normalized design removes three kinds of anomaly that the single file invites:

  • Update anomalies. Correcting the wording of a diagnosis in the file means changing every row that carries it, 2,483 of them for hypertension; miss one and the data contradict themselves. In condition it is one row.
  • Insertion anomalies. The file cannot record a drug identifier and its name unless someone takes the drug. The drug table can.
  • Deletion anomalies. Deleting the only person who takes a rare drug deletes the only record of its name. With a separate table, it does not.

The design has a cost. A question that spans the tables, such as which conditions are most often treated with a given drug, now needs joins, the subject of Join Queries. That is the usual division of labor: data are stored normalized, so that each fact has one home and updates are safe, and are joined into a wide table, one row per person with many columns, when a model or a plot needs them. The table of 5,533 examined adults used throughout Performing Exploratory Data Analysis is such a join of ten NHANES files. The two views are close relatives: Wickham (2014) describes tidy data, in which each variable is a column, each observation a row and each type of observational unit a table, as "Codd's 3rd normal form ... but with the constraints framed in statistical language" (Structuring Data).

Missing values and NULL

SQL marks a missing value with NULL, which is not zero, not an empty string and not equal to anything, including another NULL. One marker has to stand for situations that are different in kind, and the NHANES tables contain several of them:

  • Not applicable. The education question was asked only of adults aged 20 and over, so education is NULL for all 3,685 people under 20. Nothing is unknown; the question did not apply.
  • Unknown. The ratio of family income to the poverty line is NULL for 1,231 of the 9,254 people. A value exists in principle; the survey does not have it.
  • Refused or not known by the respondent. These are not NULL at all in the published data but codes: 7 for refused and 9 for don't know in education (13 adults), 77777 and 99999 in the drug names and the days of use. A query that averages a column holding the code 99999 as if it were a number of days gets an absurd answer, which Cleaning Data discusses for the same survey.
  • Known only to a limit. Age is top-coded at 80, so the 427 people recorded as 80 are 80 or older, and the income ratio is capped at 5 (1,208 people). These values are present but not exact.

The database stores all of these as published, so the SQL articles can show how each is handled. Collapsing them into one NULL loses the distinction between "did not apply", "refused" and "unknown", which can matter: people who refuse a question are rarely a random subset. NULL's behavior in comparisons and logic, the source of most surprises in SQL, is covered in Simple SQL Queries, and its behavior in aggregates in Aggregate Functions. One small example of the second: counting the refusal codes separately for the people under 20 and those 20 and over gives 13 for the older group and NULL, not 0, for the younger, because a sum over values that are all null is null.

SQL

Codd (1970) proposed that users query relations through a "universal data sublanguage based on an applied predicate calculus": a language that describes the set of data wanted rather than the steps for finding it. Several such languages were built at IBM in the 1970s; one of them, SEQUEL, a "structured English query language" (Chamberlin and Boyce, 1974), became SQL. SQL was first standardized in 1986, and the standard is now ISO/IEC 9075, but no product implements the whole standard and every product adds features of its own, so SQL comes in dialects: PostgreSQL, MySQL, SQL Server, Oracle and SQLite differ in details, from the function that computes a logarithm to whether FULL JOIN exists. The articles in this topic use SQLite, because it ships with Python and needs no server, and note the differences from the other main dialects where they matter. As the SQLite documentation puts it, SQLite "is not intended as a replacement or competitor" to client-server databases such as PostgreSQL; it is an embedded engine, a library that reads and writes a single file (SQLite, n.d.-a).

SQL statements fall into a few groups:

  • Data definition: CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, as above.
  • Data manipulation: INSERT, UPDATE, DELETE.
  • Queries: SELECT, which reads data and changes nothing. Most of a data scientist's SQL is queries, and so are the next five articles.
  • Transaction control: BEGIN, COMMIT, ROLLBACK, covered with Python in Queries in Python.

A few lexical rules apply in all dialects. Keywords are not case-sensitive (SELECT and select are the same); writing them in capitals is a convention. Line breaks and indentation are ignored. String literals take single quotes, 'female'; in standard SQL double quotes delimit identifiers such as column names, a rule SQLite relaxes with consequences shown in the next article. Two hyphens begin a comment that runs to the end of the line. A semicolon separates statements.

The most important property of SQL is that it is declarative. A query states what result is wanted, and the database decides how to compute it: which table to read first, whether to use an index, how to perform a join. The BMI query above returned the same rows with and without an index because it described a set, not a procedure. This is the payoff of Codd's data independence, and it is also why the SQLite documentation, describing the steps by which a SELECT is evaluated, cautions that the description "is purely illustrative" and that "in practice neither SQLite nor any other SQL engine is required to follow this or any other specific process" (SQLite, n.d.-b). The logical order in which the clauses of a query take effect is set out in Simple SQL Queries and Aggregate Functions.

Worked problems

The three problems settle claims the article makes about the prescription file: that it has no natural key, that its dependencies can be tested, and that the split into four tables loses nothing that the database keeps. The review-pass block of relational-databases.py recomputes every number on SQLite 3.49.1.

Problem 1 (no set of the file's columns is a key). Show that no combination of the 13 published columns can serve as the primary key of the 14,434 rows of reported medicines, so that a surrogate key is needed and not merely convenient.

Approach. A set of columns is a key only if no two rows agree on all of it. If two rows agree on every column, they agree on every subset of the columns, so a single pair of identical rows rules out every candidate at once. It is therefore enough to look for rows that repeat an earlier row in all 13 columns.

Solution. Fifteen rows do: 30 rows form 15 identical pairs, the same person reporting the same generic drug with the same days of use, the same reasons and the same answer about the container. No subset of the columns separates the two rows of a pair, so no subset is a key. The weaker candidate tried first in the article, the pair (person, drug identifier), fails more often, with 60 repeated pairs, because rows that differ in days or reasons still repeat it. The NCHS documentation's reasons for such repeats (two brand names with one generic name, two forms of one product) describe distinctions the public file does not record, so no amount of care in choosing columns can recover them. The surrogate rx_id, the row's position in the file, is the only identifier the data support.

Problem 2 (testing a functional dependency in SQL). A column $Y$ is functionally dependent on a set of columns $X$, written $X \to Y$, when any two rows that agree on $X$ also agree on $Y$. Give a query that tests a dependency on a table, prove that it does, and apply it to the dependencies the normalization relied on.

The test. Group the rows by $X$. The dependency holds exactly when no group contains two different values of $Y$:

SELECT RXDDRGID FROM raw_rx WHERE RXDDRGID <> ''
GROUP BY RXDDRGID
HAVING COUNT(DISTINCT RXDDRUG) > 1;      -- no rows: each identifier has one name

Proof. If two rows agree on $X$ and differ on $Y$, they fall in the same group, and that group has at least two distinct values of $Y$, so the query returns it. Conversely, a group returned by the query has two rows with the same $X$ and different $Y$, which is a violation. So the query returns no rows if and only if the dependency holds on the table.

A gap, and its repair. COUNT(DISTINCT Y) ignores NULLs (Aggregate Functions), so a group holding one value and one NULL passes the test. On a three-row table with $x = 1$ paired with 'a' and with NULL, the query above finds no violation. Adding MAX(Y IS NULL) to the count, so that a NULL counts as one more value, finds it. Whether a NULL should count as a disagreement is a choice; the strict form is the safe one for normalization, since a NULL beside a value means the two rows do not record the same fact.

Results. On the raw file, in both forms, the drug identifier determines the name and the name the identifier (666 of each, one to one); the person determines RXDUSE and RXDCOUNT; and the first reason code determines its description, once the three special codes, which have none, are set aside. As a check that the test can fail, the person does not determine the drug: 2,719 of the 4,045 people who reported medicines have more than one drug identifier (2,730 if a medicine recorded without an identifier, which the file stores as an empty string, counts as one more value). RXDCOUNT equals the number of rows for all 4,045, the fact that made it removable. A query of this kind can refute a dependency but not establish it: the data show that no two rows disagree, not that no future row will. The dependency is a statement about what the columns mean, and the data are only evidence for it.

Problem 3 (the decomposition loses nothing that the database keeps). Prove that splitting a table on a functional dependency loses no information, and check that joining prescription, drug, rx_reason, condition and questionnaire gives back the file.

The theorem. Let a table have columns $X$, $Y$ and $Z$ (each a set of columns), with $X \to Y$. Split it into its projections on $X \cup Y$ and on $X \cup Z$. Then the natural join of the two projections is the original table. The result is usually credited to Heath (1971) and called Heath's theorem, and it is the basis of every decomposition above; the proof is short.

Proof. Every row $t = (x, y, z)$ of the table has projections $(x, y)$ and $(x, z)$, which agree on $X$, so the join contains $t$. For the converse, take a row $(x, y, z)$ of the join. It comes from a row $t_1 = (x, y, z_1)$ of the table and a row $t_2 = (x, y_2, z)$. These two rows agree on $X$, so by the dependency $y_2 = y$, and $t_2 = (x, y, z)$ is a row of the table. The join therefore contains exactly the original rows. Without the dependency the converse fails: the join can pair $y$ from one row with $z$ from another and create rows that were never there.

Application. The theorem is stated for relations, which have neither duplicate rows nor NULLs, and the file has both. The surrogate rx_id removes the duplicates: with it in $X$, every row is distinct. The split of the drug name uses drug_id $\to$ name; the split of each reason's description uses code $\to$ description; and the reasons themselves move to rx_reason with their position, so the three columns can be laid out again. NULL drug identifiers match nothing in a join, so the rebuild uses left joins for them. The query in the script rebuilds all 14,434 rows of reported medicines with the person, RXDUSE, the drug identifier and name, the days, the three codes and descriptions, and RXDCOUNT computed as a count, and it agrees with the file in all 12 of these columns in every row, except the drug-name column of the 134 medicines without an identifier, discussed below. The 5,209 rows of people who reported no medicine are recovered from questionnaire, where rx_use is not 1 for exactly 5,209 people.

What the database does not keep. Two columns of the file do not come back. RXQSEEN was not loaded at all (the interviewer saw the container for 12,543 medicines). And for the 134 medicines without an identifier, the file's name column holds which of the codes 55555, 77777 or 99999 applied, while the database keeps only a NULL. Neither loss is caused by normalization; they are choices of what to load. Adding RXQSEEN as a column of prescription and a two-column table of the 134 coded names would make the rebuild complete at a cost of 14,702 more values: 120,947 in the four tables, 130,201 with questionnaire, 51% of the file's 255,359.

Open questions

  • Do drug identifiers keep one name across survey cycles? Loading the prescription file of NHANES 2015 to 2016 (RXQ_RX_I) beside this one and running the dependency test of Problem 2 on the union would show whether drug_id $\to$ name holds over time, which a database holding several cycles would need.
  • Is each condition's description stable across ICD-10-CM revisions? Comparing the 241 code descriptions here with the official ICD-10-CM code tables of later fiscal years would show whether condition needs a version column, a case where a dependency that holds within one file fails across files.
  • How much space does normalization save on disk? Storing the file as one wide SQLite table and as the normalized tables, with and without indexes on the foreign keys, and comparing page counts from SQLite's dbstat table would replace the count of values used here with the bytes actually stored.
  • Can the 15 identical pairs be told apart? The documentation's reasons for repeats suggest brand names or dosage forms; whether any public NHANES file records them, and whether they separate all 15 pairs, would settle whether a natural key exists at all for these data.

Where it leads

The relational model stores each fact once, in tables tied together by keys, and leaves the method of finding data to the system. Simple SQL Queries begins querying the result: reading one table with SELECT, FROM and WHERE, and the three-valued logic that the NULLs described above bring into every condition. Database design in its formal sense (dependencies, normal forms beyond the third, decomposition algorithms) and the guarantees a database adds to files (transactions, concurrency, recovery) are subjects of their own that this collection does not cover.

In Python

The database is built from the public NHANES files by tools/experiments/data-science/sqldata.py in the site's repository. The pattern is the one Queries in Python explains in detail: connect, create the tables, insert the rows with placeholders, commit.

import sqlite3

con = sqlite3.connect("nhanes.db")
con.execute("PRAGMA foreign_keys = ON")      # SQLite does not enforce them by default
con.executescript(SCHEMA)                     # the CREATE TABLE statements
con.executemany("INSERT INTO drug VALUES (?, ?)", drug_rows)
con.commit()
con.execute("PRAGMA foreign_key_check").fetchall()   # [] when every reference resolves

The numbers in this article are computed by tools/experiments/data-science/relational-databases.py, which builds the database, checks the dependencies on the raw file and exercises the constraints on a scratch copy.

References

Reviewed: verified, 23 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 23 Nov 2026 · editorial review, 23 Nov 2026 · 7 revisions