SQL Keywords

SQL ASSERTION

What does the SQL ASSERTION statement do?

SQL ASSERTION defines a global, schema-level constraint that must always evaluate to TRUE across one or more tables.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL ASSERTION:

SQL ASSERTION Full Explanation

ASSERTION is part of the SQL standard (starting with SQL-92) and lets you declare a rule that spans multiple tables or views. Unlike a table-level CHECK constraint, an ASSERTION is evaluated whenever any statement could change data referenced in its search condition. If the condition would become FALSE, the statement is rejected and an error is returned. Because it operates at the schema level, an ASSERTION can enforce complex business rules such as referential counts, mutually exclusive states, or cross-table aggregates without resorting to triggers. Despite its power, most commercial databases have not implemented ASSERTION due to performance and dependency-tracking challenges. In practice, equivalent behavior is usually achieved with triggers, materialized views, or application logic.

SQL ASSERTION Syntax

CREATE ASSERTION assertion_name
CHECK ( search_condition );

DROP ASSERTION assertion_name;

SQL ASSERTION Parameters

  • assertion_name (identifier) - Name of the assertion, unique within the schema.
  • search_condition (boolean) - Expression that must always evaluate to TRUE. May reference multiple base tables, views, and scalar functions.

Example Queries Using SQL ASSERTION

-- Ensure total outstanding orders never exceeds available inventory
CREATE ASSERTION inventory_ok
CHECK (NOT EXISTS (
  SELECT 1
  FROM   orders o
  JOIN   inventory i ON i.product_id = o.product_id
  GROUP  BY i.product_id, i.qty_available
  HAVING SUM(o.qty_ordered) > i.qty_available
));

-- Attempt to insert an order that would violate the assertion
INSERT INTO orders (product_id, qty_ordered) VALUES (42, 1000);
-- Error: assertion "inventory_ok" failed

-- Remove the rule when no longer needed
DROP ASSERTION inventory_ok;

Expected Output Using SQL ASSERTION

  • CREATE ASSERTION registers the rule and returns a confirmation message
  • Subsequent DML statements that make the search_condition FALSE abort with an integrity-constraint violation
  • DROP ASSERTION deletes the rule and lifts the restriction

Use Cases with SQL ASSERTION

  • Enforce complex business invariants that span multiple tables.
  • Guarantee aggregate constraints, such as "sum of account debits equals credits".
  • Replace manual trigger logic with a declarative, centrally managed rule.

Common Mistakes with SQL ASSERTION

  • Assuming major databases implement ASSERTION – most ignore the syntax.
  • Forgetting that the search condition must always be TRUE, not just after specific statements.
  • Using non-deterministic functions (e.g., NOW()) inside the check, which is forbidden by the standard.

Related Topics

First Introduced In

SQL-92 (ISO/IEC 9075:1992)

Frequently Asked Questions

What is the purpose of SQL ASSERTION?

An ASSERTION guarantees that a complex condition spanning multiple tables is always TRUE. If a data change would break the rule, the operation fails.

Do MySQL or PostgreSQL implement ASSERTION?

No. Although the keyword appears in the SQL standard, popular engines have not implemented it. Use triggers or application logic as a workaround.

Can an ASSERTION reference aggregate functions?

Yes, the standard allows aggregates in the search condition as long as the overall expression is deterministic and evaluates to a boolean.

How do I drop an ASSERTION?

Use DROP ASSERTION assertion_name; to remove the constraint and its enforcement.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!