I have a table which contains an auto incremented primary key id. If I delete the last row (highest id, for example id = 22) and insert a new row, the new id starts at 23. What should I do to start it with 22 again?
Example Table:
PrimaryKeyID Column 2 Column 3
Row 1 (auto-incr.) x x
2 x x
deleted row 3 x x
inserted row 4 (should be 3) x x
This is from MYSQL developer comment. See More https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
In order to reset the auto_increment, in a situation where some of the
most recently added rows were deleted, use:
ALTER TABLE your_table_name AUTO_INCREMENT=1234 //this is a demo number
Then future insertions will be numbered from 1234 again (unless you still had rows numbered greater than 1234, and then the future insertions will start from the greatest number + 1 ).
Execute a query to check that your auto_increment (SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE table_schema='database' AND table_name='table') is larger than max(audited_id) and perform a fake insert. You can put that logic with --init-file or on a trigger, please refer this article.
http://dev.mysql.com/doc/refman/5.6/en/innodb-auto-increment-handling.html#c12158
Related
If we delete row from MySQL table, and if there is some auto incremental column exists, after insertion it will be resumed incrementing by last value. That is if the last value was 1000, after deletion, incremented value will be 1001.
But what if I want it to increment that column (e.g. id) according max id each time before insertion. I know that it could be achieved by setting:
ALTER TABLE `tableName` AUTO_INCREMENT = MAX(`ID`) + 1
manually, but this sql should be called every time after the deletion or before insertion.
In MySQL table I am just inserting elements and fetching them to the java.util.List, and I only need to get data according some interval.
Getting according id is much faster than by limit.
e.g.
SELECT * FROM `TABLE_NAME` WHERE ID >=START AND ID <=END
is much faster than
SELECT * FROM `TABLE_NAME` LIMIT START, (END-START)
And getting size of the table is faster by MAX(ID) than COUNT(*)
Edit:
Regarding deletions, I only delete row from the end of the table or the rest from the id (including) that should be deleted, so that after deletion MAX(ID) and COUNT(*) will be equal.
The question is: Is there any way of setting "automated" sequential AUTO_INCREMENT in MySQL?
I created a table department with 4 columns and set deparmentid column to autoincrement. Now, after deleting 8 records out of 10, on adding the new record the value of departmentid is shown as 11 instead of 3. I truncated the whole table but again it is showing the same result on inserting the data. What should I do?
You can reset the counter with:
ALTER TABLE tablename AUTO_INCREMENT = 1
For InnoDB you cannot set the auto_increment value lower or equal to the highest current index.
This is how auto increment is working. It doesn't matter if you delete from the table. If you want to change the auto increment id you need to run
ALTER TABLE department AUTO_INCREMENT = 3;
the number of rows in my mysql TB (As shown in TB info) is 11093 where as the auto increment id (starting from 1) is 11361. Why is that so?
Deleted rows do not reset the AI index. Row count is the number of entries currently in your table, while the AI value is the number of rows you ever added to the table.
In my table I have auto increment id which is having its number like 1 to 66,440 and next number is 88846790 + increment from here till 88846900.
There is no records in between 66440 to 88846790
I want my next auto increment number to be 66441 but its taking 88846791, can you help me in setting next auto increment to 66441,
alter table tablename AUTO_INCREMENT=664441
should do
You can use ALTER TABLE to change the auto_increment value:
ALTER TABLE my_table AUTO_INCREMENT = 66441;
See the MySQL reference for more details.
Remember to reinsert all rows with an id higher than 88846790
If you don't remove rows with a higher ID than 66441,
the change in autoincrement does nothing.
Example:
ID Value
---- --------
1 C
2 A
4 D
50 A
51 C
If I want to change auto increment to 5 I need to remove or re-insert the 50 and 51 first. Because otherwise the next auto increment is gonna be 52 anyway.
Depending on how much and how important the data is, often the best thing to do is: Nothing
Because those primary ID's have relations to other rows, and maybe even web- urls based opn those IDs. This will all fail, unless you create some sort of script.
I had trouble getting this to work in phpMyAdmin, even inputting the query directly. If you browse the table you want to reset the index of, then click Operations, you should be able to set the AUTO_INCREMENT directly under Table options. Then just click Go and you're all set!
Rebutal to all those recommending ALTER:
You cannot reset the counter to a value less than or equal to the
value that is currently in use. For both InnoDB and MyISAM, if the
value is less than or equal to the maximum value currently in the
AUTO_INCREMENT column, the value is reset to the current maximum
AUTO_INCREMENT column value plus one.
-- https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
(That is from the 8.0 manual, but I believe that it has always applied; I think I discovered it in 4.0 days.)
Better than deleting and re-inserting:
Renumber the higher values:
UPDATE t SET id = id - 88846790 + 66440 + 1 WHERE id >= 88846790;
But then comes the hassle of renumbering references to this id. They can use a similar update:
UPDATE other_table SET t_id = t_id - 88846790 + 66440 + 1 WHERE t_id >= 88846790;
I have a column in a table that is auto incremented. Let's call it employee_id.
Let the initial value be 1 and let it be incremented by 1 for each insertion.
After inserting 10 rows, the auto incremented value becomes 10 (employee_id is 10).
Now if I manually insert 11th row with an employee Id as 15 , and then allow MySql's AUTO_INCREMENT to take over, will the next auto incremented value be 11 or 16 ?
It will be 16, bcs MySQL will update the value of AUTO_INCREMENT counter after each insert/update operation. This is for both most popular table engines, MyISAM and InnoDB:
http://dev.mysql.com/doc/refman/5.0/en/myisam-storage-engine.html
http://dev.mysql.com/doc/refman/5.5/en/innodb-auto-increment-handling.html
I tried it on localhost and after inserting 15th row manually (employee_id=10, name=John), it is going on by getting max auto_increment id.
Storage Engine of table is: MyISAM.
Next Auto_increament value will be 16. As Auto_increament will be updated after every insert/delete operation.