0
0
PostgreSQLquery~20 mins

INSTEAD OF trigger for views in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
INSTEAD OF Trigger Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of INSTEAD OF trigger on view insert

Consider a PostgreSQL view employee_view on table employees with an INSTEAD OF INSERT trigger that inserts data into employees. What will be the result of this query?

INSERT INTO employee_view (id, name) VALUES (1, 'Alice');
SELECT * FROM employees WHERE id = 1;
PostgreSQL
CREATE TABLE employees (id INT PRIMARY KEY, name TEXT);
CREATE VIEW employee_view AS SELECT * FROM employees;
CREATE FUNCTION insert_employee() RETURNS trigger AS $$
BEGIN
  INSERT INTO employees VALUES (NEW.id, NEW.name);
  RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER trg_insert_employee INSTEAD OF INSERT ON employee_view
FOR EACH ROW EXECUTE FUNCTION insert_employee();
A1 | Alice
BNo rows returned
CSyntax error on INSERT
DRuntime error: trigger function failed
Attempts:
2 left
💡 Hint

INSTEAD OF triggers on views replace the original action with the trigger's code.

🧠 Conceptual
intermediate
1:30remaining
Purpose of INSTEAD OF triggers on views

What is the main purpose of an INSTEAD OF trigger on a view in PostgreSQL?

ATo automatically create indexes on views
BTo speed up SELECT queries on views
CTo allow modification operations on views that are normally not updatable
DTo prevent any data changes through the view
Attempts:
2 left
💡 Hint

Think about what views normally cannot do and how triggers help.

📝 Syntax
advanced
2:00remaining
Correct syntax for creating an INSTEAD OF UPDATE trigger on a view

Which of the following is the correct syntax to create an INSTEAD OF UPDATE trigger on a view product_view that calls function update_product()?

ACREATE TRIGGER trg_update_product ON product_view INSTEAD OF UPDATE EXECUTE PROCEDURE update_product();
BCREATE TRIGGER trg_update_product INSTEAD OF UPDATE ON product_view EXECUTE FUNCTION update_product();
CCREATE TRIGGER trg_update_product FOR EACH ROW INSTEAD OF UPDATE ON product_view EXECUTE FUNCTION update_product();
DCREATE TRIGGER trg_update_product INSTEAD OF UPDATE ON product_view FOR EACH ROW EXECUTE FUNCTION update_product();
Attempts:
2 left
💡 Hint

Remember the order of clauses in CREATE TRIGGER and the exact keywords.

🔧 Debug
advanced
2:30remaining
Why does this INSTEAD OF DELETE trigger fail?

Given this trigger function and trigger, why does deleting from the view customer_view fail?

CREATE FUNCTION delete_customer() RETURNS trigger AS $$
BEGIN
  DELETE FROM customers WHERE id = OLD.id;
  RETURN OLD;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trg_delete_customer INSTEAD OF DELETE ON customer_view
FOR EACH ROW EXECUTE FUNCTION delete_customer();

DELETE FROM customer_view WHERE id = 10;
AThe trigger function must return NULL for INSTEAD OF DELETE triggers
BThe trigger function must return OLD, but it returns NULL
CThe DELETE statement inside the trigger is invalid syntax
DThe trigger is missing FOR EACH STATEMENT clause
Attempts:
2 left
💡 Hint

Check what the trigger function should return for INSTEAD OF DELETE triggers.

optimization
expert
3:00remaining
Optimizing INSTEAD OF trigger for bulk inserts on a view

You have an INSTEAD OF INSERT trigger on a view that inserts rows one by one into the underlying table. How can you optimize it to handle bulk inserts efficiently?

AKeep FOR EACH ROW trigger but add a delay to reduce load
BRewrite the trigger as a FOR EACH STATEMENT trigger and use a temporary table to batch inserts
CUse a BEFORE INSERT trigger on the underlying table instead
DDisable the trigger during bulk inserts and insert directly into the table
Attempts:
2 left
💡 Hint

Think about how triggers can be fired per row or per statement and how to batch operations.