October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

NOT NULL vs. CHECK Constraints: What Each One Validates

NOT NULL requires a value; CHECK validates a condition. Learn why a CHECK rule may still allow NULL and when to use both constraints.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

NOT NULL requires a column to have a value; CHECK tests whether a row satisfies a condition. A CHECK alone may still allow NULL, so use both when a value must be present and must meet a rule.

What does each constraint validate?

NOT NULL: presence

A NOT NULL constraint prevents an inserted or updated row from storing SQL NULL in that column. It does not restrict which non-null values are allowed. PostgreSQL 17 describes it as requiring that a column “must not assume the null value.” PostgreSQL 17: Constraints

CHECK: a condition

A CHECK constraint tests an expression against a row. It is useful for rules such as price > 0 or for relationships between columns in the same row. A false result violates the constraint, but how a database treats null or unknown results matters.

Does a CHECK constraint prevent NULL?

Not necessarily. In PostgreSQL 17, a CHECK is satisfied when its expression evaluates to true or null. If price is null, price > 0 evaluates to null rather than false, so CHECK (price > 0) does not by itself require a price. MySQL 8.4 likewise documents that a check condition may evaluate to TRUE or UNKNOWN, including for null values. PostgreSQL 17: Constraints · MySQL 8.4: CHECK Constraints

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use NOT NULL when presence is required. A comparison such as price = NULL is not the right way to test for null; MySQL recommends IS NULL and IS NOT NULL for null tests. MySQL 8.4: Problems with NULL Values

When should you use one, or both?

  • Use NOT NULL when the rule is simply that the field must be supplied.
  • Use CHECK when only certain values are valid, or when values in a row must satisfy a relationship.
  • Use both when a value must be present and must meet a condition.

For example, this PostgreSQL-style table definition requires a name and a price, and also requires the price to be greater than zero:

CREATE TABLE products (
  name text NOT NULL,
  price numeric NOT NULL CHECK (price > 0)
);

A table-level check can express a relationship between columns in the same row, such as comparing a regular price with a discounted price. PostgreSQL’s documentation gives this kind of comparison as a check-constraint use case. PostgreSQL 17: Constraints

Why not replace NOT NULL with CHECK?

In PostgreSQL, NOT NULL is functionally equivalent to CHECK (column_name IS NOT NULL), but PostgreSQL says the explicit NOT NULL form is more efficient. It also states the presence requirement directly, making the schema easier to read. PostgreSQL 17: Constraints

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3

What CHECK constraints cannot safely express

PostgreSQL assumes check conditions are immutable: for the same row, the expression is expected to keep producing the same result. Its documentation cautions that a check should not rely on data outside the row being checked. A rule involving other rows or tables therefore needs a different mechanism; do not treat CHECK as a general substitute for a foreign key, a uniqueness constraint, or a cross-row aggregate rule. PostgreSQL 17: Constraints

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Database and version differences

Constraint behavior and syntax can vary by database and version. The PostgreSQL behavior described here is from PostgreSQL 17 documentation; the MySQL behavior is from MySQL 8.4 documentation, which also documents an enforcement option for check constraints. SQLite’s CREATE TABLE reference documents both NOT NULL and CHECK constraints, but that alone is not a basis for assuming every detail matches PostgreSQL or MySQL. Check the documentation for the exact engine and version you deploy. SQLite: CREATE TABLE

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.