0

I am looking for a way to create a trigger after any changes occur in a table on any row or field.

I want for my web app to automatically refresh if they're have been any changes to the data since it was last loaded. For this I need a "modified_on" attribute for a table which will apply to the whole table, not just a row.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Blease
  • 1,380
  • 4
  • 38
  • 64
  • What is preventing you to do what you described? – Jocelyn Sep 11 '15 at 18:16
  • possible duplicate of [MySQL Trigger after update only if row has changed](http://stackoverflow.com/questions/6296313/mysql-trigger-after-update-only-if-row-has-changed) – Norbert Sep 11 '15 at 18:17

1 Answers1

0

Not sure what database triggers have to do with this problem, as they are not going to be able to trigger any behavior at the web application level. You will need to build logic in your web application to inspect the data looking for a change. Most likely, this would take the form of some some-client triggered refresh process (i.e. AJAX), which would need to call a application script that would take information from the client on when it last checked for an update and compare it to the most recently updated row(s) in the table. As long as you have a timestamp/datetime field on the table and update each row when it is updated, you can retrieve all updated rows via a simple query such as

SELECT {fields} FROM {table}
WHERE {timestamp field} > '{last time checked}'

I you want, you could use this to only update those rows in the application view which need updating rather than re-rendering the whole table (this would minimize response bandwidth/download time, rendering time, etc.). If you simply want to check if the table has been updated from some certain, but don't care about individual rows, you can just check that the above query returns 1 or more rows.

If you don't want the client application view to have to check at regular intervals (as would likely be done with AJAX), you might also consider websockets or similar to enable bi-directional client-server communication, but this still wouldn't change the fact that your server-side application would need to query the database to look for changed records.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • "*database triggers ... are not going to be able to trigger any behavior at the web application level*"—not strictly true, since one could use a UDF like [lib_mysqludf_sys](https://github.com/mysqludf/lib_mysqludf_sys#readme), but that's such a horrible idea that I do agree with the general sentiment. – eggyal Sep 11 '15 at 20:11
  • @eggyal Agreed. It is technically possible, but is not a configuration I would ever recommend running, just from a security standpoint alone. Good link for OP to read - https://patternbuffer.wordpress.com/2012/09/14/triggering-shell-script-from-mysql/ – Mike Brant Sep 11 '15 at 20:15