Mysql 触发器执行 INSERT 而不是 UPDATE

发布于 2024-09-16 05:42:44 字数 211 浏览 2 评论 0原文

我想在 MySQL 表中实现一个基本的版本控制系统。让我们想象一个包含 2 列的简单表:名称 (pk) 和价格。我想我只需添加一列“版本”并将其添加到主键中。然后我会捕获所有更新并进行插入,增加版本号。

首先,这可能吗?我可以在 UPDATE 之前创建触发器并执行插入并取消 UPDATE 吗?语法是什么? 第二,这个想法可以吗?你将如何实现这一目标?

感谢您的帮助, 巴特

I would like to implement a basic versioning system in a MySQL table. Let's imagine a simple table with 2 columns: name (pk) and price. I thought I would simply add a column 'version' and add it to the primary key. Then I would catch all the UPDATE's and do an insert instead, incrementing the version number.

First, is this possible ? Can I make a trigger BEFORE UPDATE and do an insert and cancel the UPDATE ? What would be the syntax ?
Second, is this idea ok ? how would you achieve this ?

Thank you for your help,
Barth

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

稳稳的幸福 2024-09-23 05:42:44

您无法取消更新。我会将表的版本与“主”表分开,并在主表更新时将新记录插入到该表中。或者甚至更简单 - 使用带有新版本号的插入而不是没有任何触发器的更新。

You cannot cancel the update. I would keep table with versions separately from the "main" table, and would insert into this table new record when main table gets updated. Or even easier - use insert with new version number instead of update without any triggers.

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