capypad
0 day streak
sql / expert
Snippet

Enforcing Logic via WITH CHECK OPTION

The WITH CHECK OPTION ensures that any INSERT or UPDATE performed through the view must satisfy the view's WHERE clause. Using CASCADED extends this enforcement to all underlying views, preventing 'ghost' updates where data is modified but becomes invisible to the view.

snippet.sql
sql
1
2
3
4
5
CREATE VIEW high_value_orders AS
SELECT order_id, customer_id, total_amount
FROM orders
WHERE total_amount > 1000
WITH CASCADED CHECK OPTION;
Breakdown
1
CREATE VIEW high_value_orders AS
Defines a new virtual table (view) named high_value_orders.
2
WHERE total_amount > 1000
The logical constraint that filtered data must exceed 1000.
3
WITH CASCADED CHECK OPTION;
Enforces that any modification through this view must still satisfy the WHERE clause and all underlying view conditions.