MySQL 是否可以在 select 语句上使用触发器?
我知道触发器可用于插入、更新和删除,但是 select 语句上的触发器(或某种触发器)又如何呢?我想使用触发器在B表上插入数据,当它选择A表上的现有记录时,这可能吗?
提前致谢。
I know that triggers can be used on insert, update and delete, but what about a trigger (or sort of) on a select statement. I want to use a trigger to insert data on a table B when it is selected an existent record on a table A, it could be possible?.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不完全是一个触发器,但你可以:
然后,
但这不是一个优雅的解决方案。
Not exactly a trigger, but you can:
And then
Not an elegant solution, though.
这在数据库本身中是不可能的。
然而,有一些针对数据库的监控/检测产品(例如 Sybase - 不确定 MySQL),它们跟踪服务器执行的每个查询,并可以基于该查询执行任何操作 - 通常将查询日志存储到数据仓库中以供以后分析,但是我猜他们也可以为你在表 B 中插入一条记录。
It is not possible in the database itself.
However there are monitoring/instrumentation products for databases (e.g. for Sybase - not sure about MySQL) which track every query executed by the server, and can do anything based on that - usually store the query log into a data warehouse for later analysis, but they can just as well insert a record into table B for you, I would guess.
您应该设计您的应用程序,以便仅通过某些方法进行数据库访问,并在这些方法中添加您需要的监视。
You should design your application so that database access occurs only through certain methods, and in those methods, add the monitoring you need.
您可以编写一个应用程序来监视查询日志并在发生选择时执行某些操作。不过,解决问题的一种非常粗略的方法......
You can write an application which will be monitoring the query log and doing something when a select occurs. A pretty crude way to solve the problem though...