Sunday, August 16, 2026
Home Academic Assertion in SQL - Basic

Assertion in SQL – Basic

In SQL, an assertion is a statement that ensures that certain conditions are always enforced in the database no matter what. Same as domain or other constraints, assertions differ in the way that they are defined separately from table definitions. It is a named statement that defines a condition that must be true for the data in the database. If the condition is not met, the assertion will fail and the database will generate an error.

Here is the syntax for creating an assertion in SQL:

CREATE ASSERTION assertion_name
CHECK (condition);

For example, to create an assertion called “salary_assertion” that checks that no employee in the “employees” table has a salary greater than $100,000, you could use the following statement:

CREATE ASSERTION salary_assertion
CHECK (salary <= 100000);

We can create different assertions for specific conditions that must always satisfy in SQL. For example, the department_id attribute in manager relation is always not null since each manager works at least in one department. The following example shows an assertion named nomanager which checks that all the tuples in manager relation with department_id being NULL as not a manager.

CREATE ASSERTION nomanager
CHECK ( NOT EXISTS (
    SELECT * FROM MANAGER
    WHERE Department_id IS NULL
) );

Hence, the above assertion ensures that there is no manager who is not assigned any department at any time.

Another example can be the sum of all loan amounts for each branch must be less than the sum of all account balances at the branch. This can be satisfied by creating assertion as below:

CREATE ASSERTION sum_constraint CHECK ( <– Start Global Check
  NOT EXISTS ( <– Start Bad-Data Filter
    SELECT 1 FROM branch B
    WHERE ( <– Start Loans Calculation
      SELECT SUM(amount) FROM loan L
      WHERE L.branch_name = B.branch_name
    ) <– End Loans Calculation
    >= ( <– Start Accounts Calculation
      SELECT SUM(amount) FROM account A
      WHERE A.branch_name = B.branch_name
    ) <– End Accounts Calculation
  ) <– End Bad-Data Filter
); <– End Global Check

 To drop an assertion, you can use the following statement:

DROP ASSERTION assertion_name;

Assertions are useful for enforcing data integrity and ensuring that the data in the database meets certain conditions. They can be used to enforce business rules or to ensure the consistency of the data. However, they can also be time-consuming to create and maintain, so they are not always used in practice.

CHECK vs ASSERTION

DBMS Constraint Hierarchy: CHECK vs. ASSERTION

1. Table-Level Constraint (CHECK)

Scoped to a single table. Validates data row-by-row locally.

CREATE TABLE Employee (
  Salary INT,
  CHECK (Salary <= 100000)
);
Scope: Isolated to Employee table.
GLOBAL GUARD

2. Schema-Level Constraint (ASSERTION)

Standalone entity. Enforces multi-table business logic database-wide.

CREATE ASSERTION loan_limit
CHECK (NOT EXISTS (
  SELECT * FROM Branch
  WHERE SUM(Loan) >= SUM(Account)
));
Scope: Spans Branch, Loan, and Account tables.
SQL assertions provide a way to define rules that the database should always maintain. Although they are not commonly supported in many popular database systems, understanding the concept is useful for learning database constraints and integrity rules. For beginners, assertions also help build a stronger understanding of how databases can enforce business rules beyond basic constraints such as PRIMARY KEY, FOREIGN KEY, UNIQUE, and CHECK. Even when assertions are unavailable in a particular DBMS, the same requirements can often be implemented using supported constraints, triggers, or application-level validation. Happy Learning! – DataSagar
datasagarhttp://www.DataSagar.com
Sagar is multidisciplinary technologist, educator, and entrepreneur based in Nepal. As the founder of Illionso Technologies and Kashi Garden Resort, he operates at the intersection of web architecture, data intelligence, innovation, and digital transformation. From building digital solutions to scaling real-world concepts, his mission is to share knowledge alongside merge emerging as well as disruptive technologies with transformative offline experiences.
RELATED ARTICLES

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

What is Answer Engine Optimization (AEO)? Getting Ready for AI Powered Search and Agentic Era Digital Marketing

Search is changing faster than it has in the last two decades. For years, businesses focused on ranking their websites on search...

Most Popular

Why Linux Won the Infrastructure War? Lessons from Linus Torvalds

In 1991, 21-year-old Linus Torvalds announced a "hobby" operating system. Today, Linux powers supercomputers, cloud servers, and Android. Explore his journey, the open-source economy, and the lessons for modern developers.

What Is Vibe Coding? A Complete Beginner’s Guide to Building Software and Web Pages With AI

A few years ago, building a website meant sitting down with a programming book, picking a language, and slowly working through syntax...

What is Answer Engine Optimization (AEO)? Getting Ready for AI Powered Search and Agentic Era Digital Marketing

Search is changing faster than it has in the last two decades. For years, businesses focused on ranking their websites on search...

The Plain Text Web: Why llms.txt Is Becoming the New Site Standard

The web built today is heavy. Between client-side JavaScript, cookie banners, tracker scripts, and responsive CSS grids, a typical webpage carries megabytes...