MySQL trigger to process data in this table

Source: Internet
Author: User
Tags mysql tutorial

MySQL triggers are basically described in every Mysql tutorial, but I found that the tutorials are about how to process data from other tables. When writing a trigger in MySQL to operate the column data of this table, the writing method is different from that of other columns.

I wrote a trigger to replace it with a space if the column name string contains "-" before inserting data ).

Error code:

Delimiter |
Create trigger replaceGangcn
Before insert on dept
For each row
Begin
Update dept set name = replace (new. name ,'-','');
End
|
Delimiter;

After the trigger is executed, it is successfully used on the dept table, but when I insert data into the table, Mysql prompts:

ERROR 1442 (HY000): Can't update table 'dept' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

I do not quite understand the meaning of this error message. However, I know that Mysql triggers only need to use the New. Column name to directly process data in this table, rather than using the update statement. The above code is changed to the following:

Delimiter |
Create trigger replaceGangcn
Before insert on dept
For each row
Begin
Set new. name = replace (new. name ,'-','');
End
|
Delimiter;

Related Article

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.