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 does the NEW keyword represent in a PostgreSQL trigger?
NEW holds the new row data for INSERT or UPDATE operations inside a trigger function.
Click to reveal answer
beginner
What does the OLD keyword represent in a PostgreSQL trigger?
OLD holds the old row data for UPDATE or DELETE operations inside a trigger function.
Click to reveal answer
beginner
Can you use NEW in a DELETE trigger?
No. NEW is not available in DELETE triggers because no new row is created.
Click to reveal answer
beginner
In which trigger events is OLD available?
OLD is available in UPDATE and DELETE triggers because the old row exists before the change.
Click to reveal answer
beginner
How do you access the NEW record's column named price inside a trigger function?
Use NEW.price to get the value of the price column in the new row.
Click to reveal answer
Which keyword gives you the old row data in a PostgreSQL trigger?
ANEW
BOLD
CCURRENT
DPREVIOUS
✗ Incorrect
OLD holds the old row data before an update or delete.
In which trigger event can you use NEW?
ANONE
BDELETE
CTRUNCATE
DUPDATE
✗ Incorrect
NEW is available in INSERT and UPDATE triggers, not DELETE.
What happens if you try to use NEW in a DELETE trigger?
AIt causes an error
BIt returns the deleted row
CIt returns NULL
DIt returns the new row
✗ Incorrect
NEW is not defined in DELETE triggers and causes an error if accessed.
Which of these is true about OLD in triggers?
AAvailable in UPDATE and DELETE triggers
BAvailable in INSERT and UPDATE triggers
CAvailable only in INSERT triggers
DNever available
✗ Incorrect
OLD is available in UPDATE and DELETE triggers.
How do you refer to the name column of the new row in a trigger?
AOLD.name
BCURRENT.name
CNEW.name
DROW.name
✗ Incorrect
NEW.name accesses the name column of the new row.
Explain the difference between NEW and OLD in PostgreSQL triggers.
Think about when rows are created, changed, or deleted.
You got /4 concepts.
Describe when you can access NEW and when you can access OLD in trigger functions.
Consider the lifecycle of a row during different operations.
You got /4 concepts.
Practice
(1/5)
1. In a PostgreSQL trigger function, which record variable would you use to access the new row data after an INSERT operation?
easy
A. PREVIOUS
B. NEW
C. CURRENT
D. OLD
Solution
Step 1: Understand trigger timing for INSERT
For an INSERT operation, the new row is being added, so the trigger can access the new data using the NEW record.
Step 2: Identify correct record variable
The OLD record is not available for INSERT because there is no previous row. Therefore, NEW is used to access the inserted row.
Final Answer:
NEW -> Option B
Quick Check:
INSERT uses NEW = D [OK]
Hint: Use NEW for inserted or updated rows, OLD for deleted or old rows [OK]
Common Mistakes:
Using OLD in INSERT triggers
Confusing NEW and OLD for UPDATE
Assuming CURRENT or PREVIOUS exist
2. Which of the following is the correct syntax to access the old value of a column named price inside a DELETE trigger in PostgreSQL?
easy
A. OLD.price
B. NEW.price
C. OLD->price
D. NEW->price
Solution
Step 1: Identify record variable for DELETE
In a DELETE trigger, the row is being removed, so the old data is accessible via OLD.
Step 2: Use correct syntax for column access
PostgreSQL uses dot notation to access columns in record variables, so OLD.price is correct.
Final Answer:
OLD.price -> Option A
Quick Check:
DELETE uses OLD.column = A [OK]
Hint: Use dot notation with OLD for deleted row columns [OK]
Common Mistakes:
Using NEW in DELETE triggers
Using arrow (->) instead of dot for record access
Confusing syntax for JSON operators
3. Consider this trigger function snippet for an UPDATE operation:
IF NEW.quantity < OLD.quantity THEN
RAISE NOTICE 'Quantity decreased from % to %', OLD.quantity, NEW.quantity;
END IF;
What will be the output if the old quantity was 10 and the new quantity is 7?
medium
A. Quantity decreased from 10 to 7
B. Quantity decreased from 7 to 10
C. No output
D. Syntax error
Solution
Step 1: Understand the condition in the IF statement
The condition checks if the new quantity is less than the old quantity. Here, 7 < 10 is true.
Step 2: Analyze the RAISE NOTICE output
The message prints the old quantity first, then the new quantity, so it will output: 'Quantity decreased from 10 to 7'.
Final Answer:
Quantity decreased from 10 to 7 -> Option A
Quick Check:
NEW < OLD triggers notice = A [OK]
Hint: Compare NEW and OLD values carefully in UPDATE triggers [OK]
Common Mistakes:
Mixing up NEW and OLD values in output
Assuming no output when condition is true
Confusing syntax of RAISE NOTICE
4. You wrote this trigger function for DELETE:
CREATE FUNCTION trg_delete_check() RETURNS trigger AS $$
BEGIN
IF NEW.id IS NULL THEN
RAISE EXCEPTION 'ID cannot be null';
END IF;
RETURN OLD;
END;
$$ LANGUAGE plpgsql;
What is the error in this function?
medium
A. RAISE EXCEPTION syntax is wrong
B. RETURN OLD is invalid in DELETE triggers
C. Using NEW in a DELETE trigger where only OLD is available
D. Function must return VOID, not trigger
Solution
Step 1: Check record variables in DELETE triggers
In DELETE triggers, the NEW record is not available because no new row is inserted or updated.
Step 2: Identify incorrect usage of NEW
The function incorrectly uses NEW.id, which causes an error. It should use OLD.id instead.
Final Answer:
Using NEW in a DELETE trigger where only OLD is available -> Option C
Quick Check:
DELETE triggers have OLD, not NEW = C [OK]
Hint: Use OLD in DELETE triggers; NEW is unavailable [OK]
Common Mistakes:
Using NEW in DELETE triggers
Returning OLD incorrectly
Misunderstanding trigger return types
5. You want to create a trigger that logs changes to a salary column only when the salary is updated to a higher value. Which trigger condition and record access correctly implements this in PostgreSQL?
hard
A. IF OLD.salary > NEW.salary THEN INSERT INTO log_table VALUES (OLD.id, OLD.salary); END IF;
B. IF NEW.salary < OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF;
C. IF NEW.salary = OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF;
D. IF NEW.salary > OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF;
Solution
Step 1: Understand the condition for logging
The trigger should log only when the new salary is greater than the old salary, so the condition is NEW.salary > OLD.salary.
Step 2: Use correct record variables for UPDATE
The new salary and id come from NEW because the row is updated with new values.
Final Answer:
IF NEW.salary > OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF; -> Option D
Quick Check:
Log when NEW > OLD salary = B [OK]
Hint: Compare NEW and OLD to detect increases, then log NEW data [OK]