(5)创建入库表出库表上的六个触发器。
时间: 2024-04-30 21:19:34 浏览: 51
数据库创建触发器
下面是针对入库表和出库表的六个触发器:
1. 入库表插入触发器:当向入库表中插入一条记录时,自动更新库存表中对应商品的库存数量。
```
CREATE TRIGGER update_inventory_on_insert
AFTER INSERT ON incoming_inventory
FOR EACH ROW
BEGIN
UPDATE inventory
SET quantity = quantity + NEW.quantity
WHERE product_id = NEW.product_id;
END;
```
2. 入库表更新触发器:当更新入库表中的一条记录时,自动更新库存表中对应商品的库存数量。
```
CREATE TRIGGER update_inventory_on_update
AFTER UPDATE ON incoming_inventory
FOR EACH ROW
BEGIN
UPDATE inventory
SET quantity = quantity - OLD.quantity + NEW.quantity
WHERE product_id = NEW.product_id;
END;
```
3. 入库表删除触发器:当从入库表中删除一条记录时,自动更新库存表中对应商品的库存数量。
```
CREATE TRIGGER update_inventory_on_delete
AFTER DELETE ON incoming_inventory
FOR EACH ROW
BEGIN
UPDATE inventory
SET quantity = quantity - OLD.quantity
WHERE product_id = OLD.product_id;
END;
```
4. 出库表插入触发器:当向出库表中插入一条记录时,自动更新库存表中对应商品的库存数量。
```
CREATE TRIGGER update_inventory_on_insert
AFTER INSERT ON outgoing_inventory
FOR EACH ROW
BEGIN
UPDATE inventory
SET quantity = quantity - NEW.quantity
WHERE product_id = NEW.product_id;
END;
```
5. 出库表更新触发器:当更新出库表中的一条记录时,自动更新库存表中对应商品的库存数量。
```
CREATE TRIGGER update_inventory_on_update
AFTER UPDATE ON outgoing_inventory
FOR EACH ROW
BEGIN
UPDATE inventory
SET quantity = quantity + OLD.quantity - NEW.quantity
WHERE product_id = NEW.product_id;
END;
```
6. 出库表删除触发器:当从出库表中删除一条记录时,自动更新库存表中对应商品的库存数量。
```
CREATE TRIGGER update_inventory_on_delete
AFTER DELETE ON outgoing_inventory
FOR EACH ROW
BEGIN
UPDATE inventory
SET quantity = quantity + OLD.quantity
WHERE product_id = OLD.product_id;
END;
```
阅读全文