Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set autoincrement format to 0001 in MySQL?

Tags:

mysql

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

So instead of '1' make '0001'?

like image 262
32. Avatar asked Mar 04 '09 16:03

32.


People also ask

What does AutoIncrement do in MySQL?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

How do I change my primary key to auto increment?

To change a primary key to auto_increment, you can use MODIFY command. Let us first create a table. Look at the above sample output, StudentId column has been changed to auto_increment.

How do I create an autonumber in MySQL?

You can set the MySQL Auto Increment Primary Key field via the following syntax: CREATE TABLE table_name ( column1 datatype NOT NULL AUTO_INCREMENT, column2 datatype [ NULL | NOT NULL ], ... );


6 Answers

Try adding ZEROFILL attribute to the field.

like image 97
Jeremy L Avatar answered Oct 05 '22 17:10

Jeremy L


Could you leave it as an integer and format it for humans in your SQL, for example, to pad with zeros to 4 chars wide

select lpad(idcolumn,4,'0') from mytable;

Or use zerofill and specify the desired width when declaring the table:

create table tmpfoo (
   mykey int(6) zerofill not null auto_increment, 
   primary key(mykey)
);

insert into tmpfoo values(1),(2);

select * from tmpfoo;
+--------+
| mykey  |
+--------+
| 000001 |
| 000002 |
+--------+
like image 31
Paul Dixon Avatar answered Oct 05 '22 16:10

Paul Dixon


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.

like image 41
gahooa Avatar answered Oct 05 '22 18:10

gahooa


If you need the auto_increment column in a zero padded format, I suggest that you display it as such and not attempt to store it in the database that way.

In PHP, you could use the following code to display or otherwise use the id:

$padded_id = str_pad($id, 4, '0');
like image 22
jonstjohn Avatar answered Oct 05 '22 16:10

jonstjohn


To pad in the database set the id column to ZEROFILL

But if its for display purposes only I recommend using LPAD SELECT RIGHT('000000' + yourNum, 6);

like image 30
Hawk Kroeger Avatar answered Oct 05 '22 18:10

Hawk Kroeger


is the field an integer? if so, the answer is, "why? it's an integer!" ;-)

like image 1
Steven A. Lowe Avatar answered Oct 05 '22 17:10

Steven A. Lowe