Mysql field , auto increment and same length
i want开发者_StackOverflow社区ed to create a field , which is all in same length. i.e , all the column named 'id' should be like 000001 , 000002 .. 999999 , not 1 , 2 , .. 999999. If i wanted a 6 digits length field with varchar(6) auto_increment.
Many thanks !
To do this, you have to use the ZEROFILL option when creating your field. For instance
CREATE TABLE MyTable(
ID INTEGER(11) ZEROFILL AUTO_INCREMENT
,SomeValue Varchar(100)
,PRIMARY KEY(ID)
);
If you only want 6 digits, then use
ID INTEGER(6) ZEROFILL AUTO_INCREMENT
Instead of 11 listed in the first part. However, after you reach 1,000,000, the columns will no longer be padded to the same length, because all values below 1,000,000 will only have 6 digits. However, it will continue to increment past the 6 digits specified, all the way to the maximum value for an integer (2 billion).
You'll probably want to use an int for this, actually. Add the zerofill attribute to the field and it should do what you want.
Mysql have a zerofill attribute. if you want to fill up to 6 you do
int(11) zerofill
精彩评论