Mysql trigger insertion

Source: Internet
Author: User
Mysql trigger insert MySQL trigger important learning materials:

Http://dev.mysql.com/doc/refman/5.1/zh/triggers.html

When using a trigger, an error occurs:

[SQL]insert into tb_recharge_records(user_id,pointcoupon_added) values(214983,10);[Err] 1442 - Can't update table 'tb_recharge_records' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

Search for information:

Http://blog.csdn.net/liuliu20036/article/details/4158891

This new record is updated when the trigger is inserted. error it is already used by statement which invoked this stored functiontrigger solution

Problem:
Create trigger InsertUser
Begin
Update users
Set user_power = '[resource]'
Where user_id = new. user_id;
End;
When the trigger is executed, an error occurs: Mysql5.0 is used.
Can't update table 'users' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
What is going on here?
Solution:
If you insert/update the inserted data in the trigger, this problem occurs. This will cause loop calls.
You should use the set operation instead of using update in the trigger, for example
Create trigger InsertUser
Begin
Set new. user_power = '[resource]'
End;
In addition, the trigger must be executed before insertion.

================
The insert time is automatically added when a record is added using a trigger.

Set @ newid = NEW. recharge_id;
If @ newid> 0 then
Update tb_recharge_records set add_time = now () where recharge_id = @ newid and "" = "trigger cannot call host table ";
End if;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.