WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PostgreSQL · Basic · question 20 of 100

What are triggers in PostgreSQL, and what are some common use cases for them?

📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

In PostgreSQL, a trigger is a set of instructions that can be automatically executed by the database when certain events occur on a table, view or schema. The events supported by PostgreSQL triggers are:

- BEFORE INSERT

- AFTER INSERT

- BEFORE UPDATE

- AFTER UPDATE

- BEFORE DELETE

- AFTER DELETE

When any of these events occur on a table, view or schema, the corresponding trigger is executed, allowing the execution of additional operations alongside the original query.

For example, let’s say that we have a table ‘employees‘ that includes columns for ‘name‘, ‘salary‘, and ‘hire_date‘. We can define a trigger on the ‘employees‘ table that will automatically update the ‘hire_date‘ column whenever a new employee is inserted. Here’s an example:

CREATE TRIGGER update_hire_date
BEFORE INSERT ON employees
FOR EACH ROW
EXECUTE FUNCTION update_hire_date_function();

In this example, we’ve defined a trigger named ‘update_hire_date‘ that will execute a function called ‘update_hire_date_function()‘ before every ‘INSERT‘ operation on the ‘employees‘ table. Inside the function, we can update the ‘hire_date‘ column to the current date and time, like this:

CREATE FUNCTION update_hire_date_function()
RETURNS TRIGGER AS $$
BEGIN
  NEW.hire_date := NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

This example trigger will ensure that the ‘hire_date‘ column is automatically updated whenever a new employee is added to the ‘employees‘ table.

Some common use cases for PostgreSQL triggers include:

- Enforcing data integrity: Triggers can be used to validate data before it is inserted, updated or deleted from a table, ensuring that data remains consistent and correct.

- Auditing changes: Triggers can be used to track changes to tables, views or schemas, allowing administrators to monitor the history of changes and identify potential security risks.

- Synchronizing data: Triggers can be used to update data across multiple tables or databases, ensuring that data remains consistent across the enterprise.

- Implementing business rules: Triggers can be used to enforce business rules and processes, for example, by automatically sending notifications or alerts when data meets certain criteria.

Overall, triggers are a powerful and flexible tool that can be used to implement a wide range of functionality in PostgreSQL databases.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PostgreSQL interview — then scores it.
📞 Practice PostgreSQL — free 15 min
📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

All 100 PostgreSQL questions · All topics