DEV Community

Kailas Warade
Kailas Warade

Posted on

SQL CHECK Constraint in Oracle: Prevent Invalid Data with a Practical Example

Have you ever wondered how to stop someone from entering a negative salary into an employee table?

You can check the value in your application, but SQL also provides a way to protect your data directly at the database level.

That is where the CHECK constraint is useful.

Let's understand it with a simple Oracle SQL example.

  1. What is a CHECK Constraint?

A CHECK constraint makes sure that a column value follows a condition before the data is stored.

For example:

  • Salary cannot be negative.
  • Age must be between 18 and 60.
  • Product quantity cannot be less than zero.
  • Marks must be between 0 and 100.
  1. Create an Employee Table

Let's create an employee table where salary cannot be negative or NULL.

CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
employee_name VARCHAR2(50),
salary NUMBER NOT NULL
CONSTRAINT chk_employee_salary
CHECK (salary >= 0)
);

Here:

  • "PRIMARY KEY" prevents duplicate employee IDs.
  • "NOT NULL" ensures salary cannot be left empty.
  • "CHECK (salary >= 0)" prevents negative salary values.
  1. Insert Valid Data

Let's insert an employee with a valid salary.

INSERT INTO employees
VALUES (101, 'Rahul', 50000);

This statement executes successfully because the salary is greater than zero.

Now let's try inserting a negative salary.

INSERT INTO employees
VALUES (102, 'Priya', -5000);

What happens?

Oracle rejects this row because -5000 does not satisfy the CHECK condition.

The row is not inserted.

  1. Check the Table Data

Run this query:

SELECT * FROM employees;

Expected result:

EMPLOYEE_ID EMPLOYEE_NAME SALARY
101 Rahu 50000

Only Rahul's record is inserted.

  1. Does CHECK Also Work with UPDATE?

Yes. A CHECK constraint is also applied when existing data is updated.

For example:

UPDATE employees
SET salary = -1000
WHERE employee_id = 101;

Oracle rejects this update because the new salary violates the CHECK condition.

So, the CHECK constraint protects data during both INSERT and UPDATE operations.

  1. CHECK vs NOT NULL

These two constraints are useful, but they do different jobs.

Constraint| Purpose
CHECK| Ensures a value satisfies a condition
NOT NULL| Prevents a column from storing NULL

One important point: "CHECK (salary >= 0)" alone does not prevent NULL values in Oracle. That is why we used "NOT NULL" separately.

  1. Try This SQL Challenge

Suppose you have a students table.

You want to make sure that a student's age is always between 18 and 60.

Which CHECK condition should you use?

A) "CHECK (age > 18)"

B) "CHECK (age BETWEEN 18 AND 60)"

C) "CHECK (age < 60)"

D) "CHECK (age != 18)"

Correct answer: B

CONSTRAINT chk_student_age
CHECK (age BETWEEN 18 AND 60)

This allows ages from 18 to 60, including both limits.

Final Takeaway

The CHECK constraint is a simple but useful way to protect database data.

Instead of depending only on application code, you can define business rules directly in your database.

You can use it for salary, age, quantity, marks, discount percentage, and many other real-world conditions.

For more examples of SQL constraints, including PRIMARY KEY, FOREIGN KEY, UNIQUE and NOT NULL, you can read this guide:

"SQL Constraints: Primary Key, Foreign Key, UNIQUE and NOT NULL" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/sql-constraints-primary-foreign-key-faq.html)

Your turn: Where would you use a CHECK constraint in a real-world database?

Share your example in the comments.


Top comments (0)