在mysql中创建触发器以生成UUID [英] Creating trigger in mysql for generation UUID
本文介绍了在mysql中创建触发器以生成UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在插入MySQL之前尝试创建触发器时,如何为每个输入生成新的UUID,这会引发错误.
how can this throw an error when trying to create a trigger before insert in MySQL so that it generates new UUID for every input.
CREATE TRIGGER insert_guid
BEFORE INSERT ON guid_tool (FOR EACH ROW
BEGIN
SET NEW.guid_key = UUID());
END;
这是我的桌子
create table guid_tool (
ID INT NOT NULL AUTO_INCREMENT,
guid_key CHAR(40) NOT NULL,
PRIMARY KEY(ID)
) CHARSET=LATIN1;
我一定做错了.
推荐答案
正如@mabi在评论中提到的那样-括号中有语法错误.以下修改后的代码对我有用:
As mentioned by @mabi in the comments - you have a syntax error with the brackets. The following modified code works for me:
DELIMITER #
CREATE TRIGGER insert_guid
BEFORE INSERT ON guid_tool
FOR EACH ROW
BEGIN
SET NEW.guid_key = UUID();
END;
#
DELIMITER ;
对其进行测试:
mysql> INSERT INTO guid_tool (ID) VALUES (1);
Query OK, 1 row affected, 1 warning (0.04 sec)
mysql> SELECT * FROM guid_tool;
+----+--------------------------------------+
| ID | guid_key |
+----+--------------------------------------+
| 1 | a0467ebf-5c4f-11e3-903a-6cccbb4423e3 |
+----+--------------------------------------+
1 row in set (0.00 sec)
这篇关于在mysql中创建触发器以生成UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文