创建入库表出库表上的六个触发器。mysql能运行代码
时间: 2024-05-13 11:16:49 浏览: 75
以下是创建入库表和出库表上的六个触发器的 MySQL 代码:
```
-- 创建入库表触发器
CREATE TRIGGER before_insert_incoming
BEFORE INSERT ON incoming
FOR EACH ROW
BEGIN
-- 增加商品库存
UPDATE product SET stock = stock + NEW.quantity WHERE id = NEW.product_id;
END;
CREATE TRIGGER after_insert_incoming
AFTER INSERT ON incoming
FOR EACH ROW
BEGIN
-- 增加商品入库总量
UPDATE product SET total_incoming = total_incoming + NEW.quantity WHERE id = NEW.product_id;
END;
CREATE TRIGGER before_delete_incoming
BEFORE DELETE ON incoming
FOR EACH ROW
BEGIN
-- 减少商品库存
UPDATE product SET stock = stock - OLD.quantity WHERE id = OLD.product_id;
END;
CREATE TRIGGER after_delete_incoming
AFTER DELETE ON incoming
FOR EACH ROW
BEGIN
-- 减少商品入库总量
UPDATE product SET total_incoming = total_incoming - OLD.quantity WHERE id = OLD.product_id;
END;
-- 创建出库表触发器
CREATE TRIGGER before_insert_outgoing
BEFORE INSERT ON outgoing
FOR EACH ROW
BEGIN
-- 减少商品库存
UPDATE product SET stock = stock - NEW.quantity WHERE id = NEW.product_id;
END;
CREATE TRIGGER after_insert_outgoing
AFTER INSERT ON outgoing
FOR EACH ROW
BEGIN
-- 增加商品出库总量
UPDATE product SET total_outgoing = total_outgoing + NEW.quantity WHERE id = NEW.product_id;
END;
CREATE TRIGGER before_delete_outgoing
BEFORE DELETE ON outgoing
FOR EACH ROW
BEGIN
-- 增加商品库存
UPDATE product SET stock = stock + OLD.quantity WHERE id = OLD.product_id;
END;
CREATE TRIGGER after_delete_outgoing
AFTER DELETE ON outgoing
FOR EACH ROW
BEGIN
-- 减少商品出库总量
UPDATE product SET total_outgoing = total_outgoing - OLD.quantity WHERE id = OLD.product_id;
END;
```
这些触发器包括:
1. `before_insert_incoming`:在插入入库记录之前,将商品库存增加。
2. `after_insert_incoming`:在插入入库记录之后,将商品入库总量增加。
3. `before_delete_incoming`:在删除入库记录之前,将商品库存减少。
4. `after_delete_incoming`:在删除入库记录之后,将商品入库总量减少。
5. `before_insert_outgoing`:在插入出库记录之前,将商品库存减少。
6. `after_insert_outgoing`:在插入出库记录之后,将商品出库总量增加。
7. `before_delete_outgoing`:在删除出库记录之前,将商品库存增加。
8. `after_delete_outgoing`:在删除出库记录之后,将商品出库总量减少。
这些触发器将确保商品的库存和总量始终保持正确的状态。
阅读全文