0

I have a users table in my database and I want it to set UUID for every new user record.

DELIMITER ;;
CREATE TRIGGER user_uuid
BEFORE INSERT ON users
FOR EACH ROW
BEGIN
  IF new.uuid IS NULL THEN
    SET new.uuid = uuid();
  END IF;
END
;;

I used the above code and I checked from phpMyAdmin if it's set or not. It seems set but not creating UUID value on uuid columns when a user registered.

Can you see what's wrong with my code as it seems okay from here. Thank you.

I'm storing UUID values on CHAR32 data type, if it's needed.

  • 2
    You might want to check out this question [How to make MySQL table primary key auto increment with some prefix](http://stackoverflow.com/questions/17893988/how-to-make-mysql-table-primary-key-auto-increment-with-some-prefix) – dearSympho May 05 '17 at 22:41
  • Why not just insert a `UUID()` value when creating users? – tadman May 05 '17 at 22:52
  • Why aren't you using `auto_increment`? – Barmar May 05 '17 at 23:15

1 Answers1

0

This code worked for me;

DELIMITER //
CREATE TRIGGER before_insert_on_user_set_uuids
  BEFORE INSERT ON users
  FOR EACH ROW 
    BEGIN
      SET new.uuid = uuid();
    END ;  //
DELIMITER ;