Sobes.tech
Junior — Middle

Do you have experience creating and using triggers in SQL databases?

sobes.tech AI

Answer from AI

Yes, I have experience creating and using triggers in SQL databases. Triggers are special procedures that automatically execute in response to certain events in the database, such as insert (INSERT), update (UPDATE), or delete (DELETE) operations.

Examples of trigger usage:

  • Automatic update of the "updated_at" field when a record changes.
  • Logging changes into a separate table.

An example of a trigger in PostgreSQL that updates the updated_at field:

CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
   NEW.updated_at = NOW();
   RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER update_timestamp
BEFORE UPDATE ON your_table
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();

In the context of Golang, I used triggers to ensure data integrity and automate some business rules at the database level.