有没有办法在mysql数据库中插入带有前缀的自动增量主id? [英] Is there a way to insert an auto-incremental primary id with a prefix in mysql database?

查看:418
本文介绍了有没有办法在mysql数据库中插入带有前缀的自动增量主id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在MySQL数据库中插入一个数据作为主要ID,它有一个字母数字值和两个数字值。该数据将自动递增地生成数字,但字母数字值将被固定。像D1,D2 ... D54,D55,D56等。这里,'D'总是相同的,但是数字将自动递增。有没有办法做到这一点?

解决方案

首先是其他人评论,你可以有这个id值即时生成。



但是如果你想要它,你至少有两种方法:



strong>或多或少可靠方式涉及使用单独的表进行排序和触发



方案:

  CREATE TABLE Table1_seq 

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
CREATE TABLE Table1

`id` VARCHAR(10)NOT NULL PRIMARY KEY DEFAULT'',
...
);

触发器:

 code> DELIMITER $$ 
CREATE TRIGGER tg_bi_table1
BEFORE INSERT ON table1
每个ROW
BEGIN
INSERT INTO table1_seq()VALUES();
SET NEW.id = CONCAT('D',LPAD(LAST_INSERT_ID(),4,'0'));
END $$
DELIMITER;然后,您只需将行插入到 table1

  INSERT INTO Table1()VALUES(),(), 

你会得到

 
| ID |
---------
| D0001 |
| D0002 |
| D0003 |

这里是 SQLFiddle 演示



不可靠的方式是在< c $ c> INSERT 语句本身

  INSERT INTO Table1(id,...)
SELECT CONCAT('D',LPAD(COALESCE(SUBSTR(MAX(id),2),0)+ 1,4,'0')),
...
FROM table1

这里是 SQLFiddle 演示



此方法的问题:


  1. 在重负载下,两个并发会话可以获取相同的 MAX(id)值,因此生成相同的新id,


  2. 不能使用多插入语句。
  3. I'm trying to insert a data as a primary ID that has one alphanumerical value and two numerical value in MySQL database. This data will auto incrementally generate number, but the alphanumerical value will be fixed. Like, D1, D2....D54, D55, D56 etc. Here, 'D' is always the same, but the number will be automatically incremented. Is there any way to do this?

    解决方案

    First of all it's unadvisable to do so, like others commented, you can have this id value generated on the fly.

    But if nonetheless you want it your way there're at least two ways to do so:

    More or less reliable way involves using a separate table for sequencing and a trigger

    Schema:

    CREATE TABLE Table1_seq 
    (
      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
    );
    CREATE TABLE Table1
    (
      `id` VARCHAR(10) NOT NULL PRIMARY KEY DEFAULT '',
       ...
    );
    

    Trigger:

    DELIMITER $$
    CREATE TRIGGER tg_bi_table1
    BEFORE INSERT ON table1
    FOR EACH ROW
    BEGIN
      INSERT INTO table1_seq() VALUES();
      SET NEW.id = CONCAT('D', LPAD(LAST_INSERT_ID(), 4,'0'));
    END$$
    DELIMITER ;
    

    Then you just insert your rows to table1

    INSERT INTO Table1 () VALUES (),(),();
    

    And you'll get

    |    ID |
    ---------
    | D0001 |
    | D0002 |
    | D0003 |
    

    Here is SQLFiddle demo

    Unreliable way is to generate your new id on the fly in INSERT statement itself

    INSERT INTO Table1 (id, ...) 
    SELECT CONCAT('D', LPAD(COALESCE(SUBSTR(MAX(id), 2), 0) + 1, 4, '0')),
           ...
      FROM table1
    

    Here is SQLFiddle demo

    The problems with this approach:

    1. Under heavy load two concurrent sessions can grab the same MAX(id) value and therefore generate the same new id leading to the failure of insert.
    2. You can't use multi-insert statements

    这篇关于有没有办法在mysql数据库中插入带有前缀的自动增量主id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆