capypad
0 day streak
sql / expert
Snippet

Data Integrity Enforcement via View Check Options

The WITH CHECK OPTION clause prevents data modifications through a view that would result in rows that are not visible to the view itself. It ensures that any INSERT or UPDATE operation must satisfy the view's WHERE clause, providing a powerful layer of declarative data integrity at the presentation level.

snippet.sql
sql
1
2
3
4
5
CREATE VIEW high_priority_tasks AS
SELECT task_id, task_name, priority
FROM tasks
WHERE priority > 8
WITH CHECK OPTION;
Breakdown
1
CREATE VIEW high_priority_tasks AS
Defines a virtual table based on a specific query.
2
WHERE priority > 8
Filters the underlying table to only show tasks with a priority higher than 8.
3
WITH CHECK OPTION
Mandates that any row added or updated through this view must still have a priority > 8.