MySQL callback - is there such a thing?

I was wondering if there is such a thing as the equivalent of a callback function using mysql after INSERT or UPDATE, which can return me the line # and possibly the values ​​of such lines.

+6
source share
3 answers

I am not aware of any callback as you say, but, of course, from your calling application you can get the last inserted identifier if you did not specify it, and db generated an auto-increment value. Other values ​​you need to know because you inserted them.

if you need to know these values ​​on the database server, you may have an SQL trigger that runs each time you insert, so you can do more processing in the newly inserted record, for example, write something in another table, etc. ..

+4
source

You can create triggers that are called upon insert and update. They do not return a value, but they can set variables that you can read outside of them.

+7
source

In MySQL, it is not available. I think there are two ways to do this:

  1. You will need to simulate it using a poll - a rather ugly method, easy to program, but hard on the server.
  2. See Does MySQL Allow Callbacks in C for an Error to Occur When Changing? - write a user-defined function that could notify a registered listener using any of your own creation methods. It’s harder to program, just on the server. In this case, make sure that your UDF is reliable, does not freeze when transmitting a notification to a listener who may have died, etc.
This will be a convenient universal UDF for MySQL and will bring him something like this, he was constantly absent compared to Firebird / Interbase, which they call events, and not to be confused with events related to MySQL. On the other hand, the MySQL time-based event function is something that Firebird and Interbase did not have the last time I looked at them.

See this for a description of the Firebird event in case you (or some later reader) decide to do it through UDF - this is a good design specification aimed at: http://www.janus-software.com/fbmanual/manual. php? book = php & topic = 49

+2
source

Source: https://habr.com/ru/post/896072/


All Articles