MySQL触发器:插入前或插入后 [英] MySQL Trigger: Before Insert or After Insert
问题描述
我正在处理一些用于计费的数据库项目(PHP/MySQL).
I am working on some database project (PHP/MySQL) used for billing.
无论何时创建新的帐单,我都想生成一个由年,周和增量号组成的帐单号.我想通过触发器来做到这一点.触发器将使用现有的帐单编号来查找增量编号,或者以新的一周和/或新年的第一个帐单的新增量开始.
Whenever a new bill is created I want to generate a bill number consisting of year, week and increment number. I would like to do this with a trigger. The trigger will use the existing billnumbers to find the increment number or start with a fresh increment for the first bill n a new week and/or new year.
除了生成帐单号之外,我还可以执行 BEFORE INSERT
触发器并将 NEW.billnumber
设置为新生成的帐单号.还可以执行 AFTER INSERT
并使用生成的帐单号更新记录.
Apart from generating the bill number, I can do a BEFORE INSERT
trigger and set the NEW.billnumber
to the newly generated billnumber. It is also possible to do an AFTER INSERT
and update the record with the generated billnumber.
我的问题是我应该选择哪一个.在插入之前
还是在插入之后
?我确实进行了搜索,但是在何时使用BEFORE或AFTER之后找不到很好的论据.
My question is which one should I choose. BEFORE INSERT
or AFTER INSERT
? I did search for this, but I can't find a good argumentation when to use BEFORE or AFTER.
推荐答案
发现使用 BEFORE INSERT
可以完成此操作,只是因为MySQL不允许操作触发的表插入后
触发器.
Found out that it can be done with BEFORE INSERT
only because MySQL does not allow manipulation of the table that triggered the AFTER INSERT
trigger.
这篇关于MySQL触发器:插入前或插入后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!