capypad
0 day streak
sql / expert
Snippet

Data Integrity via WITH CHECK OPTION

The WITH CHECK OPTION clause on a view prevents users from inserting or updating rows that would not be visible through the view itself. This enforces business logic at the schema level, ensuring data consistency for updatable views.

snippet.sql
sql
1
2
3
4
5
CREATE VIEW Active_Staff AS
SELECT id, name, status
FROM Employees
WHERE status = 'ACTIVE'
WITH CHECK OPTION;
Breakdown
1
WHERE status = 'ACTIVE'
Defines the filter criteria for the view's contents.
2
WITH CHECK OPTION
Mandates that any DML operation via this view must satisfy the WHERE clause.