How can I set autoincrement format to 0001 in MySQL?

后端 未结 6 465
悲&欢浪女
悲&欢浪女 2020-11-29 05:59

How can I make MySQL auto increment in 4 digit format?

So instead of \'1\' make \'0001\'?

6条回答
  •  清歌不尽
    2020-11-29 06:42

    MySQL supports ZEROFILL on integer columns:

    mysql> create table foo (the_key int unsigned zerofill not null 
           auto_increment primary key);
    Query OK, 0 rows affected (0.21 sec)
    
    mysql> insert into foo SET the_key = Null;
    Query OK, 1 row affected (0.00 sec)
    
    ...
    
    mysql> insert into foo SET the_key = Null;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from foo;
    +------------+
    | the_key    |
    +------------+
    | 0000000001 |
    | 0000000002 |
    | 0000000003 |
    | 0000000004 |
    | 0000000005 |
    | 0000000006 |
    | 0000000007 |
    | 0000000008 |
    +------------+
    8 rows in set (0.00 sec)
    

    You may need to look into using a smallint (5 digits), or trimming/padding.

提交回复
热议问题