我是SQL / SQLite的新手,我正在尝试在我的数据库中设置触发器。基本上,有一个表包含人员的姓名和ID,另一个表包含这些人的条目。这是我的表的架构:
CREATE TABLE people(_id INTEGER PRIMARY KEY AUTOINCREMENT, name STRING NOT NULL,
amount_people INTEGER);
CREATE TABLE entries (_id INTEGER PRIMARY KEY AUTOINCREMENT,
user INTEGER NOT NULL, date STRING NOT NULL,
title STRING NOT NULL, amount_entry INTEGER NOT NULL,
notes STRING, FOREIGN KEY (user) REFERENCES people (_id) ON DELETE CASCADE);
我想构建一个触发器,每次在条目表中为该用户创建新条目时,该触发器都会更新people表中用户的余额。这似乎应该很容易,但我花了最后2个小时试图实现这一目标,但没有成功。
这是我的错误版本的触发器:
CREATE TRIGGER update_total_balance AFTER INSERT ON entries
BEGIN
UPDATE people SET amount_people = amount_people + (amount_entry FROM entries)
WHERE _id = (user FROM entries);
END;
答案 0 :(得分:0)
未经测试...
CREATE TRIGGER update_total_balance AFTER INSERT ON entries
BEGIN
UPDATE people SET amount_people = amount_people + new.amount_entry
WHERE _id = new.user;
END;