如何根据其他 table 中的数据在 table 上创建 SQLite 触发器?
How do I create an SQLite trigger on a table based on data in other tables?
我有两个表 -
part(partID, brand, price, size)
consists_of(customerID, partID, quantity, price, shipdate)
|
FK part.ID from part
Table part
永远不会是 changed/updated,但 consists_of
会。
- How do I add a [before insert?] trigger on
consists_of
that checks if consists_of.price
for each entry is less than or equal consists_of.quantity * part.price
for that particular consists_of.partID
and raises an abort if it isn't so?
或者,
- How do I add [after insert?] trigger on
consists_of
that does INSERT INTO consists_of(price) VALUES (...)
where the value of consists_of.price
is equal to consists_of.quantity * part.price
for that consists_of.partID
?
如果我没理解错的话,你可以在子查询中 select part.price
并计算 part.price * consists_of.quantitiy
.
- 插入前
CREATE TABLE part(part_id INTEGER, price INTEGER);
CREATE TABLE consists_of(customer_id INTEGER, part_id INTEGER, quantity INTEGER, price INTEGER);
INSERT INTO part VALUES(10, 50);
INSERT INTO part VALUES (20, 1000);
CREATE TRIGGER IF NOT EXISTS raise_if_consists_of_price_too_expensive
AFTER INSERT ON consists_of
WHEN new.price > (SELECT part.price * new.quantity FROM part WHERE part.part_id = new.part_id)
BEGIN
SELECT RAISE (ABORT, 'too expensive.');
END;
-- OK
INSERT INTO consists_of(customer_id, part_id, quantity, price)
VALUES(10050, 20, 31, 100);
-- OK
INSERT INTO consists_of(customer_id, part_id, quantity, price)
VALUES(80030, 10, 9, 50 * 9);
-- Raise abort
INSERT INTO consists_of(customer_id, part_id, quantity, price)
VALUES(80099, 10, 9, 50 * 9 + 1);
- 插入后
CREATE TABLE part(part_id INTEGER, price INTEGER);
CREATE TABLE consists_of(customer_id INTEGER, part_id INTEGER, quantity INTEGER, price INTEGER);
INSERT INTO part VALUES(10, 50);
INSERT INTO part VALUES (20, 1000);
CREATE TRIGGER IF NOT EXISTS fill_consists_of_price
AFTER INSERT ON consists_of
BEGIN
UPDATE consists_of
SET
price = (
SELECT consists_of.quantity * part.price
FROM part
WHERE part.part_id = consists_of.part_id
)
WHERE customer_id = new.customer_id AND part_id = new.part_id
;
END;
INSERT INTO consists_of(customer_id, part_id ,quantity)
VALUES(10050, 20, 31);
INSERT INTO consists_of(customer_id, part_id ,quantity)
VALUES(80033, 10, 9);
我有两个表 -
part(partID, brand, price, size)
consists_of(customerID, partID, quantity, price, shipdate)
|
FK part.ID from part
Table part
永远不会是 changed/updated,但 consists_of
会。
- How do I add a [before insert?] trigger on
consists_of
that checks ifconsists_of.price
for each entry is less than or equalconsists_of.quantity * part.price
for that particularconsists_of.partID
and raises an abort if it isn't so?
或者,
- How do I add [after insert?] trigger on
consists_of
that doesINSERT INTO consists_of(price) VALUES (...)
where the value ofconsists_of.price
is equal toconsists_of.quantity * part.price
for thatconsists_of.partID
?
如果我没理解错的话,你可以在子查询中 select part.price
并计算 part.price * consists_of.quantitiy
.
- 插入前
CREATE TABLE part(part_id INTEGER, price INTEGER);
CREATE TABLE consists_of(customer_id INTEGER, part_id INTEGER, quantity INTEGER, price INTEGER);
INSERT INTO part VALUES(10, 50);
INSERT INTO part VALUES (20, 1000);
CREATE TRIGGER IF NOT EXISTS raise_if_consists_of_price_too_expensive
AFTER INSERT ON consists_of
WHEN new.price > (SELECT part.price * new.quantity FROM part WHERE part.part_id = new.part_id)
BEGIN
SELECT RAISE (ABORT, 'too expensive.');
END;
-- OK
INSERT INTO consists_of(customer_id, part_id, quantity, price)
VALUES(10050, 20, 31, 100);
-- OK
INSERT INTO consists_of(customer_id, part_id, quantity, price)
VALUES(80030, 10, 9, 50 * 9);
-- Raise abort
INSERT INTO consists_of(customer_id, part_id, quantity, price)
VALUES(80099, 10, 9, 50 * 9 + 1);
- 插入后
CREATE TABLE part(part_id INTEGER, price INTEGER);
CREATE TABLE consists_of(customer_id INTEGER, part_id INTEGER, quantity INTEGER, price INTEGER);
INSERT INTO part VALUES(10, 50);
INSERT INTO part VALUES (20, 1000);
CREATE TRIGGER IF NOT EXISTS fill_consists_of_price
AFTER INSERT ON consists_of
BEGIN
UPDATE consists_of
SET
price = (
SELECT consists_of.quantity * part.price
FROM part
WHERE part.part_id = consists_of.part_id
)
WHERE customer_id = new.customer_id AND part_id = new.part_id
;
END;
INSERT INTO consists_of(customer_id, part_id ,quantity)
VALUES(10050, 20, 31);
INSERT INTO consists_of(customer_id, part_id ,quantity)
VALUES(80033, 10, 9);