SQL CHECK Constraint: Prevent Invalid Data with a Practical Example
Have you ever wondered how to stop someone from entering a negative salary into an employee table?
For example, an employee's salary should never be less than zero.
We can check this in application code, but Oracle SQL also gives us a way to enforce this rule directly in the database.
That's where the CHECK constraint comes in.
Let's understand it with a practical example.
- What is a CHECK Constraint?
A CHECK constraint allows us to define a condition for a column.
Whenever someone inserts or updates a record, Oracle checks that condition.
If the new value violates the condition, Oracle rejects the statement.
For example:
- Salary can be 25000.
- Salary can be 50000.
- Salary cannot be -5000.
- Create an Employee Table
Suppose we are creating an employee table.
Our requirement is simple: salary must be zero or greater, and it cannot be 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 is what each constraint does:
- "PRIMARY KEY": Prevents duplicate and NULL employee IDs.
- "CHECK": Does not allow negative salaries.
- "NOT NULL": Does not allow a missing salary.
We have also given our CHECK constraint a name: "chk_employee_salary".
Giving constraints meaningful names makes them easier to identify when troubleshooting database errors.
- Insert Valid and Invalid Records
Let's insert two employee records.
First INSERT:
INSERT INTO employees VALUES (101, 'Rahul', 50000);
This statement succeeds because Rahul's salary is 50000.
Now try inserting a negative salary.
Second INSERT:
INSERT INTO employees VALUES (102, 'Priya', -5000);
What happens now?
Oracle rejects this statement because -5000 does not satisfy the condition:
CHECK (salary >= 0)
Oracle raises a CHECK constraint violation error.
- What Will Be the Final Table Output?
Only the first record is inserted successfully.
employee_id| employee_name| salary 101| Rahul| 50000
Priya's record is not inserted.
- Does CHECK Also Work with UPDATE?
Yes.
A CHECK constraint is not limited to INSERT statements. It also checks updated values.
For example:
UPDATE employees SET salary = -1000 WHERE employee_id = 101;
Oracle rejects this update because the new salary is negative.
The existing salary remains unchanged.
This is useful when multiple users or applications work with the same database.
- CHECK Constraint vs NOT NULL
These two constraints have different purposes.
CHECK| NOT NULL
Checks a specified condition| Prevents NULL
values Can restrict negative salaries| Can require a salary value
Example: "CHECK (salary >= 0)"| Example: "salary NOT NULL"
One important point: a CHECK constraint alone does not reject NULL values. That's why we have also used NOT NULL in our employee table.
- Try This SQL Practice Question
The company wants to make sure that every employee's age is between 18 and 60.
Which CHECK condition would you use?
Option A
CHECK (age > 18 AND age < 60)
Option B
CHECK (age >= 18 AND age <= 60)
Option C
CHECK (age >= 0)
Option D
CHECK (age IS NOT NULL)
Think about the requirement before choosing.
Correct answer: Option B
It allows employees aged exactly 18 or 60 and rejects values outside that range.
Remember to add NOT NULL separately if age is mandatory.
Final Takeaway
The CHECK constraint is a simple but useful feature in Oracle SQL.
It helps prevent invalid values from entering a table, whether through INSERT or UPDATE statements.
Instead of depending only on application code, we can also enforce important business rules directly in the database.
If you want to learn more about PRIMARY KEY, FOREIGN KEY, UNIQUE and NOT NULL constraints, read this practical 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: Can you think of another real-world situation where a CHECK constraint would be useful?
For example, product quantity, discount percentage or employee age.
Share your example in the comments.
#SQL #OracleSQL #Database #LearnSQL
Disclosure: I used AI assistance to help prepare this article and reviewed the SQL examples.
Comments
Join the discussionNo comments yet. Be the first to comment.