0
0
PostgreSQLquery~20 mins

Views with CHECK OPTION in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
View Mastery with CHECK OPTION
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of INSERT on a view with CHECK OPTION

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');
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;

INSERT INTO sales_employees (name, department) VALUES ('Charlie', 'HR');
AThe insert succeeds and adds Charlie to employees with department 'HR'.
BThe insert succeeds but Charlie's department is automatically changed to 'Sales'.
CThe insert succeeds but Charlie is not visible in the sales_employees view.
DThe insert fails with a CHECK OPTION violation error.
Attempts:
2 left
💡 Hint

Think about what the CHECK OPTION enforces on inserts through the view.

query_result
intermediate
2:00remaining
Update behavior on a view with CHECK OPTION

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';
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;

UPDATE sales_employees SET department = 'HR' WHERE name = 'Alice';
AThe update fails with a CHECK OPTION violation error.
BThe update succeeds and Alice's department changes to 'HR'.
CThe update succeeds but Alice remains visible in the view with department 'Sales'.
DThe update succeeds but Alice is deleted from the employees table.
Attempts:
2 left
💡 Hint

Consider what the CHECK OPTION enforces on updates through the view.

🧠 Conceptual
advanced
1:30remaining
Purpose of WITH CHECK OPTION in views

What is the main purpose of using WITH CHECK OPTION when creating a view in PostgreSQL?

ATo allow the view to be updated even if it contains aggregate functions.
BTo automatically create indexes on the view for faster queries.
CTo ensure that any data inserted or updated through the view satisfies the view's defining condition.
DTo restrict SELECT queries on the view to only certain users.
Attempts:
2 left
💡 Hint

Think about data integrity when modifying data through views.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in view creation

Which of the following CREATE VIEW statements will cause a syntax error in PostgreSQL?

ACREATE VIEW v AS SELECT * FROM employees WHERE department = 'Sales' CHECK OPTION;
BCREATE VIEW v AS SELECT * FROM employees WHERE department = 'Sales' WITH LOCAL CHECK OPTION;
CCREATE VIEW v AS SELECT * FROM employees WHERE department = 'Sales' WITH CASCADED CHECK OPTION;
DCREATE VIEW v AS SELECT * FROM employees WHERE department = 'Sales' WITH CHECK OPTION;
Attempts:
2 left
💡 Hint

Check the correct syntax for adding CHECK OPTION to a view.

🔧 Debug
expert
2:30remaining
Diagnose why an update through a view with CHECK OPTION fails

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?

PostgreSQL
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';
ABecause the view does not allow updates on the price column.
BBecause the updated price 1.0 does not satisfy the view's WHERE condition price > 2, violating the CHECK OPTION.
CBecause the products table has a trigger preventing price changes below 2.
DBecause the UPDATE statement syntax is incorrect for views with CHECK OPTION.
Attempts:
2 left
💡 Hint

Consider the view's filter condition and how CHECK OPTION enforces it on updates.