Consider the following table and view in PostgreSQL:
CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT, department TEXT);
INSERT INTO employees (name, department) VALUES ('Alice', 'Sales'), ('Bob', 'HR');
CREATE VIEW sales_employees AS
SELECT * FROM employees WHERE department = 'Sales'
WITH CHECK OPTION;What happens when you run this query?
INSERT INTO sales_employees (name, department) VALUES ('Charlie', 'HR');CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT, department TEXT); INSERT INTO employees (name, department) VALUES ('Alice', 'Sales'), ('Bob', 'HR'); CREATE VIEW sales_employees AS SELECT * FROM employees WHERE department = 'Sales' WITH CHECK OPTION; INSERT INTO sales_employees (name, department) VALUES ('Charlie', 'HR');
Think about what the CHECK OPTION enforces on inserts through the view.
The WITH CHECK OPTION on the view ensures that any inserted or updated rows through the view must satisfy the view's WHERE condition. Since 'Charlie' has department 'HR', which does not match 'Sales', the insert violates the CHECK OPTION and fails.
Given the same employees table and sales_employees view as before, what is the result of this update?
UPDATE sales_employees SET department = 'HR' WHERE name = 'Alice';
CREATE TABLE employees (id SERIAL PRIMARY KEY, name TEXT, department TEXT); INSERT INTO employees (name, department) VALUES ('Alice', 'Sales'), ('Bob', 'HR'); CREATE VIEW sales_employees AS SELECT * FROM employees WHERE department = 'Sales' WITH CHECK OPTION; UPDATE sales_employees SET department = 'HR' WHERE name = 'Alice';
Consider what the CHECK OPTION enforces on updates through the view.
The WITH CHECK OPTION requires that updated rows still satisfy the view's WHERE condition. Changing Alice's department to 'HR' would make her row no longer satisfy the condition, so the update fails.
What is the main purpose of using WITH CHECK OPTION when creating a view in PostgreSQL?
Think about data integrity when modifying data through views.
WITH CHECK OPTION enforces that any insert or update through the view must produce rows that satisfy the view's WHERE clause, preserving data consistency.
Which of the following CREATE VIEW statements will cause a syntax error in PostgreSQL?
Check the correct syntax for adding CHECK OPTION to a view.
The correct syntax requires the keyword WITH before CHECK OPTION. Option A omits WITH, causing a syntax error.
You have this setup:
CREATE TABLE products (id SERIAL PRIMARY KEY, name TEXT, price NUMERIC);
INSERT INTO products (name, price) VALUES ('Pen', 1.5), ('Notebook', 3.0);
CREATE VIEW expensive_products AS
SELECT * FROM products WHERE price > 2
WITH CHECK OPTION;When you run this update, it fails:
UPDATE expensive_products SET price = 1.0 WHERE name = 'Notebook';
Why does this update fail?
CREATE TABLE products (id SERIAL PRIMARY KEY, name TEXT, price NUMERIC); INSERT INTO products (name, price) VALUES ('Pen', 1.5), ('Notebook', 3.0); CREATE VIEW expensive_products AS SELECT * FROM products WHERE price > 2 WITH CHECK OPTION; UPDATE expensive_products SET price = 1.0 WHERE name = 'Notebook';
Consider the view's filter condition and how CHECK OPTION enforces it on updates.
The WITH CHECK OPTION enforces that updated rows must still satisfy the view's WHERE clause. Setting price to 1.0 breaks the condition price > 2, so the update fails.