我正在尝试在MYSQL插件上创建一个触发器,如果插入的字段具有空的hashkey,它将生成一个随机的32个字符hashkey。
我按照以下方式尝试了它:
CREATE TRIGGER `external_contacts_hash_auto_create` BEFORE INSERT ON `external_contacts`
FOR EACH ROW BEGIN SET NEW.hash_key =
concat(
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@lid)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@lid)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@lid)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@lid)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed:=round(rand(@seed)*4294967296))*36+1, 1),
substring('abcdefghijklmnopqrstuvwxyz0123456789', rand(@seed)*36+1, 1)
) WHERE OLD.hash_key = null;
END
但是生成的hashkey总是相同的,没有什么是随机的。
答案 0 :(得分:2)
你的整个触发器都充满了错误。您永远不会初始化或为@lid
或@seed
分配值。
4294967296的目的是什么?这对我来说毫无意义。
语句SET whatever WHERE whatever;
在语法上是错误的。这里没有WHERE
条款。
当您与NULL
进行比较时,IS NULL
或IS NOT NULL
。
在BEFORE INSERT
触发器中,没有OLD
行。
并且不要让我开始关于你的编码风格重复同样的声明32次。
看来你只想拥有一个随机字符串。你可以使用这样的东西:
CREATE TABLE ins_hash (id int auto_increment primary key, hash_key char(32));
DELIMITER $$
CREATE TRIGGER trg_ins_hash BEFORE INSERT ON ins_hash
FOR EACH ROW
BEGIN
SET @string := 'abcdefghijklmnopqrstuvwxyz0123456789';
SET @i := 1;
SET @hash := '';
WHILE (@i <= 32) DO
SET @hash := CONCAT(@hash, SUBSTRING(@string, FLOOR(RAND() * 36 + 1), 1));
SET @i := @i + 1;
END WHILE;
SET NEW.hash_key := @hash;
END$$
DELIMITER ;
INSERT INTO ins_hash (id) VALUES (NULL);
INSERT INTO ins_hash (id) VALUES (NULL), (NULL);
SELECT * FROM ins_hash;
+----+----------------------------------+
| id | hash_key |
+----+----------------------------------+
| 1 | tl84mhbtuhng50di5y132sitcyg54wt6 |
| 2 | 3otr42mqfq42q8m7vgec8u9ihk6woer8 |
| 3 | ph0dkgbv4r42q47cv18vbpa4efldw2de |
+----+----------------------------------+
3 rows in set (0.00 sec)