简体   繁体   English

数据库触发器将基于另一个触发更新一个表

[英]Database triggers to update one table based on another

I have 2 tables 我有2张桌子

  1. customer (which contains customer_id as primary key etc) 客户(包含customer_id作为主键,等等)
  2. order1 (which also contains order_id as primary key, customer_id as foreign key) order1(也包含order_id作为主键,customer_id作为外键)

I was asked to write a trigger to update order1's customer_id whenever customer table's customer_id was updated. 每当客户表的customer_id被更新时,我被要求编写一个触发器来更新order1的customer_id。

Please help me to find the answer. 请帮助我找到答案。 Please write me the query for the question. 请给我查询该问题。

If you want more information, reply me. 如果您需要更多信息,请回复我。

I have not tried this before because I don't know what to do very weak in triggers as well as in database. 我以前没有尝试过,因为我不知道在触发器以及数据库中该怎么做。

I don't have any idea about these. 我对此一无所知。

You can put pretty much any SQL you want in your trigger code. 您可以在触发器代码中放入几乎任何您想要的SQL。

For example a mysql trigger with a query which inserts data in a second table: 例如,带有查询的mysql触发器,该查询将数据插入第二个表中:

CREATE TRIGGER trigger_tru AFTER UPDATE ON some_table
FOR EACH ROW BEGIN INSERT INTO someotherdatabase.someothertable
SELECT * FROM some_table AS tablealias WHERE tablealias.primarykey=new.primarykey; 
END;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM