E
E
ebanytiu_lis2017-09-21 15:54:54
MySQL
ebanytiu_lis, 2017-09-21 15:54:54

Triggers in MySQl, PostgreSQL?

I can’t understand triggers at all: everything is muddy in Google.
There is a table with columns: id, balance, refer. It is necessary, for example, when requesting

UPDATE talbe SET balance = balance + 100 WHERE id = 1
, execute also the request ( in the request: 50 = 100 from the first request / 2 )
UPDATE talbe SET balance = balance + 50 WHERE id = РЕФЕР 1-го
. This whole thing can be implemented through triggers, but how?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Fortop, 2017-09-21
@Fortop

It is possible, but if it is cloudy for you, then it is better not to do it.
Execute these two queries from code.
Because with this logic of work, you may have cyclic execution of triggers depending on the correctness of your database data and the code of the triggers themselves
. Problem:
https://stackoverflow.com/questions/10699673/a-tri...
One of the options for solving it:
https://stackoverflow.com/questions/9570204/how-to...

R
Rsa97, 2017-09-21
@Rsa97

EMNIP, in the trigger it is impossible to change the table on which the trigger works. So either in the client or write a stored procedure.

V
Vyacheslav Uspensky, 2017-09-21
@Kwisatz

First, you get an infinite loop.
Secondly, if you need this, I advise you to seriously think about the architecture, such a task should not have arisen.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question