mysql - sqlite 触发器对整个数据库起作用还是仅对更改的行起作用

标签 mysql sql sqlite triggers

我需要将 sqlite 触发器转换为 mysql 触发器。我已经转换了以下 sqlite 触发器

create trigger arpinsert after insert on arp
begin
    update arp set timeout = datetime('now', '60 seconds') where rowid = new.rowid and new.mac != '00:00:00:00:00:00';
    update arp set timeout = datetime('now')               where rowid = new.rowid and new.mac  = '00:00:00:00:00:00';
end;

进入下面的mysql触发器

delimiter //
create
    trigger arpBeforeInsert
    before insert
    on arp for each row
        begin
            if new.mac != '00:00:00:00:00:00' then
                set new.timeout = date_add(now(), interval 60 second);
            else
                set new.timeout = now();
            end if;
        end;//
delimiter ;

我知道mysql触发器只触发受影响的行。 sqlite 触发器也是如此吗?如果我删除 where rowid = new.rowid SQLite 触发器会更新整个表吗?

最佳答案

SQLite documentation说:

At this time SQLite supports only FOR EACH ROW triggers, not FOR EACH STATEMENT triggers. Hence explicitly specifying FOR EACH ROW is optional. FOR EACH ROW implies that the SQL statements specified in the trigger may be executed (depending on the WHEN clause) for each database row being inserted, updated or deleted by the statement causing the trigger to fire.

但是,这仅适用于触发器本身。 触发器内的任何 SQL 语句都是独立的(如果要访问其他表/行,则必须如此)。因此,执行不带 WHERE 子句的 UPDATE 将更新表的所有行,就像触发器外部一样。

关于mysql - sqlite 触发器对整个数据库起作用还是仅对更改的行起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31565979/

相关文章:

mysql - 如何在 Java 中使用 hibernate 在 MySQL 数据库中创建连接表?

php - 为什么这个数据库连接这么慢?

sql - 将一个 SQL 字符串拆分为多个字符串

mysql - 时间相关查询

火狐扩展开发和sqlite

mysql - 如何创建非强制加入?

php + mysql插入错误

mysql - 为什么我的查询出现 SQL_SAFE_UPDATE 错误?

database - 如何在 Mac OS X 应用程序中播种数据库

SQLite 并以 UTC 格式插入当前日期