Trying to reset the auto increment value of a table - mysql

First off, I've gone through the normal steps to try and reset a value, GUI and CLI, it says x rows affected.
I have all the permissions I would need to do this(any amazon specific ones i don't know about aside), locally the queries work fine.
Is there a way to modify my tables auto increment number so that it starts at say 19, and not 1,000,021?
Queries I've tried.
ALTER TABLE X AUTO_INCREMENT = 19
I have all permissions granted to all tables, and I have even tried granting ALTER specifically to my user on the specific db.table.
Any insight or a push in the right direction would be lovely.

Your command:
ALTER TABLE X AUTO_INCREMENT = 19
will work only if your table does NOT have row with id 19 present. If you try to insert new row which would cause insertion conflict, it will automatically reset to maximum existing value of your auto-increment column + 1. In other words, if you have rows with id 19 and 1000020 (maximum), it will automatically reset to 1000021 on any attempt to insert.

Related

MySQL incrementing Primary Key on INSERT INTO .. ON DUPLICATE KEY UPDATE [duplicate]

Note: I'm new to databases and PHP
I have an order column that is set to auto increment and unique.
In my PHP script, I am using AJAX to get new data but the problem with that is, is that the order skips numbers and is substantially higher thus forcing me to manually update the numbers when the data is inserted. In this case I would end up changing 782 to 38.
$SQL = "INSERT IGNORE INTO `read`(`title`,`url`) VALUES\n ".implode( "\n,",array_reverse( $sql_values ) );
How can I get it to increment +1?
The default auto_increment behavior in MySQL 5.1 and later will "lose" auto-increment values if the INSERT fails. That is, it increments by 1 each time, but doesn't undo an increment if the INSERT fails. It's uncommon to lose ~750 values but not impossible (I consulted for a site that was skipping 1500 for every INSERT that succeeded).
You can change innodb_autoinc_lock_mode=0 to use MySQL 5.0 behavior and avoid losing values in some cases. See http://dev.mysql.com/doc/refman/5.1/en/innodb-auto-increment-handling.html for more details.
Another thing to check is the value of the auto_increment_increment config variable. It's 1 by default, but you may have changed this. Again, very uncommon to set it to something higher than 1 or 2, but possible.
I agree with other commenters, autoinc columns are intended to be unique, but not necessarily consecutive. You probably shouldn't worry about it so much unless you're advancing the autoinc value so rapidly that you could run out of the range of an INT (this has happened to me).
How exactly did you fix it skipping 1500 for ever insert?
The cause of the INSERT failing was that there was another column with a UNIQUE constraint on it, and the INSERT was trying to insert duplicate values in that column. Read the manual page I linked to for details on why this matters.
The fix was to do a SELECT first to check for existence of the value before attempting to INSERT it. This goes against common wisdom, which is to just try the INSERT and handle any duplicate key exception. But in this case, the side-effect of the failed INSERT caused an auto-inc value to be lost. Doing a SELECT first eliminated almost all such exceptions.
But you also have to handle a possible exception, even if you SELECT first. You still have a race condition.
You're right! innodb_autoinc_lock_mode=0 worked like a charm.
In your case, I would want to know why so many inserts are failing. I suspect that like many SQL developers, you aren't checking for success status after you do your INSERTs in your AJAX handler, so you never know that so many of them are failing.
They're probably still failing, you just aren't losing auto-inc id's as a side effect. You should really diagnose why so many fails occur. You could be either generating incomplete data, or running many more transactions than necessary.
After you change 782 in 38 you can reset the autoincrement with ALTER TABLE mytable AUTO_INCREMENT = 39. This way you continue at 39.
However, you should check why your gap is so high and change your design accordingly. Changing the autoincement should not be "default" behaviour.
I know the question has been answered already.. But if you have deleted rows in the table before, mysql will remember the used ID/Number because typically your Auto increment is Unique.. So therefore will not create duplicate increments.. To reindex and increment from the current max ID/integer you could perform:
ALTER TABLE TableName AUTO_INCREMENT=(SELECT max(order) + 1 FROM tablename)
auto increment doesn't care, if you delete some rows - everytime you insert a row, the value is incremented.
If you want a numbering without gaps, don't use auto increment and do it by yourself. You could use something like this to achive this for inserting
INSERT INTO tablename SET
`order` = (SELECT max(`order`) + 1 FROM (SELECT * from tablename) t),
...
and if you delete a row, you have to rearange the order column manually

SQL auto_increment id randomly jumping after switching servers

I developed my website on Godaddy then moved it over to MediaTemple. After doing so when a new entry is inserted on this table the auto increment id is no longer in increments of 1. The first ten entries were added while on the old server and are 1,2,3,4,5,6,7,8,9, and 10. After moving the database to the new server the next id was 55, then 108, 136, 182, and 190.
This only happens with this one table and happens when an entry is inserted via php/pdo or phpmyadmin. The other tables still add ids in increments of one.
Here are some screenshots of the table's structure.
drive.google.com/file/d/0B9Tpq5uOpTLFakt2N3JlYk9Lams/view?usp=sharing
drive.google.com/file/d/0B9Tpq5uOpTLFRW9DNlJUa2pld0k/view?usp=sharing
drive.google.com/file/d/0B9Tpq5uOpTLFOGdybXZHWTE5dE0/view?usp=sharing
I understand that your ##auto_increment_increment value is getting changed from somewhere. So my suggestion is that please check it by using this command,
SELECT ##auto_increment_increment;
And if it returns 1 then its fine if doesn't then set it like,
SET ##auto_increment_increment=1;
Also you can check it in Mysql configuration file, provided that you have permissions to access that file \mysql\bin\my.ini
Also you can change the seed value of AUTO_INCREMENT by,
ALTER your_table AUTO_INCREMENT = 1;
Thank you.
As shown from the export structure syntax the auto_increment was set to 267 somehow.
I used the query...
ALTER TABLE table_name AUTO_INCREMENT=191
And this solved the issue. 190 was the last id so 191 would be next. Did a few tests and it is back to auto increments of one. I can only guess that the ids were doing big jumps in an attempt to reach 267 and that they would have went back to increments of one after reaching 267. Just a guess.
Thanks to those who commented.

Setting an auto-incrementing field back to its original

I have a SQL database with a table inside called members and inside that there are some columns, one being an ID which auto-increments.
However I have done a few tests, and the auto-increment does work. But even after deleting the tests the auto-increment will not start from 0 again.
How do I make it will start back from 0 rather than carry on from about 17 or something...
EDIT:
I have worked out the answer:
In the "Operations" tab in phpMyAdmin there is a section called Table Options.
In there you can edit where the auto-increment continues from.
Assuming You're using MySQL:
To reset the next value of *auto_increment* column, you need to use ALTER TABLE statement in the following form:
ALTER TABLE my_table AUTO_INCREMENT=123
(Where "123" is the new next value)
If u are using postgresql u have following sql statement to alter sequence
ALTER SEQUENCE table_name_id_seq RESTART WITH 1
for example, if u have table called users, then u should do
ALTER SEQUENCE users_id_seq RESTART WITH 1
where 1 is the new sequence.

auto increment not starting from 1

I am inserting some data to a database and I have an id column which has auto increment. I updated my xampp server yesterday and now the auto increment is starting from 4, 3, 5 in different tables. It used to work fine before. I did not delete any rows from the table it just starts from those numbers. What is wrong?
ALTER TABLE tablename AUTO_INCREMENT = 1
This will reset your auto increment to start from 1
If you really want to reset this, in phpMyAdmin, open this table, go to Operations and change the value for AUTO_INCREMENT.
You can reset your auto increment ID from the following Command:
ALTER TABLE TABLE_NAME AUTO_INCREMENT = 1;
This will start from 1.
OR
go to LOCALHOST/PHPMYADMIN enter:
select your DB
after choose the specific table than go to Operations an set a number to the Auto increment.
If you really want to reset this, in phpMyAdmin, open this table, go to Operations and change the value for AUTO_INCREMENT.----- this is working for me, i have just done this,
This can be a consequence of using a Galera cluster. By default, it will automatically adjust the auto_increment_increment and auto_increment_offset variables according to the size of the cluster, and when the cluster size changes. This avoids replication conflicts due to auto_increment.
The short answer is not to depend on auto_increment numbers being subsequent, without gaps.

Mysql 5.1.42 alter table auto_increment = 0 doesn't work, truncate does

For my automated acceptance tests, I want inserts to start with id=1.
I achieved this on one PC (XP 32bit, mysql 5.1.something) with (after deleting all rows from the table), "alter table tableName auto_increment = 0".
I'm now setting up a new PC (Windows 7 64bit, mysql 5.1.42), and this command seems to have no effect.
I can see in the information_schema.tables table that the auto_increment value is not changed back to 0 --- it just keeps going up. If I try to change the value in that table directly, I'm told that access is denied to 'root'#'localhost'. Does this perhaps give a hint to my problem?
Other stackoverflow people had suggested that "truncate from tableName" is a good alternative. I'm happy to report that this works. But does anyone know why the "alter table" command won't reset the auto_increment?
Thanks!
NOt sure why it worked on one server, and doesn't work on the other, but the MySQL manual states (quoting, emphasis mine) :
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.
Maybe that's the cause of the problem : you are trying to put the auto_increment counter back to 0, but it's already higher than that value -- and as you cannot reset it to a value that's less than any value that's already been used, it doesn't work.