我有主要的 id。它具有自动增量。
每当我尝试输入默认值时,例如:000 - 它不是从该数字开始计数,而是从 0、1、2、3 等开始计数......
我怎样才能使它成为000?
或者在我的情况下,我真的想要它作为发票 - 比如说 2011-000 并开始计算这可能吗?
有任何想法吗??
为了使您的列包含 000,您需要将其设为zerofill
. zerofill
因此,使用您的列选项更改您的表格。
您可以阅读有关 zerofill 的更多信息,
http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
此外,正如 ajreal 建议的那样,您需要将列物理更新为零。
Why you need to bother between 0000 and 0001 ?
PS - http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html#sqlmode_no_auto_value_on_zero
NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number.
This mode can be useful if 0 has been stored in a table's AUTO_INCREMENT column. (Storing 0 is not a recommended practice, by the way.) For example, if you dump the table with mysqldump and then reload it, MySQL normally generates new sequence numbers when it encounters the 0 values, resulting in a table with contents different from the one that was dumped. Enabling NO_AUTO_VALUE_ON_ZERO before reloading the dump file solves this problem. mysqldump now automatically includes in its output a statement that enables NO_AUTO_VALUE_ON_ZERO, to avoid this problem.
Hope the following make sense without change NO_AUTO_VALUE_ON_ZERO
...
mysql> create table invoice (years int(4) unsigned, seq int(4) unsigned zerofill not null auto_increment, primary key(years, seq)); Query OK, 0 rows affected (0.01 sec) mysql> insert into invoice values (2011, 0); Query OK, 1 row affected (0.00 sec) mysql> update invoice set seq=0; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> alter table invoice auto_increment=0; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into invoice values (2011, 0); Query OK, 1 row affected (0.00 sec) mysql> select * from invoice; +-------+------+ | years | seq | +-------+------+ | 2011 | 0000 | | 2011 | 0001 | +-------+------+ 2 rows in set (0.00 sec) mysql> insert into invoice values (2011, 0), (2011, 0); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from invoice; +-------+------+ | years | seq | +-------+------+ | 2011 | 0000 | | 2011 | 0001 | | 2011 | 0002 | | 2011 | 0003 | +-------+------+ 4 rows in set (0.00 sec)
ALTER TABLE tablename AUTO_INCREMENT = 1