如何使用UNIQUE Chars序列在MySQL上生成TRIGGER?

时间:2016-08-02 19:37:35

标签: mysql triggers

O /

我想使用大写和小写+数字生成4个字符序列..但我想在MySQL DB上插入新用户之前使用触发器生成此序列。

类似=“Ae5f”或“5Bd2”

我正在使用

  CHAR( FLOOR(65 + (RAND() * 25))),
  CHAR( FLOOR(65 + (RAND() * 25))),
  CHAR( FLOOR(65 + (RAND() * 25))),
  CHAR( FLOOR(65 + (RAND() * 25)))

因此代码将生成仅4个大写字母的数字并且不会是 UNIQUE 值,在某些时间返回“重复条目”。

任何人都可以帮助我?

-----------------------编辑-----------------------

我在Base32上的最后一次实现

CREATE DEFINER=`root`@`localhost` TRIGGER `healthcare`.`TESTE_BEFORE_INSERT` BEFORE INSERT ON `teste` FOR EACH ROW
BEGIN
    DECLARE last_id integer;
    SET last_id = (SELECT MAX(ID) AS lastID FROM `healthcare`.`teste`);
    IF last_id IS NULL THEN
        SET last_id = 0;
    END IF;
    SET NEW.USER_KEY = conv((1048575-last_id), 10, 32); /* 1048575 = VVVV */
END

欢迎随机化解决方案。

1 个答案:

答案 0 :(得分:2)

以下是您可能感兴趣的例程。这个概念是你有一个控制表用于各种序列(在系统范围内使用)。简而言之,以下是Base36稻草人。它使用MySQL“意图锁定”。

您要求的是Base36数字功能,这就是它的一部分。如果你这样做,你如何将它楔入你的设置中取决于你。

控制表:

-- drop table if exists sequences;
create table sequences
(   -- the numbers in here are the next numbers free to use
    -- so it is yours once you acquire the INTENTION lock
    -- but do an UPDATE for the next guy by incrementing the number
    -- and COMMIT
    id int auto_increment primary key,
    sectionType varchar(200) not null,
    nextSequence int not null,
    unique key(sectionType) -- perhaps overkill on index but meh
);

-- truncate table sequences;
insert sequences (sectionType,nextSequence) values
('Chassis Serial Number',1),('Engine Block Serial Number',1),('base36number',0);

存储过程:

DROP PROCEDURE IF EXISTS getBase36;
DELIMITER $$
CREATE PROCEDURE getBase36
(   OUT sOutVar CHAR(4) -- out parameter for base36 number
)
BEGIN
    DECLARE num_to_use INT;
    DECLARE i1,i2 INT;
    DECLARE sSendBack CHAR(4);

    -- 0-9 is ascii 48 to 57
    -- A-Z is ascii 65 to 90
    -- 0000 to ZZZZ in that order. 0 to 9 then A etc 36 positions
    -- first char is 0. 36th char is Z. Base36,  0 is 0000 , 35 is 000Z, 36 is 00010
    -- 1.68M possibilities
    -- output ranges from 0000 to ZZZZ
    START TRANSACTION;
    SELECT nextSequence into num_to_use from sequences where sectionType='base36number' FOR UPDATE;
    UPDATE sequences set nextSequence=nextSequence+1 where sectionType='base36number';
    COMMIT; -- because of this, it cannot be a FUNCTION but must be a stored proc, else error 1422
            -- which is Error 1422: Explicit or implicit commit is not allowed in a stored function or trigger

    SET sOutVar='';

    -- IF num_to_use>1679616 THEN
    --  SET sSendBack='----'; 
    --     -- SET sOutVar='----'; -- ran out of space. Think up something else. This was your idea, afterall :p
    --  -- we will drop out of routine
    -- END IF;

    IF num_to_use<1679616 THEN
        -- I don't feel like doing a LOOP for the below
        -- Honestly just because I am tired at the moment.

        SET i2=num_to_use;
        SET i1=FLOOR(i2/46656); -- 46656 is 36 cubed
        IF i1 between 0 and 9 THEN
            SET sSendBack=CHAR(48+i1);
        ELSE
            SET sSendBack=CHAR(65+i1-10);
        END IF;

        SET i2=i2-(i1*46656); 
        SET i1=FLOOR(i2/1296); -- 1296 is 36 squared
        IF i1 between 0 and 9 THEN
            SET sSendBack=CONCAT(sSendBack,CHAR(48+i1));
        ELSE
            SET sSendBack=CONCAT(sSendBack,CHAR(65+i1-10));
        END IF;

        SET i2=i2-(i1*1296); 
        SET i1=FLOOR(i2/36); -- 36 is 36 to the first power
        IF i1 between 0 and 9 THEN
            SET sSendBack=CONCAT(sSendBack,CHAR(48+i1));
        ELSE
            SET sSendBack=CONCAT(sSendBack,CHAR(65+i1-10));
        END IF;

        SET i2=i2-(i1*36);
        SET i1=FLOOR(i2/1); -- 1 is 36 to the 0th
        IF i1 between 0 and 9 THEN
            SET sSendBack=CONCAT(sSendBack,CHAR(48+i1));
        ELSE
            SET sSendBack=CONCAT(sSendBack,CHAR(65+i1-10));
        END IF;
        SET sOutVar=sSendBack; -- base36 number (a string) to OUT parameter
        SELECT num_to_use,sOutVar as yourNumber; -- send out as a resultset too
    ELSE
        SET sSendBack='----';
        SET sOutVar=sSendBack;  -- base36 number (a string) to OUT parameter
        select num_to_use,sOutVar as yourNumber; -- send out as a resultset too
    END IF;
END;$$
DELIMITER ;  

测试:

set @f='';
CALL getBase36(@f); -- initial time for 0000
CALL getBase36(@f); -- 1 is 0001
CALL getBase36(@f); -- 2 is 0002

-- now start testing Boundary conditions

-- pretend we have an INTENTION lock on table and just do an update to test quicker
UPDATE sequences set nextSequence=34 where sectionType='base36number';
CALL getBase36(@f); -- 34 is 000Y
CALL getBase36(@f); -- 35 is 000Z
CALL getBase36(@f); -- 36 is 0010

UPDATE sequences set nextSequence=12345 where sectionType='base36number';
CALL getBase36(@f); -- 12345 is 09IX =9*36*36+18*36+33 
select 9*36*36+18*36+33;
-- = 12345
UPDATE sequences set nextSequence=1679614 where sectionType='base36number';
CALL getBase36(@f); -- 1679614 is ZZZY 
CALL getBase36(@f); -- 1679615 is ZZZZ
CALL getBase36(@f); -- 1679616 is ----
CALL getBase36(@f); -- 1679617 is ----
-- so after 1679615 you can't use your numbering scheme anymore (or my scheme)
-- but in either case, this is the range of numbers you chose to implement
-- up to 1.68M (roughly)