The MySQL implementation automatically uses the UUID as the primary key and solves the idea that the trigger cannot be invoked

Source: Internet
Author: User
Tags uuid

This function is implemented using the trigger program.

The triggering program syntax is as follows:

Create Trigger <tri_name>

{Before|after}

{Insert|update|delete}

On <tab_name>

For each row

< trigger SQL statements >

Core code:

1 Use t14test2 Show Tables3drop tableifexists uuidtest4 CREATE TABLE Uuidtest (5TestID VARCHAR ( $) Not NULL DEFAULT'1',6TestData VARCHAR ( +),7 PRIMARY KEY (' TestID ')8 )9 /*Create a trigger*/Ten /* One * Terminal Creating a stored procedure requires defining a delimiter A * Delimiter// -  * */ - CREATE TRIGGER Tri_auto_uuid the before insert - On uuidtest -  forEach ROW - BEGIN + if New. TestID ='1'ThenSet New. TestID = (Selectuuid ()); -Endif; + END A /*Delete Trigger*/ atDrop Triggerifexists Tri_auto_uuid - /*Inserting Data*/ -Insert into Uuidtest (testData) VALUES ('a piece of data')  - Select* fromUuidtest

Three insertions were run and the results were as follows:

Use a trigger to implement the UUID as the primary key.

Code with the problem:

1 CREATE TRIGGER Tri_auto_uuid 2 After insert 3 On uuidtest 4  for Each ROW 5 Set testid= ((select uuid ()))

If this defines the trigger program, it seems to be no problem, can also add success, but the input data will be error.

Can ' t Update table ' tb_user ' in stored Function/trigger because it's already used by statement which invoked this stored Function/trigger.

Online says to avoid recursive triggering, updating a data cannot trigger an update operation other than set for that data. Otherwise you will get an error.

But I this trigger is after insert and is set operation, why is there a problem?

There is a reason for this, and it may have something to do with recursive triggering. Leave it to the bottom how it works. Just change your mind, and change after insert to before insert.

The MySQL implementation automatically uses the UUID as the primary key and solves the idea that the trigger cannot be invoked

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.