MySQL auto_increments always insert 2147483647 - mysql

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

Related

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.

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;

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.