Recently I was working on a car maintenance database. The database stores, among other things, odometer reading, miles per fill up, number of gallons per fill up, and cost per gallon. One of the missing pieces of information I wanted to report on is the miles per gallon I was achieving. While this is trivial to insert, I didn’t want to modify the client which was already deployed and running. Since the data to determine this value was already present, I turned to database triggers to help me record this value.
Wikipedia describes “A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database.”
In our case, the procedural code we want to execute is the computation of our miles per gallon, and the event we are responding to is an insert into our table. In this article I will outline how to create these triggers in both MySQL and PostgreSQL.
Continue reading →