在MySQL上创建触发器

时间:2019-03-20 16:35:16

标签: mysql database-trigger

我在这里看到了一些创建触发器示例,但没有一个可以直接与我的情况相关。我有以下两个表:

CREATE TABLE product_item (
product_item_id INT(10) NOT NULL AUTO_INCREMENT,
product_item_name varchar(100) NOT NULL,
product_item_description varchar(100),
product_item_price DECIMAL(6,2),
product_type_id INT(10) NOT NULL,
product_supplier_id INT(10) NOT NULL,
CONSTRAINT product_itempk PRIMARY KEY (product_item_id),
CONSTRAINT product_item_product_typefk FOREIGN KEY (product_type_id) REFERENCES product_type (product_type_id),
CONSTRAINT product_item_supplierfk FOREIGN KEY (product_supplier_id) REFERENCES supplier (supplier_id)
);

CREATE TABLE customer_order_item (
customer_order_id INT(10) NOT NULL,
product_item_id INT(10) NOT NULL,
customer_order_item_price DECIMAL(6,2),
customer_order_item_quantity INT(5) NOT NULL,
CONSTRAINT customer_order_item_orderpk PRIMARY KEY (customer_order_id, product_item_id),
CONSTRAINT customer_order_item_orderfk FOREIGN KEY (customer_order_id) REFERENCES customer_order (order_id),
CONSTRAINT customer_order_item_productfk FOREIGN KEY (product_item_id) REFERENCES product_item (product_item_id)
);

添加 customer_order_item 时,我希望它从 product_item 中的 product_item_price 字段中获取 customer_order_item_price >表格。

我正在寻找类似的东西:

  CREATE TRIGGER after_customer_order_item_insert
  AFTER INSERT ON customer_order_item
  BEGIN
  SET customer_order_item.customer_order_item_price = product_item .product_item_price
  WHERE 
  END

我对SQL完全陌生,所以很抱歉这个问题很幼稚,但是到目前为止我还无法弄清楚。

先谢谢了。

0 个答案:

没有答案