SQL auto_increment id randomly jumping after switching servers - mysql

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.

Related

Duplicate entry for key 'PRIMARY' - even though it is set to AUTO_INCREMENT

Bit of a strange one - I haven't changed the config, database, PHP code for this site for about 8 years. The whole time the guy I made it for has been adding, removing, changing stock with no issue, and suddenly today gets this error:
Duplicate entry '2541' for key 'PRIMARY'
executing (inserted generic values for the texts):
INSERT INTO stock (id,title,category,description,price,last_update,sold) VALUES(NULL,'Item name','72','Item description','0',1613723525,'no')
Searching around seemed to suggest this was a common problem when the primary key is not set to auto increment - makes sense. However, checking it out through phpMyAdmin, it definitely is.
Is there something in the index being set to primary rather than unique?
There are 5 other tables on the database but none are linked (as in hard links through the SQL, PHP handles all the cross-table stuff).
I checked and indeed there IS an item in the stock table with ID 2541 already. So why is that NULL AUTO_INCREMENT value converting to an existing id?
EDIT
I noticed that a table I created more recently (via MySQL Workbench probably) has a different setup for the indexes, with the addition of an id_UNIQUE index - do I need one of these on the stock table that is causing issues?
Based on your description and your comment "Interestingly, each time I refresh the ID it is attempted to insert (and failing on) increments by 1", I suspect that somehow the seed for the autoincrement for that table got changed to some value that was inserted at some time before.
How exactly that could happen I don't know.
Now, each time you attempt to insert a record this internal counter increments, so you see in the error message that the number increases (2541, 2542, ...) When you attempt to insert a row the internal counter increments regardless of whether the transaction is committed to the database or not. In your case the insert operation is rolled back, because the generated value violates the unique constraint and the internal counter keeps growing.
To change the seed you can run the following statement:
ALTER TABLE stock AUTO_INCREMENT=1234567;
You'll need to set it to the current MAX value that exists in the table, so that new entries that the system attempts to insert do not conflict.
See also How to set initial value and auto increment in MySQL?
This answer shows how to change the autoincrement seed in MySQL Workbench and in PhpMyAdmin. Maybe somebody did this accidentally and didn't notice.

Rows keep showing #DELETED

Everytime I enter data into one of the SQL linked tables, some of the rows keep saying #deleted no matter what I do. The data is fine in the actual SQL server but not within Access. I never have used Access before so I have no idea what I'm doing. Please use the most non-tech savy dialog as possible... This is all so new to me and I am not good with technology at all.
I have tried refreshing the tables by going to the "Linked Table Manager" thing but that hasn't helped. I tried completely deleting the data from both Access and the SQL Server, re-entering it into the SQL server, and creating a new linked table within Access. I have tried exporting the data into Excel from the server and importing it into Access. None of it has worked. It's only the first 10 rows of data though... The rest of the table is completely fine and all the data has similar structure so I don't know why only the first 10 rows are being affected.
Ok, there are 3 things that often cause this.
1 - Make sure the sql table has a PK column. This is often (useally) a autonumber (incrementing by 1 integer column). So when you create the column in sql server, set it as primry key (a button in the menu can be hit to set PK using the sql manager). Then change in the property sheet the column to identify "yes" and it will set the starting number (1) and the increment (1) for you. Now add the other columns.
So Access needs a PK column.
If above was not your issue, then next up that is common is if you have a "bit" column in sql eerver. These can't be null, or access goes crazy. so if you have a bit column, then MAKE sure you set the default for that in the sql table designer as (0).
If the above don't fix your issue? Then number 3 on the list is to add what is called a row version column to the sql table. Simply add a timestamp column (this is NOT a date column, but is a time stamp row).
In ALL of the above cases, after you make the change to the sql server table, you have to re-link the access table. It is sufficient to right click on the table in Access, choose linked table manager, and then check box the table in queston, and hit ok. The link will be refreshed for you.
So the above are the 3 main issues. In most cases, the PK is the issue. However, if the table on SQL also has a trigger (that inserts) to other tables, then that table trigger has to be changed - but lets take this 1 step and soluion at a time.
As a general rule, Access needs a PK column when working with sql server. If you have that, then check the null "bit" issue - sql server tables need a default setting of 0 for those columns, and if they are null, then Access don't like that.
If both above issues are NOT your issue, then adding a column of timestamp to the sql table will fix this.

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.

Trying to reset the auto increment value of a table

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.

mysql auto_increment by 5?

I'm running into a really, really, really weird problem with mysql.
I have a primary key, "id". It's set to auto increment. Problem is, first entry started at "3". And every new entry increases by 5, so the next entry's id is 8, the next is 13, then 18, so on. This is stupid. Why isn't it just incrementing by 1, like it should be? And why is it starting at 3???
Is there some setting somewhere I'm missing? I'm using phpmyadmin, if that helps.
There's a my.cnf configuration for that: auto_increment_increment. It's used for master-master server setups to prevent the same key from being defined twice by two different servers. So using that coupled with auto_increment_offset, it allows each server to always generate unique ids...
So, from what you're describing, it sounds like you have this:
auto_increment_increment = 5
auto_increment_offset = 3
The auto increment is probably set to 5. Try:
ALTER TABLE YourTable AUTO_INCREMENT = 1;
You can retrieve the current setting with:
SHOW TABLE STATUS LIKE 'YourTable'
See the MySQL docs for more details.
it appears the table was created with an increment set to 5. you can change it back to one with the following:
ALTER TABLE tbl AUTO_INCREMENT = 1;