Autoincrement not working properly in table - 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;

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 PrimaryKey AutoIncrement

i have the following problem:
I make an insert to a table. The primaryKey is auto incremented and is an integer.
In one table i have an index of 2345 and suddenly it is updated to 10000.
In another table i have an index of 263564 and it is updated to 1000000.
Does anyone has an idea?
If your inserted value contains field with autoincrement, your key will be updated.
You can change your auto increment value
ALTER TABLE tbl AUTO_INCREMENT = 100;
http://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html
Also, here is two system variables, which controls behavior of autoincrement.
auto_increment_increment controls the interval between successive column values.
auto_increment_offset determines the starting point for the AUTO_INCREMENT column value
http://dev.mysql.com/doc/refman/5.6/en/replication-options-master.html#sysvar_auto_increment_increment
It was an internal problem. Some Testcases manipulated the Id.
Thanks for your help.

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.

Changing the current count of an Auto Increment value in 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.