Note that there are some explanatory texts on larger screens.

plurals
  1. POIn MariaDB, function placed in a trigger starts returns a different value
    primarykey
    data
    text
    <p>There are such table and functions:</p> <pre><code>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 </code></pre> <p>Insert is working properly:</p> <pre><code>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 | +--------------------------------------+ </code></pre> <p>Just create a trigger:</p> <pre><code>CREATE TRIGGER `test_uuid_encode` BEFORE INSERT ON `test` FOR EACH ROW BEGIN SET NEW.ID = UUID_ENCODE(NEW.ID); END </code></pre> <p>and get <strong>half truncated</strong> value in table:</p> <pre><code>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 | +--------------------------------------+ </code></pre> <p>What is wrong with this trigger?</p> <p>Software version is:</p> <blockquote> <p>Ver 5.5.33a-MariaDB for Win64 on x86 (mariadb.org binary distribution)</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload