SI Database / Journal

What Is Data Validation? How to Check Records Before Trusting Them

On this page
A table of records where three rows carry green check marks and one row with a negative price carries an amber warning mark.
Illustrative non-NULL prices checked against price > 0. The SQL example also explains why CHECK alone permits NULL. Diagram by SI Database.

Data validation is the practice of checking a record against defined rules before a system accepts it or relies on it. A rule might say that a price must be greater than zero, or that every order must point to a product that exists. A record that breaks a rule can be rejected, corrected, or set aside for a person to review.

The rules sit in different places. Some live inside the database, where the system enforces them every time a row is added or changed. Others run inside a data pipeline, where a checking tool tests each new batch. Both kinds share a limit that matters as much as the checks themselves: a rule can confirm that a record is well-formed, but it cannot confirm that the record is true.

Rules a database can enforce

PostgreSQL, a relational database, supports several kinds of rules, called constraints. This small example, adapted from its documentation, shows three of them:

CREATE TABLE products (
    product_no integer PRIMARY KEY,
    name text,
    price numeric CHECK (price > 0)
);

CREATE TABLE orders (
    order_id integer PRIMARY KEY,
    product_no integer REFERENCES products (product_no),
    quantity integer
);

Read it as plain statements. Every product needs a unique number (the primary key). A non-NULL price must be greater than zero (the check). A non-NULL product number on an order must exist in the products table (the foreign key). Of that last rule, PostgreSQL’s documentation says: “Now it is impossible to create orders with non-NULL product_no entries that do not appear in the products table.”

Two more constraints complete the usual set. A not-null constraint rejects NULL, and a unique constraint prevents duplicate non-NULL values by default in PostgreSQL. A unique constraint also builds an index behind the scenes, which is one way a rule can affect speed; our article on database indexes explains that cost.

Rules that pass for the wrong reason

Several of these constraints have documented behavior that can surprise a reader, and the surprises involve NULL. In SQL, NULL marks a missing or unknown value. In PostgreSQL, it is different from zero and from an empty string.

  • A check can accept NULL. PostgreSQL states that “a check constraint is satisfied if the check expression evaluates to true or the null value.” A NULL price makes the expression price > 0 evaluate to NULL, so this check accepts the row. Blocking it takes a separate not-null constraint.
  • A unique column can contain multiple NULLs by default. PostgreSQL permits several rows with NULL in a unique column; it still rejects duplicate non-NULL values. The option NULLS NOT DISTINCT makes NULLs count as equal for this constraint. Other database systems can handle NULLs differently.
  • A foreign key can be skipped. Normally, a row with a NULL in its referencing column does not have to satisfy the foreign-key rule.

None of this is a defect. Each behavior is documented and defensible. Each can still surprise someone who assumed the rule meant “never empty and never repeated.” A validation rule is only as strong as the author’s understanding of what it tests.

PostgreSQL also draws a boundary around what a check can see: it does not support check constraints that reference table data other than the row being added or updated. A rule such as “an order’s total cannot exceed the customer’s credit limit” needs another mechanism.

Rules in a pipeline

Not all data arrives through a database that enforces rules. Files, spreadsheets, and API responses often reach a pipeline first. Tools such as Great Expectations let teams write checks as code. Its current documentation describes an Expectation as an assertion that can be tested against data. A Checkpoint runs validation definitions and can trigger actions after validation.

The idea is to write each rule once and run it on every new batch, the way a database runs its constraints on every new row.

What happens after a failure is a design decision that no tool makes for you. A team can stop the load, load the good rows while setting the bad ones aside, or load everything with a flag attached. The right choice depends on the cost of a mistake. A finance table may justify stopping; a marketing list may tolerate flagged rows. These options are our summary of common practice, and each organization has to weigh them for itself.

What validation cannot tell you

Our article on why clean data is not necessarily correct data gives an example:well-formatted but that is well formatted and wrong. A rule that accepts positive numbers will accept it. Validation tests the record against the rule; it does not test the record against the world.

Rules can also refuse good data. Imagine a rule that caps order quantity at 100, written when orders were small. A legitimate order for 500 units would be rejected, and someone would have to notice the rejection to learn the rule was out of date. (The example is our illustration.)

For that reason, treat each rule as a claim that needs an owner. Someone who understands the business should approve it, write down why it exists, and review rejected records for patterns. A burst of rejections from one source often means that either the source or the rule has changed. That habit is data governance on a small scale: a named person is responsible for what the rule means.

Where to start

  • Begin with errors that have already happened. A rule that would have caught a real mistake earns its keep.
  • Write each rule as a plain sentence before turning it into code or a constraint.
  • Decide in advance what happens when a record fails.
  • Look at the rejected records on a regular schedule.

A validated record has passed the tests someone thought to write. Whether those tests ask the right questions remains a human judgment.

Sources and further reading

Read next

Know someone who would find this useful?

Share it with a colleague or post it where your team talks about data.

Cover of The Data Behind AI

The Data Behind AI

A 48-page illustrated guide to databases, search, and intelligent systems. No coding required.

Explore the book →

← Back to the journal

Get new SI Database articles by email

One short email when we publish, plus news from the other Neura Collective sites. No spam.