Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert the key 0 into mysql for AUTO_INCREMENT row? [duplicate]

Tags:

mysql

Possible Duplicate:
How to force MySQL to take 0 as a valid auto-increment value

I know I can do an update on an existing row, but is it possible to INSERT a row with the id 0?

mysql> create table inc( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, sz TEXT);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into inc(id, sz) select 25, "a";
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into inc(id, sz) select 25, "a";
ERROR 1062 (23000): Duplicate entry '25' for key 'PRIMARY'
mysql> insert into inc(id, sz) select 27, "b";
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into inc(id, sz) select -5, "c";
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into inc(id, sz) select 0, "d";
Query OK, 1 row affected (0.15 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from inc;
+----+------+
| id | sz   |
+----+------+
| -5 | c    |
| 25 | a    |
| 27 | b    |
| 28 | d    |
+----+------+
4 rows in set (0.00 sec)

1 Answers

From http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

"No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign NULL or 0 to the column to generate sequence numbers."

So it seems using 0 just tells it to auto_increment.

But if you are assigning ID numbers, I'm not sure why you have defined the column to use AUTO_INCREMENT?

like image 148
mary Avatar answered Oct 23 '25 19:10

mary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!