MySQL 是否可以在 select 语句上使用触发器?

发布于 2024-08-31 06:54:46 字数 104 浏览 1 评论 0原文

我知道触发器可用于插入、更新和删除,但是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

养猫人 2024-09-07 06:54:46

不完全是一个触发器,但你可以:

CREATE FUNCTION myFunc(...) BEGIN INSERT INTO myTable VALUES(...) END;

然后,

SELECT myFunc(...), ... FROM otherTable WHERE id = 1;

但这不是一个优雅的解决方案。

Not exactly a trigger, but you can:

CREATE FUNCTION myFunc(...) BEGIN INSERT INTO myTable VALUES(...) END;

And then

SELECT myFunc(...), ... FROM otherTable WHERE id = 1;

Not an elegant solution, though.

昵称有卵用 2024-09-07 06:54:46

这在数据库本身中是不可能的。

然而,有一些针对数据库的监控/检测产品(例如 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.

调妓 2024-09-07 06:54:46

您应该设计您的应用程序,以便仅通过某些方法进行数据库访问,并在这些方法中添加您需要的监视。

You should design your application so that database access occurs only through certain methods, and in those methods, add the monitoring you need.

失而复得 2024-09-07 06:54:46

您可以编写一个应用程序来监视查询日志并在发生选择时执行某些操作。不过,解决问题的一种非常粗略的方法......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文