[英]Set AUTO_INCREMENT starting value in a InnoDB table to zero?
Is there any to get the an AUTO_INCREMENT field of a InnoDB to start counting from 0 not 1 有没有什么可以让InnoDB的AUTO_INCREMENT字段从0开始计数而不是1
CREATE TABLE `df_mainevent` (
`idDf_MainEvent` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`idDf_MainEvent`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
MySQL documentation: MySQL文档:
If a user specifies NULL or 0 for the AUTO_INCREMENT column in an INSERT, InnoDB treats the row as if the value had not been specified and generates a new value for it.
如果用户为INSERT中的AUTO_INCREMENT列指定NULL或0,则InnoDB会将行视为未指定值,并为其生成新值。
So it means that 0 is a 'special' value which is similar to NULL. 因此,这意味着0是一个类似于NULL的“特殊”值。 Even when you use AUTO_INCREMENT = 0 is will set the initial value to 1.
即使使用AUTO_INCREMENT = 0也会将初始值设置为1。
Beginning with MySQL 5.0.3, InnoDB supports the AUTO_INCREMENT = N table option in CREATE TABLE and ALTER TABLE statements, to set the initial counter value or alter the current counter value.
从MySQL 5.0.3开始,InnoDB在CREATE TABLE和ALTER TABLE语句中支持AUTO_INCREMENT = N表选项,以设置初始计数器值或更改当前计数器值。 The effect of this option is canceled by a server restart, for reasons discussed earlier in this section.
由于本节前面讨论的原因,服务器重新启动会取消此选项的作用。
CREATE TABLE `df_mainevent` (
`idDf_MainEvent` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`idDf_MainEvent`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
works with MySQL >= 5.0.3. 适用于MySQL> = 5.0.3。
EDIT: 编辑:
Just noticed that MySQL in general does not like auto-increment values equal to 0
- that's independent from the used storage engine. 刚刚注意到,MySQL通常不喜欢等于
0
自动增量值-它与所使用的存储引擎无关。 MySQL just uses 1
as the first auto-increment value. MySQL仅使用
1
作为第一个自动增量值。 So to answer the question: NO that's not possible but it does not depend on the storage engine. 因此,要回答这个问题: 不,那是不可能的,但它不依赖于存储引擎。
This works in both InnoDB and MyISAM, and the second insert is a 1
not a 2
: 这在InnoDB和MyISAM中都适用,第二个插入是
1
而不是2
:
CREATE TABLE ex1 (id INT AUTO_INCREMENT PRIMARY KEY) ENGINE=MyISAM;
SET sql_mode='NO_AUTO_VALUE_ON_ZERO';
INSERT INTO ex1 SET id=0;
INSERT INTO ex1 SET id=NULL;
SELECT * FROM ex1;
+----+
| id |
+----+
| 0 |
| 1 |
+----+
2 rows in set (0.00 sec)
CREATE TABLE ex2 (id INT AUTO_INCREMENT PRIMARY KEY) ENGINE=InnoDB;
SET sql_mode='NO_AUTO_VALUE_ON_ZERO';
INSERT INTO ex2 SET id=0;
INSERT INTO ex2 SET id=NULL;
SELECT * FROM ex2;
+----+
| id |
+----+
| 0 |
| 1 |
+----+
2 rows in set (0.00 sec)
Daren Schwenke's technique works. Daren Schwenke的技术作品。 To bad that the next record inserted will be 2.
不好的是,下一个插入的记录将是2。
For example: 例如:
CREATE TABLE IF NOT EXISTS `table_name` (
`ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(100) NOT NULL,
PRIMARY KEY( `ID` )
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
INSERT INTO `table_name` (`Name`) VALUES ('Record0?');
UPDATE `table_name` SET `ID`=0 WHERE `ID`=1;
INSERT INTO `table_name` (`Name`) VALUES ('Record1?');
SELECT * FROM `table_name`;
ID Name
0 Record0?
2 Record1?
This isn't a big deal its just annoying. 这不是什么大问题,只是令人讨厌。
Tim 提姆
I have not been able to have autoincrement start at 0, but starting at 1 and then setting it to 0 via an UPDATE works fine. 我无法从0开始自动递增,但是从1开始,然后通过UPDATE将其设置为0很好。
I commonly use this trick to detect deletes in a table. 我通常使用此技巧来检测表中的删除。
On update of any row, I set that row's last update time. 在更新任何行时,我都设置了该行的最后更新时间。
On deletes, I set the last update time of row 0. 在删除时,我设置第0行的最后更新时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.