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