Understanding MySql Auto_Increment - mysql

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.

Related

how to auto increment with 1 after deleting data from table

i'm deleting previous data and trying to insert new list of data,id values are keep on increment because of auto-increment. is it possible to have new auto increment id with 1 ?
and i tried with ALTER TABLE table AUTO_INCREMENT = 1; its not working for me.
Use this query while deleting your old 20 Records.
truncate table YourTableName;
It will reset the database structure and if you insert new record it will start from 1(one) id again.
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.
And also read this article Link
Just visit this question

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

Set AUTO_INCREMENT in all tables within a MySQL DB to the largest inserted increment value

Is it possible to set AUTO_INCREMENT value for all tables within a database to the latest insert value?
UPDATE
For example if my table had 1000 records with increment values from 1..1000 and I have deleted last 900 records the last increment value in the field would be 100. I would like to set the AUTO_INCREMENT to the 101. And this for all tables within DB.
Is it possible to set AUTO_INCREMENT value for all tables within a database to the latest insert value?
The value of AUTO_INCREMENT is actually the next available value, i.e. maximum value plus 1.
If your tables use the InnoDB engine then all you have to do is to restart the MySQL server (not the computer but just the MySQL service).
The documentation explains how the value of AUTO_INCREMENT it stored (in memory, not on disc) and restored when the service starts:
To initialize an auto-increment counter after a server restart, InnoDB executes the equivalent of the following statement on the first insert into a table containing an AUTO_INCREMENT column.
SELECT MAX(ai_col) FROM table_name FOR UPDATE;
InnoDB increments the value retrieved by the statement and assigns it to the column and to the auto-increment counter for the table. By default, the value is incremented by 1.
This is automatically done.
Quoting the manual:
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.
AUTO_INCREMENT actually remember the last number it insert even if the data has been deleted. So if your AUTO_INCREMENT number is less than 100, you can just temporary insert a blank data with AUTO_INCREMENT number = 100 and then delete it. The next time you insert a data, it will go to 101.
You can use this query to reset the AUTO_INCREMENT number to the current highest one in the table.
ALTER TABLE table_name AUTO_INCREMENT = 1;

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.

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.