Reset the table Auto Increment and make it apply to the table - mysql

I have a table with around 10k rows which I've imported. The ID is a significant column to my application, and it has to be ordered. Currently, I got something like: 1,2,3,4,5....5789,9275,9276.....
It jumped from 5789 to 9275. Is there any way I can reset the Auto Increment but also make it apply to the table? which means, now it will start giving them IDS all over again from 1 to 10k
Thanks!

ALTER TABLE <tablename> AUTO_INCREMENT=<new_value>;
Of course you need to fix the high IDs and all references to them manually.
However, why do you care? Does it really matter if there's a hole in the IDs? If yes, you might want to use a separate column that's always set to MAX(col) + 1 instead of an AUTO_INCREMENT column.

You can certainly reset the auto_increment value to be whatever you want by simply issuing this query:
ALTER TABLE <tbl> AUTO_INCREMENT = <n>;
where tbl is your table name and n is the value to start it at. However, if you have existing IDs in that table already, I believe it will simply set the next inserted items ID to be max(id) + 1 of the ID column

Related

Change AutoIndex number Mysql

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;

General MYSQL Database understanding

Lets say database has a table which has only two columns of ID which is Auto increment and name which is text. When we first add 2 names, then delete both of the names, next time again enter another name, the ID count starts from number 3 while it should start with number 1.
Question is that is there any way to reset the ID so that it starts from 0 once all values of ID's are removed instead of continuing increment from the last ID number that was removed?
Here's the SQL query to reset the AUTO_INCREMENT value:
ALTER TABLE tablename AUTO_INCREMENT = 0
You can use Truncate.
TRUNCATE TABLE yourtable;
It is similar to deleting all rows of your table but has some differences including resetting auto-increment to 0.
Yes you can
ALTER TABLE mytable AUTO_INCREMENT = 0
But why bother? There are plenty of numbers in the universe or even in 32 bits!
I think this will do what you are looking for.
ALTER TABLE table_name AUTO_INCREMENT = 1;
ALTER TABLE yourtable AUTO_INCREMENT = 1
There sure is!
ALTER TABLE 'mytable' AUTO_INCREMENT = 0;
This will reset the auto increment back down to 0 and continue from there.
A general note from MySQL-dev:
You cannot Reset the counter to a value less than or equal to any that have already been used. For 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 plus one. For InnoDB, if the value is less than the current maximum value in the column, no error occurs and the current sequence value is not changed.

Fix following id's when a row is deleted

I have a table with some rows, each row has a unique key. When a row is deleted from the table, all rows that are below this row should be 'moved up'. Is there some built in function in MySQL that does this or should I just do it with PHP or perhaps UPDATE table SET id=id-1 WHERE id > deletedid?
Using the last one seems a bit messy.
What would be the best way to do this?
Why do you want to do this? I know it's ugly to have holes in your unique ID sequence, but the downside of invalidating any references to IDs from outside the database is normally very much greater. The normal thing is to just accept the sequence won't be contiguous. If these represent a sequence, consider just sorting by the order rather than expecting the N'th value to have value N (any sort of iteration should provide its own index somewhere for this use).
If the value is one you set yourself, and you definitely want to keep it as having values from 1 to N (N="number of rows"), and you want to keep the sequence of values even if they're not in the order the rows were inserted, then "UPDATE table SET id=id-1 WHERE id > deletedid" is probably the best answer.
If the value is an auto_increment field, and you don't care which numbers go with with rows as long as each row has a number from 1 to N, you can alternatively do ALTER TABLE DROP COLUMN 'columnname' and then ALTER TABLE again to add the column again, and the database will regenerate the ids from 0. (Not necessarily in the same order, though it often is.)
There may be a way to renumber only the rows after that point, but (according to a quick google) it doesn't look like there's anything easier than what you're already planning.
First you have to ensure that the column is not a foreign-key for any other table.
Then you can try this (I am not 100% positive it will work):
DELETE FROM
MyTable
WHERE
id = deletedid;
UPDATE
table
SET
id=id-1
WHERE
id > deletedid
ORDER BY
id
As stated in mysql docs:
If the ORDER BY clause is specified,
the rows are updated in the order that
is specified.
and in this way you ensure uniqueness of the field.

Auto Increment Problem in Mysql

I created a table and set a field to auto increment some thing like this:
CREATE TABLE t1(id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT) ENGINE = MyISAM AUTO_INCREMENT = 123456;
But to some reason i deleted some of the rows in the table.
Now the question is when i insert new rows in the table the new rows should be assigned id's of the rows which have been deleted rather than assigning new id's.
I do not want to reset all the id's
How can i do this??
Help appreciated:)
Sorry to say, but that is not the use of AUTO_INCREMENT. If you want to re-use id's, then you would have to write your own trigger functions, and doing this is generally considered bad practice.
Imagine you were on id 50,000, and deleted an entry with id 1... would you really want the next record you add to re-use id 1?
The whole point of AUTO_INCREMENT is to auto increment...
You can explicitly assign these ids though and mysql will allow it.
You are going to have to do this manually rather than rely on MySQL to do it for you. The AUTO-INCREMENT flag keeps an integer that is incremented upon every insert statement and is assigned as the PK of the subsequent insert. Unless you want to write an update trigger that resets this value to the lowest non-used integer, I would suggest processing this in a server-side scripting language.
In any case, though, why is using the auto increment value a problem?
To reset the autoincrement value, you can use
ALTER TABLE t1 AUTO_INCREMENT=1
The next inserted record will use ID 1.
This might be something you're after.
alter table Users AUTO_INCREMENT=0;
This will reset the auto_increment back to 0 + whatever the current highest id is.
if you have 30, your next entry would be 31

Reset/compact mysql AI ID field?

I have a fairly large table with about 250k rows. It has an auto incremented ID column that is really sort of useless. I can't just get rid of the column without rewriting too much of the app, but the ID is never used as a foreign key or anything else (except simply as an identifier when you want to delete a row, I guess).
The majority of the data gets deleted and rewritten at least a few times a day (don't ask! it's not important, though I realize it's poor design!), though the total count of the rows stays fairly uniform. What this means is that each day to AI # increases by a quarter million or so.
My question is this: in several years' time, the ID column will get too large for the INT value. Is there a way to "reset" the ID, like an OPTIMIZE or something, or should I just plan on doing a SELECT INTO a temp table and truncating the original table, resetting the ID to 0?
Thanks
If you have the id as integer you can have 2^32 / 2 (2.147.483.647) rows, if is unsigned integer duplicate to 4.294.967.295, no worry 250.000 in nothing, if you want more, use unsigned bigint (18.446.744.073.709.551.615) :P
For reset the auto_numeric position:
ALTER TABLE table AUTO_INCREMENT = 1
Either change the datatype of ID to BIGINT and adjust your program accordingly, or if you're clearing everything out when you delete data you can use TRUNCATE TABLE TABLENAME which will reset the sequence.
Easiest and fastest :) Just drop the index, set autoincrement=1, and add it back :)
ALTER TABLE yourtable DROP id_field;
ALTER TABLE yourtable AUTO_INCREMENT=1;
ALTER TABLE yourtable ADD id_field INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id_field);