I'm using MariaDB (10.3.29-MariaDB-0+deb10u1) and DbVisualizer (Pro 12.1.1 [Build #3237]) as an IDE.
I want to manage the primary key of a table by having MariaDB set the key to a UUID, I started with this
CREATE TRIGGER before_pkey_maintenance
BEFORE INSERT ON maintenance
FOR EACH ROW
SET NEW.pkey = uuid();
which worked perfectly, except that I started providing my own UUIDs, which the trigger over-wrote, of course. I tried the following to create a UUID only if one was not provided:
CREATE TRIGGER before_pkey_maintenance
BEFORE INSERT ON maintenance
FOR EACH ROW
IF NEW.pkey IS NULL THEN
SET NEW.pkey = uuid();
END IF;
The problem is, when I execute the create, I get the following error:
[Code: 1064, SQL State: 42000] (conn=1002) You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for the right syntax
to use near '' at line 5 [Script position: 132 - 168]
Error 1064 is documented (https://riptutorial.com/mysql/example/2995/error-code-1064--syntax-error) as not correctly using back-ticks, which I can deal with ... except that the message is referring to "syntax to use near '' at line 5", the empty string in the message is less than helpful.
I found found MySQL Trigger with IF statement returning #1064, which adds the use of "delimiter" and tried "|", "//", and "$$" as delimiters. I've tried MANY variations, all similar to the following:
delimiter $$
CREATE TRIGGER before_pkey_maintenance
BEFORE INSERT ON maintenance
FOR EACH ROW
IF NEW.pkey IS null THEN
SET NEW.pkey = uuid();
END IF;
END
$$
delimiter ;
... but none of them worked.
I've looked through DbVisualizer's docs and found nothing that indicates it performs "magic" behind the scenes, but I suspect that if any of the above is correct that DbVis is executing these separately.