Changing the current count of an Auto Increment value in MySQL? - mysql

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.

Related

MySQL auto_increments always insert 2147483647

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

Insert after truncate start from 1; but insert after delete resumes from previous value

In my table there is column name id which is auto increment integer. When I truncate table and then try to perform insert query it starts from 1, but if I do delete operation and then try to insert then it just resumes from previous value. Why does insert query performs differently for delete and truncate. It should always start from 1 if there is not entry in table.
Example :
(1) Original Table
(2) delete all rows and then insert
(3) truncate the table then insert
The current max value for the autoincrement function is stored in the table definition (you can see it when you run show create table idle.
When you delete all rows from the table, the autoincrement value will stay the same. When you truncate the table you basically drop the table and recreate it, which resets the the autoincrement value to 0.
Hope this helps.
I think you are about to reset auto increment, run this query after delete query:
ALTER TABLE tablename AUTO_INCREMENT = 1 ;
NOTE: be careful about duplicate keys, if you delete some ids and reset ids to 1, while id=2 or n exists!
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.
some other actions and notes mentioned in this topic
you give AUTO INCREMENT in id that's why it is behave like this.whenever you truncate its reset the identity while in case of delete operation its start from last value of your id. if don't want this behavior remove AUTO INCREMENT index from id field.

Resetting the AUTO_INCREMENT value of a column in MySQL

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.

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.

Reset MySQL auto_increment when a large number already exists?

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;