AUTO_INCREMENT 0

Hett
Дата: 27.12.2012 10:24:26
Кто-нибудь знает, почему автоинкрементное поле не может иметь значение 0 ?
trew
Дата: 27.12.2012 11:37:02
Hett,

example-auto-increment

To start with an AUTO_INCREMENT value other than 1, you can set that value with CREATE TABLE or ALTER TABLE, like this:
mysql> ALTER TABL E tbl AUTO_INCREMENT = 100;

(убрать лишний пробел в строке)

При большом желании вы можете сделать так, что бы там хранился 0, например сделать UPDATE.
Но ближайший REPAIR или ALTER TABLE сгенерирует вам ошибку (это нужно проверить).
Hett
Дата: 27.12.2012 12:11:02
Вопрос именно в том, почему такое отношение к нулю?)
trew
Дата: 27.12.2012 12:48:39
Hett,

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.

Вообщем, cохранение 0 не является рекомендуемой практикой, для mysql.

А в MS SQL такого ограничения нет.
CREATE TABLE a3 
(
id int IDENTITY (-3,1) PRIMARY KEY,
dd int
)

Т.е. начинать с (-3) с шагом 1.
-3
-2
-1
0
1
2
и т.д.
Ошибок нет.
Hett
Дата: 27.12.2012 14:56:09
А, ну понел, при сувании ноля он вызывает инкременцию как и при НУЛЛ.