how to auto increment with 1 after deleting data from table - mysql

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

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.

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.

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 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;