如何确定字段/列是否受 UPDATE 语句影响

发布于 2024-11-05 21:01:57 字数 709 浏览 1 评论 0原文

大家好,感谢您的回答和评论。

我有一个包含多个字段的表,其中包括 versionlast_modifiedmodified_by

我正在编写一个触发器来:

  • 增加 version 每次更新后加 1,
  • last_modified 设置为当前时间戳,
  • 将最新更改的用户 id 设置为 modified_by,并
  • 防止程序员不会忽略/忘记通过引发信号(在这种情况下)在 UPDATE 语句中设置 modified_by = userid

我怎样才能实现这个目标?

我尝试检查是否 isnull(NEW.modified_by),但随后意识到 NEW.modified_byOLD.modified_by 如果不是的话,则采用相同的值不受影响。另外,检查NEW.modified_by是否等于OLD.modified_by并不完全有效,因为它可能是用户修改了自己之前修改过的记录。

有没有办法确定哪些字段受到 UPDATE 语句的影响?或者如果某个特定字段 (modified_by) 受到影响?

Hello everyone and thank you for your answers and comments.

I have a table with several fields, among which are version, last_modified and modified_by

I'm writting a trigger to:

  • increase version by 1 after every/any update,
  • set last_modified to the current timestamp,
  • set the id of the user who made the latest changes into modified_by, and
  • prevent the programmer from ignoring/forgetting to set modified_by = userid in the UPDATE statement by raising a signal (in such case).

How can I achieve this?

I tried checking if isnull(NEW.modified_by), but then realized that NEW.modified_by takes the same value as OLD.modified_by if it wasn't affected. Also, checking if NEW.modified_by equals OLD.modified_by doesn't quite make it, since it could be a user modifiyng a record previously modified by himself.

Is there a way to determine which fields where affected by the UPDATE statement? Or if a particular field (modified_by) was affected?

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

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

发布评论

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

评论(3

草莓酥 2024-11-12 21:01:57

我找不到任何可以让您检查传入信息以查看哪些字段受到影响的内容。虽然我知道您正试图在触发器级别阻止此问题,但明智的做法是要求所有表写入都通过存储过程。这样您就可以需要用户字段。

如果这不可能,我想你可能需要采取一些技巧。例如,您可能要求将 user_id 写入两个字段(为此目的创建一个空白的额外列)。然后,将虚拟列中的 user_id 与您要更新的 user_id 进行比较。一旦确定是否需要修改 user_id,请再次清空虚拟列。它并不漂亮,但它可以完成工作。

I cannot find anything that will allow you to inspect the incoming information to see which fields are being affected. While I know you are trying to stop this issue at the trigger level, it might be prudent to require all table writes to go through a stored procedure. That way you could require the user field.

If that isn't a possibility, I think you might need to get tricky. For example, you could require that the user_id be written to two fields (create an extra column that is blank for this purpose). Then, compare the user_id in the dummy column to the one you are updating. Once you figure out if you need to modify the user_id or not, blank out the dummy column again. It isn't pretty, but it would get the job done.

伊面 2024-11-12 21:01:57

我有类似的问题。我有一个表,其中有一个名为 isDirty 的tinyint 列,并且希望在更新该行时将其设置为 1,并在该行被“清理”时将其清除为 0。

问题是,NEW 和 OLD 与值 0 和 1 的组合并没有给我足够的值来解决我的问题。因此,我制定了一个“规则”,当该列更新为值 100 时,它被设置为干净值“0”,其他任何值都将其设置为“1”。这样做的原因是该列只会有 2 个值之一:0 或 1,因此您可以使用 100(或您选择的任何值)作为“标志”,表明它是干净的。

我知道将其设置为非零值以返回到 0 听起来有点倒退,但这就是我选择的方向并且它有效。该触发器如下所示:

CREATE TRIGGER calls_update BEFORE UPDATE ON `calls`
    FOR EACH ROW
    BEGIN
        IF ( NEW.isDirty = 100 ) THEN
            SET NEW.isDirty = 0;
    ELSE
            SET NEW.isDirty = 1;
        END IF;
    END
$

I had a similar issue. I have a table with a tinyint column named isDirty, and wanted to set it to 1 when the row is updated, and clear it to 0 when the row has been "cleaned".

The problem is, the combination of NEW and OLD with values of 0 and 1 didn't give me enough values to solve my problem. So instead, I made a "rule" that when the column was updated to a value of 100, it was set to the clean value of "0", anything else set it to "1". The reason this works is that this column will only ever have one of 2 values, 0 or 1, so you can use 100 (or any value of your choice) as the "flag" that indicates that it is clean.

I know that it sounds a little backwards to set it to a non-zero value to get back to 0, but that's the direction I chose and it works. Here's what that trigger looks like:

CREATE TRIGGER calls_update BEFORE UPDATE ON `calls`
    FOR EACH ROW
    BEGIN
        IF ( NEW.isDirty = 100 ) THEN
            SET NEW.isDirty = 0;
    ELSE
            SET NEW.isDirty = 1;
        END IF;
    END
$
将军与妓 2024-11-12 21:01:57

据我所知,您唯一的选择是对照旧值检查每列的新值。

SET `ColumnAChanged` = NEW.ColumnA <=> OLD.ColumnA;
SET `ColumnBChanged` = NEW.ColumnB <=> OLD.ColumnB;

As far as I know, your only option is to check each column's NEW value against the OLD.

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