Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is an INSTEAD OF trigger in PostgreSQL?
An INSTEAD OF trigger is a special trigger on a view that runs instead of the usual action (like INSERT, UPDATE, DELETE) on the view. It lets you define how to handle changes on views that normally can't be directly modified.
Click to reveal answer
beginner
Why do we need INSTEAD OF triggers for views?
Views often combine data from multiple tables and cannot be directly changed. INSTEAD OF triggers let us write code to update the underlying tables when someone tries to change the view.
Click to reveal answer
intermediate
How do you create an INSTEAD OF trigger for a view in PostgreSQL?
You first create a function that defines what happens on INSERT, UPDATE, or DELETE on the view. Then you create the trigger on the view using CREATE TRIGGER ... INSTEAD OF ... ON view_name FOR EACH ROW EXECUTE FUNCTION your_function();
Click to reveal answer
beginner
Can you directly insert data into a view without an INSTEAD OF trigger?
No, normally you cannot insert, update, or delete data directly in a view if it involves multiple tables or complex queries. INSTEAD OF triggers provide a way to handle these operations safely.
Click to reveal answer
intermediate
What happens if an INSTEAD OF trigger is defined on a view and you perform an UPDATE on that view?
The INSTEAD OF trigger function runs instead of the normal UPDATE. This function can update the underlying tables as needed to reflect the change.
Click to reveal answer
What does an INSTEAD OF trigger do on a view?
APrevents any changes to the view
BDeletes the view automatically
CRuns a function instead of the usual action on the view
DCreates a new table from the view
✗ Incorrect
INSTEAD OF triggers run a function instead of the usual INSERT, UPDATE, or DELETE on a view.
Why can't you normally update a view that combines multiple tables?
ABecause the database does not allow any updates
BBecause the view does not store data itself
CBecause views are read-only by default
DBecause views are always temporary
✗ Incorrect
Views do not store data themselves; they show data from underlying tables, so direct updates are not allowed without special handling.
Which SQL command is used to create an INSTEAD OF trigger on a view?
ACREATE FUNCTION ... INSTEAD OF
BCREATE VIEW ... INSTEAD OF TRIGGER
CALTER VIEW ... ADD TRIGGER
DCREATE TRIGGER ... INSTEAD OF ... ON view_name
✗ Incorrect
You use CREATE TRIGGER with the INSTEAD OF clause on the view to define the trigger.
What must you define to use an INSTEAD OF trigger?
AA trigger function that handles the operation
BA new table to replace the view
CA stored procedure to drop the view
DA user role with special permissions
✗ Incorrect
An INSTEAD OF trigger requires a trigger function that defines what happens when the trigger fires.
If you want to allow INSERTs on a view, what should you do?
ACreate an INSTEAD OF INSERT trigger on the view
BCreate a new table with the same name
CUse a normal INSERT statement on the view
DDrop the view and recreate it
✗ Incorrect
To allow INSERTs on a view, you create an INSTEAD OF INSERT trigger that handles inserting data into underlying tables.
Explain what an INSTEAD OF trigger is and why it is useful for views.
Think about how views show data but don't store it.
You got /4 concepts.
Describe the steps to create an INSTEAD OF trigger for a view in PostgreSQL.
Remember you need both a function and a trigger.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of an INSTEAD OF trigger on a view in PostgreSQL?
easy
A. To speed up queries on the view
B. To run custom code instead of the default insert, update, or delete on the view
C. To automatically create indexes on the view
D. To prevent any changes to the view data
Solution
Step 1: Understand what views normally do
Views in PostgreSQL are virtual tables that do not store data themselves and usually cannot be directly updated.
Step 2: Role of INSTEAD OF triggers
INSTEAD OF triggers let you define custom actions that run instead of the usual insert, update, or delete on the view, making it updatable.
Final Answer:
To run custom code instead of the default insert, update, or delete on the view -> Option B
Quick Check:
INSTEAD OF triggers = custom code on views [OK]
Hint: INSTEAD OF triggers replace default actions on views [OK]
Common Mistakes:
Thinking INSTEAD OF triggers speed up queries
Confusing INSTEAD OF triggers with indexes
Believing INSTEAD OF triggers block all changes
2. Which of the following is the correct syntax to create an INSTEAD OF trigger on a view named my_view for INSERT operations?
easy
A. CREATE TRIGGER trg_instead_of_insert ON my_view INSTEAD OF INSERT FOR EACH ROW EXECUTE FUNCTION trg_func();
B. CREATE TRIGGER trg_instead_of_insert INSTEAD OF INSERT ON my_view EXECUTE FUNCTION trg_func();
C. CREATE TRIGGER trg_instead_of_insert ON my_view INSTEAD OF INSERT EXECUTE FUNCTION trg_func();
D. CREATE TRIGGER trg_instead_of_insert ON my_view BEFORE INSERT EXECUTE FUNCTION trg_func();
Solution
Step 1: Recall correct trigger syntax in PostgreSQL
The syntax requires specifying the trigger name, the event timing (INSTEAD OF), the event type (INSERT), the target (ON my_view), and the function to execute.
Step 2: Identify the full correct syntax
CREATE TRIGGER trg_instead_of_insert ON my_view INSTEAD OF INSERT FOR EACH ROW EXECUTE FUNCTION trg_func(); correctly includes "ON my_view INSTEAD OF INSERT FOR EACH ROW EXECUTE FUNCTION trg_func();" which is the proper syntax for INSTEAD OF triggers on views.
Final Answer:
CREATE TRIGGER trg_instead_of_insert ON my_view INSTEAD OF INSERT FOR EACH ROW EXECUTE FUNCTION trg_func(); -> Option A
Quick Check:
INSTEAD OF triggers need FOR EACH ROW and correct order [OK]
Hint: INSTEAD OF triggers use 'FOR EACH ROW' and 'EXECUTE FUNCTION' [OK]
Common Mistakes:
Placing INSTEAD OF before ON
Omitting FOR EACH ROW
Using BEFORE instead of INSTEAD OF
3. Given the following setup:
CREATE VIEW emp_view AS SELECT id, name FROM employees;
CREATE FUNCTION emp_view_insert() RETURNS trigger AS $$ BEGIN INSERT INTO employees(id, name) VALUES (NEW.id, NEW.name); RETURN NEW; END; $$ LANGUAGE plpgsql;
CREATE TRIGGER trg_emp_insert INSTEAD OF INSERT ON emp_view FOR EACH ROW EXECUTE FUNCTION emp_view_insert();
INSERT INTO emp_view (id, name) VALUES (1, 'Alice');
SELECT * FROM employees WHERE id = 1;
What will the SELECT query return?
medium
A. Empty result because views cannot be inserted
B. No rows
C. Syntax error on INSERT
D. (1, 'Alice')
Solution
Step 1: Understand the INSTEAD OF trigger effect
The trigger function inserts the new row into the base table employees when an insert is done on the view emp_view.
Step 2: Trace the INSERT and SELECT
The INSERT INTO emp_view triggers emp_view_insert(), which inserts (1, 'Alice') into employees. The SELECT then finds this row.
Final Answer:
(1, 'Alice') -> Option D
Quick Check:
INSTEAD OF trigger inserts row into employees [OK]
Hint: INSTEAD OF triggers redirect inserts to base tables [OK]
Common Mistakes:
Assuming views cannot be inserted into
Expecting syntax error on insert
Forgetting trigger function inserts data
4. You wrote this trigger function for an INSTEAD OF UPDATE trigger on a view:
CREATE FUNCTION trg_update() RETURNS trigger AS $$ BEGIN UPDATE employees SET name = NEW.name WHERE id = NEW.id; END; $$ LANGUAGE plpgsql;
When you try to update the view, you get an error. What is the problem?
medium
A. The function does not return a value
B. The trigger should be BEFORE, not INSTEAD OF
C. The UPDATE statement syntax is incorrect
D. The trigger function must be written in SQL, not plpgsql
Solution
Step 1: Check trigger function requirements
Trigger functions must return a value, usually NEW or OLD, depending on the trigger type.
Step 2: Identify missing RETURN statement
The function lacks a RETURN statement, causing an error when the trigger tries to get a result.
Final Answer:
The function does not return a value -> Option A
Quick Check:
Trigger functions must return NEW or OLD [OK]
Hint: Always RETURN NEW or OLD in trigger functions [OK]
Common Mistakes:
Omitting RETURN in trigger functions
Confusing INSTEAD OF with BEFORE triggers
Assuming plpgsql is not allowed
5. You want to create an updatable view for a table products(id, name, price) that only allows updating the price through the view. Which approach using an INSTEAD OF trigger is correct?
hard
A. Create an INSTEAD OF UPDATE trigger that updates all columns in products regardless of input
B. Create a BEFORE UPDATE trigger on the view that blocks changes to name and id
C. Create an INSTEAD OF UPDATE trigger that updates only the price column in products and returns NEW
D. Create a rule instead of a trigger to handle updates on the view
Solution
Step 1: Understand the goal
The view should allow updating only the price column, so the trigger must update only that column in the base table.
Step 2: Choose the correct trigger approach
INSTEAD OF UPDATE triggers on views let you control exactly what updates happen. Updating only price and returning NEW is correct.
Final Answer:
Create an INSTEAD OF UPDATE trigger that updates only the price column in products and returns NEW -> Option C
Quick Check:
INSTEAD OF triggers control updates precisely [OK]
Hint: INSTEAD OF UPDATE triggers can limit which columns update [OK]