简体   繁体   English

SQL插入后如何使用触发器更新另一个表表

[英]How to update another table table after SQL insert with trigger

I'm a bit stuck with SQL triggers. 我对SQL触发器有些困惑。 (in Phpmyadmin), I looked how to fix my problem already in stackoverflow, but I couldn't find an answer (在Phpmyadmin中),我研究了如何在stackoverflow中解决我的问题,但找不到答案

I have a table in database, named "items", it has rows- ID, name, price, section_ID, number (how much I got). 我在数据库中有一个名为“ items”的表,它具有行ID,名称,价格,section_ID,编号(我得到了多少)。 other table is "actions", and it has rows- ID, when_inserted, which_table, actions_type_ID (insert, update, delete. like that), user_ID. 另一个表是“操作”,它具有行ID,when_inserted,whit_table,actions_type_ID(插入,更新,删除等),user_ID。

So, for what do I need the trigger? 那么,我需要什么触发器? After the update in table "items", i need to create new record in table "actions", with new ID, ec (table "actions" is for inserting every action what has been done on the database, from what person, and when) 在表“ items”中更新之后,我需要在表“ actions”中创建具有新ID的新记录ec(表“ actions”用于插入每个操作,该操作是对数据库进行的操作,来自哪个人,何时进行操作) )

Yes, I know that this question might be answered already, but I couldn't find it. 是的,我知道这个问题可能已经回答了,但是我找不到。 If you can help me and give a link to where I can follow trough and do what I need, then- please do so. 如果您可以帮助我,并提供指向我可以遵循的低谷并做我需要做的事情的链接,那么请这样做。 If you can answer with syntax, that would be awesome. 如果您可以使用语法回答,那真是太棒了。

And sorry about my bad English. 很抱歉我的英语不好。

you need to create triggers on table items for each operation one for insert one for update and one for delete 您需要为每个操作在表项上创建触发器,一个用于插入一个用于更新,另一个用于删除

DROP TRIGGER IF EXISTS `insertItems`;
DELIMITER //
CREATE TRIGGER `insertItems` AFTER UPDATE ON `items`
 FOR EACH ROW BEGIN
INSERT INTO  actions (when_inserted, which_table, actions_type_ID) VALUES (CURRENT_TIMESTAMP, 'items', 'update');
END
//
DELIMITER ;

and so on 等等

hope this helps 希望这可以帮助

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

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