Q1
True / FalseA trigger in PostgreSQL can automatically execute a specified function when a certain event occurs.
A trigger in PostgreSQL is designed to automatically execute a specified function when a certain event (such as INSERT, UPDATE, or DELETE) occurs on a table.
Q2
True / FalseTriggers in PostgreSQL are created using the CREATE TRIGGER command.
The CREATE TRIGGER command is used to create triggers in PostgreSQL.
Q3
True / FalsePostgreSQL triggers cannot be used to enforce business rules.
Triggers in PostgreSQL can indeed be used to enforce business rules by executing custom functions in response to specific events.
Q4
True / FalseA trigger function in PostgreSQL must return a TRIGGER data type.
A trigger function in PostgreSQL must return the TRIGGER data type to indicate that it is intended to be used as a trigger.
Q5
True / FalseTriggers in PostgreSQL can only be fired before an event occurs.
Triggers in PostgreSQL can be fired before or after an event occurs, depending on how they are defined.
Q6
True / FalseIt is possible to create row-level and statement-level triggers in PostgreSQL.
PostgreSQL supports both row-level triggers, which execute for each row affected by an event, and statement-level triggers, which execute once for the entire event.
Q7
True / FalsePostgreSQL allows creating triggers that fire on TRUNCATE statements.
PostgreSQL supports triggers that can fire on TRUNCATE statements, allowing actions to be taken when a table is truncated.
Q8
True / FalseYou can use the ORACLE-specific syntax for creating triggers in PostgreSQL without modification.
PostgreSQL and ORACLE have different syntax for creating triggers. The ORACLE-specific syntax cannot be used directly in PostgreSQL without modification.
Q9
True / FalseIn PostgreSQL, triggers can be deferred to execute at the end of the transaction.
PostgreSQL supports deferred triggers, which can be set to execute at the end of the transaction rather than immediately after the triggering event.
Q10
True / FalseMigrating triggers from ORACLE to PostgreSQL typically involves rewriting the trigger function and adjusting the trigger definition syntax.
When migrating triggers from ORACLE to PostgreSQL, the trigger function often needs to be rewritten to match PostgreSQL's PL/pgSQL syntax, and the trigger definition syntax may also need adjustment to align with PostgreSQL's requirements.
Q27
Multiple ChoiceWhat is the difference between a row-level and a statement-level trigger in PostgreSQL?
SQL Code
CREATE OR REPLACE FUNCTION log_row_update()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO status_log(order_id, old_status, new_status, log_date)
VALUES (OLD.id, OLD.order_status, NEW.order_status, NOW());
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER log_order_update
AFTER UPDATE ON orders
FOR EACH ROW
WHEN (OLD.order_status IS DISTINCT FROM NEW.order_status)
EXECUTE FUNCTION log_row_update();
CREATE OR REPLACE FUNCTION log_bulk_update()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO bulk_log(action, log_date)
VALUES ('Bulk order update', NOW());
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER log_bulk_order_update
AFTER UPDATE ON orders
FOR EACH STATEMENT
EXECUTE FUNCTION log_bulk_update();
A row-level trigger executes once for each row affected by the triggering event, while a statement-level trigger executes once per triggering statement, regardless of how many rows are affected. Row-level triggers are useful for auditing changes to individual rows, whereas statement-level triggers are suitable for logging actions or enforcing constraints at a higher level.
Q29
Multiple ChoiceCan a trigger in PostgreSQL call another trigger?
SQL Code
CREATE OR REPLACE FUNCTION update_summary()
RETURNS TRIGGER AS $$
BEGIN
UPDATE order_summary SET total_orders = total_orders + 1 WHERE customer_id = NEW.customer_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_order_summary
AFTER UPDATE ON orders
FOR EACH ROW
EXECUTE FUNCTION update_summary();
CREATE OR REPLACE FUNCTION log_order_update()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO order_log(order_id, log_date)
VALUES (NEW.id, NOW());
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER log_order_update
AFTER UPDATE ON orders
FOR EACH ROW
EXECUTE FUNCTION log_order_update();
Yes, in PostgreSQL, one trigger can call another trigger. This is possible because triggers can perform operations that, in turn, trigger other triggers. However, care must be taken to avoid circular references or infinite loops.