PostgreSQL Database Quiz Questions

Course Name:PostgreSQL
Chapter Name:Chapter 9 - Advanced Topics
Lesson Content Link:DB Trigger
Current Quiz Count:30
Progress
0%
Q1
True / False

A trigger in PostgreSQL can automatically execute a specified function when a certain event occurs.

Q2
True / False

Triggers in PostgreSQL are created using the CREATE TRIGGER command.

Q3
True / False

PostgreSQL triggers cannot be used to enforce business rules.

Q4
True / False

A trigger function in PostgreSQL must return a TRIGGER data type.

Q5
True / False

Triggers in PostgreSQL can only be fired before an event occurs.

Q6
True / False

It is possible to create row-level and statement-level triggers in PostgreSQL.

Q7
True / False

PostgreSQL allows creating triggers that fire on TRUNCATE statements.

Q8
True / False

You can use the ORACLE-specific syntax for creating triggers in PostgreSQL without modification.

Q9
True / False

In PostgreSQL, triggers can be deferred to execute at the end of the transaction.

Q10
True / False

Migrating triggers from ORACLE to PostgreSQL typically involves rewriting the trigger function and adjusting the trigger definition syntax.

Q11
Single Choice

Which command is used to create a trigger in PostgreSQL?

Q12
Single Choice

In PostgreSQL, when can a trigger be fired?

Q13
Single Choice

What is the default timing for a trigger in PostgreSQL if the timing is not specified?

Q14
Single Choice

Which of the following is a valid SQL syntax to create a trigger in PostgreSQL that logs updates to a table called employee?

SQL Code
A) CREATE TRIGGER log_update
 AFTER UPDATE ON employee
 FOR EACH ROW
 EXECUTE PROCEDURE log_changes();
B) CREATE TRIGGER log_update
 AFTER UPDATE ON employee
 FOR EACH ROW
 CALL log_changes();
C) CREATE TRIGGER log_update
 BEFORE UPDATE ON employee
 FOR EACH ROW
 EXECUTE FUNCTION log_changes();
D) CREATE TRIGGER log_update
 BEFORE UPDATE ON employee
 FOR EACH ROW
 CALL FUNCTION log_changes();
Q15
Single Choice

Which special variable is available in a PostgreSQL trigger function to access the row being inserted or updated?

Q16
Single Choice

How would you disable an existing trigger named audit_trigger on the sales table in PostgreSQL?

Q17
Single Choice

Which event will not fire a trigger that is defined as AFTER INSERT OR UPDATE ON orders in PostgreSQL?

Q18
Single Choice

Consider the following trigger creation statement. What is the purpose of this trigger?

SQL Code
CREATE TRIGGER check_stock
BEFORE UPDATE ON inventory
FOR EACH ROW
WHEN (NEW.stock < 0)
EXECUTE FUNCTION raise_exception();
Q19
Single Choice

Which of the following statements correctly creates a trigger in PostgreSQL that executes a function before a row in the orders table is deleted?

Q20
Single Choice

In PostgreSQL, how can you ensure that a trigger only fires for specific columns, such as salary, in the employees table during an UPDATE?

Q21
Multiple Choice

What is a database trigger in PostgreSQL?

SQL Code
CREATE OR REPLACE FUNCTION update_last_modified_column()
RETURNS TRIGGER AS $$
BEGIN
 NEW.last_modified := NOW();
 RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_last_modified
BEFORE UPDATE ON employees
FOR EACH ROW
EXECUTE FUNCTION update_last_modified_column();
Q22
Multiple Choice

When would you use an AFTER INSERT trigger in PostgreSQL?

SQL Code
CREATE OR REPLACE FUNCTION log_employee_insert()
RETURNS TRIGGER AS $$
BEGIN
 INSERT INTO employee_audit(employee_id, action, action_date)
 VALUES (NEW.id, 'INSERT', NOW());
 RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER log_new_employee
AFTER INSERT ON employees
FOR EACH ROW
EXECUTE FUNCTION log_employee_insert();
Q23
Multiple Choice

What are the limitations of triggers in PostgreSQL?

SQL Code
CREATE OR REPLACE FUNCTION check_salary_increase()
RETURNS TRIGGER AS $$
BEGIN
 IF NEW.salary < OLD.salary THEN
 RAISE EXCEPTION 'Salary cannot be decreased';
 END IF;
 RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER prevent_salary_update
BEFORE UPDATE ON employees
FOR EACH ROW
EXECUTE FUNCTION check_salary_increase();
Q24
Multiple Choice

How can you create a trigger that fires only once per statement in PostgreSQL?

SQL Code
CREATE OR REPLACE FUNCTION audit_changes()
RETURNS TRIGGER AS $$
BEGIN
 INSERT INTO audit_log(table_name, action, action_date)
 VALUES ('employees', TG_OP, NOW());
 RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER audit_employee_changes
AFTER UPDATE ON employees
FOR EACH STATEMENT
EXECUTE FUNCTION audit_changes();
Q25
Multiple Choice

What is the purpose of a BEFORE DELETE trigger in PostgreSQL?

SQL Code
CREATE OR REPLACE FUNCTION check_active_projects()
RETURNS TRIGGER AS $$
BEGIN
 IF EXISTS (SELECT 1 FROM projects WHERE employee_id = OLD.id AND status = 'active') THEN
 RAISE EXCEPTION 'Cannot delete employee with active projects';
 END IF;
 RETURN OLD;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER check_employee_termination
BEFORE DELETE ON employees
FOR EACH ROW
EXECUTE FUNCTION check_active_projects();
Q26
Multiple Choice

Can triggers in PostgreSQL be used to enforce referential integrity?

SQL Code
CREATE OR REPLACE FUNCTION check_customer_exists()
RETURNS TRIGGER AS $$
BEGIN
 IF NOT EXISTS (SELECT 1 FROM customers WHERE id = NEW.customer_id) THEN
 RAISE EXCEPTION 'Customer does not exist';
 END IF;
 RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER enforce_foreign_key
BEFORE INSERT ON orders
FOR EACH ROW
EXECUTE FUNCTION check_customer_exists();
Q27
Multiple Choice

What 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();
Q28
Multiple Choice

How can you disable a trigger in PostgreSQL temporarily?

SQL Code
ALTER TABLE employees DISABLE TRIGGER audit_employee_changes;
-- Perform necessary operations without the trigger
ALTER TABLE employees ENABLE TRIGGER audit_employee_changes;
Q29
Multiple Choice

Can 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();
Q30
Multiple Choice

What is the purpose of a BEFORE INSERT trigger in PostgreSQL?

SQL Code
CREATE OR REPLACE FUNCTION enforce_order_limit()
RETURNS TRIGGER AS $$
DECLARE
 order_count INT;
BEGIN
 SELECT COUNT(*) INTO order_count FROM orders WHERE customer_id = NEW.customer_id;
 IF order_count >= 10 THEN
 RAISE EXCEPTION 'Order limit exceeded';
 END IF;
 RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER check_order_limit
BEFORE INSERT ON orders
FOR EACH ROW
EXECUTE FUNCTION enforce_order_limit();