有这样的表和函数:
CREATE TABLE `test` (`ID` BINARY(16)) ENGINE=InnoDB;
CREATE FUNCTION `UUID_ENCODE`(`uuid` CHAR(36))
RETURNS binary(16)
DETERMINISTIC
BEGIN
RETURN UNHEX(REVERSE(REPLACE(uuid,'-','')));
END
CREATE FUNCTION `UUID_DECODE`(`uuid` BINARY(16))
RETURNS char(36)
DETERMINISTIC
BEGIN
RETURN LOWER(CONCAT_WS('-',
REVERSE(HEX(RIGHT(uuid,6))),
REVERSE(HEX(MID(uuid,9,2))),
REVERSE(HEX(MID(uuid,7,2))),
REVERSE(HEX(MID(uuid,5,2))),
REVERSE(HEX(LEFT(uuid,4)))
));
END
插入工作正常:
INSERT INTO test (ID) VALUES(UUID_ENCODE('323febe6-cd89-4773-a46c-aab794fb7cbc'));
SELECT UUID_DECODE(ID) FROM test;
+--------------------------------------+
| uuid_decode(id) |
+--------------------------------------+
| 323febe6cd89-4773-a46c-aab7-94fb7cbc |
+--------------------------------------+
只需创建一个触发器:
CREATE TRIGGER `test_uuid_encode` BEFORE INSERT ON `test` FOR EACH ROW BEGIN
SET NEW.ID = UUID_ENCODE(NEW.ID);
END
并在表中获得一半截断值:
INSERT INTO test (ID) VALUES('323febe6-cd89-4773-a46c-aab794fb7cbc');
Warning (Code 1265): Data truncated for column 'ID' at row 1
SELECT UUID_DECODE(ID) FROM test;
+--------------------------------------+
| uuid_decode(id) |
+--------------------------------------+
| 000000000000-0000-0032-3feb-e6cd8947 |
+--------------------------------------+
这个触发器有什么问题?
软件版本为:
版本 5.5.33a-MariaDB for Win64 on x86(mariadb.org 二进制发行版)