I am testing insert-select query and noticed an weird result.
CREATE TABLE `test` (
`cnt` int(11) NOT NULL AUTO_INCREMENT,
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
PRIMARY KEY (`cnt`)
)
CREATE TABLE `test_current` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL
)
First I created two tables, and insert some values into test_current
mysql> insert into test_current (a,b) values (1,1),(2,2);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
And I did this query
mysql> INSERT INTO test (a,b) SELECT a,b FROM test_current;
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 1
mysql> select * from test;
+-----+------+------+
| cnt | a | b |
+-----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
+-----+------+------+
2 rows in set (0.00 sec)
But when I did the query again
mysql> INSERT INTO test (a,b) SELECT a,b FROM test_current;
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 1
mysql> select * from test;
+-----+------+------+
| cnt | a | b |
+-----+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 4 | 1 | 1 |
| 5 | 2 | 2 |
+-----+------+------+
The auto increment just skipped cnt for 3. I want to know what is this about.
You can reset the auto_increment value to 1 every time before inserting values into your table:
ALTER TABLE `test` AUTO_INCREMENT = 1;
INSERT INTO test (a,b) SELECT a,b FROM test_current;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With