表的触发器可以应用于同一个表中的跟踪吗?
表的触发器可以应用于同一个表中的跟踪吗?
也就是说,如果我有表说“employee”,现在我可以创建一个触发器,以便它跟踪同一“employee”表
触发器中的更改,例如
create trigger "<triggers_name>" before insert on employee
For each row
Begin
insert into employee field = timestamp;
end
Can a trigger for a table can be applied for tracking in same table?
That is IF I have table Say "employee" Now can i create a triggers such that it tracks the change in the same "employee" table
trigger such as
create trigger "<triggers_name>" before insert on employee
For each row
Begin
insert into employee field = timestamp;
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简而言之,不。
那么让我们考虑一下这个问题。
“之前”触发器将在操作(在本例中为“插入”操作)发生之前执行。
如果这可行,并且每次在员工表上进行插入时都尝试向员工表中进行插入,那么您将在一个大的循环中永远循环递归混乱。
参考MySQL文档
请参阅此页面< 。
也就是说,如果您需要在插入时进行跟踪,我建议您使用第二个表,该表当然可以通过触发器引用,并为员工表创建一个 FK 并放置您的时间戳 该新表中的列并执行如下操作:
In short, no.
So let's think about this.
A "before" trigger will execute before the action (in this case, an "insert" action) takes place.
If this were to work, and you attempted to do an insert into the employee table every time an insert took place on the employee table, you'd loop forever in a big recursive mess.
Referenced from the MySQL docs
See this page for more info.
That said, if you need to do tracking when inserts take place, I would recommend using a second table which can certainly be referenced via a trigger. You and create a FK to the employee table and put your timestamp column in that new table and do something like this:
如果您只想跟踪插入时间,可以将日期字段默认为 NOW()。
我想知道是否可以对刚刚插入的表进行更新?我理解第二次插入的递归,但也许您可以更新同一个表?
If you just wanted to track the insert time, you can have your datefield default to NOW().
I wonder if you can do an update on a table that was just inserted? The recursion I understand with the second insert but maybe you can update the same table?