Trigger to update a field automatically after Insert

Asked

Viewed 766 times

0

I have a user table, basically:

CREATE TABLE IF NOT EXISTS `sistema`.`user` (
  `id_user` INT NOT NULL AUTO_INCREMENT COMMENT '',
  `hashed_id` VARCHAR(40) NOT NULL COMMENT '',
  `nome` VARCHAR(40) NOT NULL COMMENT '',
  `cpf` VARCHAR(15) NOT NULL COMMENT '',
  UNIQUE INDEX `hashed_id_UNIQUE` (`hashed_id` ASC)  COMMENT '',
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

I want to create a trigger for when a new user is created, hashed_id is already updated with a code of 12 numbers or letters, and I still have to ensure that this hash is unique, until it is, generate another.

It would be something like this:

CREATE DEFINER = CURRENT_USER TRIGGER `sistema`.`teste_BEFORE_INSERT` BEFORE
INSERT
ON
  `teste` FOR EACH ROW
BEGIN
SELECT
  @lid := LAST_INSERT_ID();
UPDATE
  `sistema`.`teste`
SET
  hashed_id = 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) * 36 + 1,
      1
    )
  )
WHERE
  id = @lid;
  END

This scheme of trigger is from here

  • 12 characters with 36 options generate 4,738,381,338,321,616,896 combinations , if the generation algorithm is random the chance of collision is almost zero.Ast the function to generate the random sequence.

  • This is a "almost zero" problem, it has to be zero! Of course as long as the number of possible combinations is less than the number of registered users.

  • The second problem is on Trigger that is not correct :( I’ve never worked with triggers, so I don’t know how to assemble it

  • It’s easier to hit the Lotto than to get a conflict, I don’t know the Mysql syntax but I would do a Function to generate the string , but check out this OS thread http://stackoverflow.com/questions/16737910/generating-a-random-unique-8-character-string-using-mysql from Paul Spiegel

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.