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.
Related
I have the following SQL code in PHP
$conn->query("INSERT INTO orders (Name,Email,phone) VALUES
('$name','$email','$phone')");
my database table has id,Name,Email,phone
insertion is successful but the id is always set to 2147483647 which is the max int(11)
so there is only one row in my table
ID-------------Name------- Email-------phone
2147483647--John--Smith--John#John.com--04524524
the id field is set as autoincrement
instead of being 1 or 2 it goe all the way to max (2147483647)
even if i make BIGINT or change the type mysql will insert the max ID increment
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. (quote from [ViralPatel][1]):
Note that 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.
If you are just bootstrapping your database, you maybe should consider emptying the table using:
TRUNCATE TABLE tablename;
The command above will also reset any automatic counters.
I have duplicate the table, data and structure,then delete the original one.
then rename table to replace, and voilá...
Autoinsert is OK
I need to reset the auto_increment value of a column in the database, I know that I can use: ALTER TABLE 'table' AUTO_INCREMENT = 1 but it is not working. I am using MySQL 5.6.14.
If it is an option you can simply truncate the table
From what I know you may not set the auto_increment lower than the highest value in your current table (protection against primary key conflicts)
If there is some rows in your table you can not reset.
Because autoincrement numbers are unique.
#ok gives you a solution but in this case the rows will be deleted.
Takes a copy of your rows truncate table and load theme again
You can only set AUTO_INCREMENT to a value that is bigger that the biggest id.
So if you have any row, and dont want to delete it, you can lower the autoincrement value, but just to some value higher than your last inserted row.
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 table with an auto incrementing column. I need to set one of the rows to 1000 and never touch it again, but now that I've set it to 1000, auto increment keeps starting at 1001 and refuses to start at 1. Is there any way to get around this?
You cannot:
To change the value of the
AUTO_INCREMENT counter to be used for
new rows, do this:
ALTER TABLE t2 AUTO_INCREMENT = value;
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.
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
Simple and short answer: you can't do this.
If you could, what would happen if you start your auto-increment at 1 and insert 1000 rows? The last couldn't be inserted due to "duplicate key"-error.
If you have to have a predefinded entry, with an id that never changes and is easy to remember, why don't you use 0 for that? Or, if you really need to use 1000, what's the problem with letting the other columns start at 1001?
Assuming you have no other row ID with 1000, you can insert the row to the bottom of the table, then you can simply use the update command:
UPDATE table.column SET id = 1000 WHERE id = current_id;
Assuming id is your auto-increment column. And current_id should be replaced with the id that the row is inserted at.
You can use MS Access that link to MySQL as external table, and you can change the auto increment table field value from MS Access via copy paste from Excel (first, you need to arrange the value of auto increment in Excel).
You could use the following statements:
UPDATE tbl SET id=1000 WHERE id=current_id;
ALTER TABLE tbl AUTO_INCREMENT=1001;
Currently every time I add an entry to my database, the auto increment value increments by 1, as it should. However, it is only at a count of 47. So, if I add a new entry, it will be 48, and then another it will be 49 etc.
I want to change what the current Auto Increment counter is at. I.e. I want to change it from 47 to say, 10000, so that the next value entered, will be 10001. How do I do that?
You can use ALTER TABLE to set the value of an AUTO_INCREMENT column ; quoting that page :
To change the value of the
AUTO_INCREMENT counter to be used for
new rows, do this:
ALTER TABLE t2 AUTO_INCREMENT = value;
There is also a note saying that :
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.
See manual for ALTER TABLE - this should do it:
ALTER TABLE [tablename] AUTO_INCREMENT = [number]
you can get that done by executing the following statement
ALTER TABLE t2 AUTO_INCREMENT = 10000;
So next Auto Increment key will start from the 10001.
I hope this will solve the problem
You can also set it with the table creation statement as follows;
CREATE TABLE mytable (
id int NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (ID)
)AUTO_INCREMENT=10000;
Hope it helps someone.