Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watching a table for change in MySQL?

Tags:

Is there a better way to watch for new entries in a table besides selecting from it every n ticks of time or something like that?

I have a table that an external program updates very often, and clients can watch for this new data as it arrive, how can I make that without having to set a fixed period of repeatable select statements?

like image 964
Edwin Jarvis Avatar asked Oct 03 '08 14:10

Edwin Jarvis


People also ask

Does MySQL have change tracking?

MySQL CDC Setup: Using Binary LogsMySQL binary logs provide a very efficient way to track data changes for MySQL CDC. They contain events that describe the modifications to data.

How do I view a specific table in MySQL?

To use the SHOW TABLES command, you need to log on to the MySQL server first. On opening the MySQL Command Line Client, enter your password. Select the specific database. Run the SHOW TABLES command to see all the tables in the database that has been selected.

Is there a MySQL option feature to track history of changes to records?

Create a table called changes . It would contain the same fields as the master table but prefixed with old and new, but only for those fields which were actually changed and a TIMESTAMP for it. It would be indexed with an ID . This way, a SELECT report could be run to show the history of each record.


2 Answers

In MySQL there's no best way than to poll (you create a specific table to simplify the polling though), in other databases you can have triggers that have impact outside the database. In MySQL triggers can only do stuff inside the database itself (for instance, populating the helper table).

like image 177
Vinko Vrsalovic Avatar answered Oct 14 '22 07:10

Vinko Vrsalovic


Another similar approach would be to add

add column Last_Modified TIMESTAMP ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP  

to each table and preface your select queries to compare the last request date/time with the max(Last_Modified).

Databases are typically pull sources and not push so you'll still need to programmatically probe for changes no matter what.

like image 26
Rob Allen Avatar answered Oct 14 '22 08:10

Rob Allen