Junior — Middle
Have you had experience using triggers in database management systems?
sobes.tech AI
Answer from AI
Triggers in DBMS are special procedures that automatically execute upon certain events (e.g., inserting, updating, or deleting data). They are useful for automating data integrity checks, auditing, or implementing business logic at the database level.
An example of using a trigger in PostgreSQL to automatically update the "updated_at" field when a record changes:
CREATE TRIGGER update_timestamp
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Function 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;
In Python, triggers are usually created and managed at the DBMS level, but ORM tools (like SQLAlchemy) can be used to work with them.