Fixing auto-increment ID in MySQL table - mysql

Hey I was wondering how I could fix my auto increment ID... it seems that everytime I add a row to the table, the ID is 1 off. So here's an example.
My table currently looks like this:
Now, as you can see, the last element jumps from 198 to 200. I want this one to be 199.
So say I manually change that last value(200) to 199, it will obviously work, but the next time my script adds a row to the table, it will be on off again(201). Any idea how I could fix it? Thanks for any help, sh042067.

Jim's right that you should find out the root cause of the 'missing' id.
But if you really just want to re-set the auto-increment counter, you can do this:
ALTER TABLE my_table AUTO_INCREMENT=200;
That should make the next auto-increment id value 200. (Assuming it's MySQL)

This probably happened because you removed a row from your table and then added a new row afterward. Is there a reason that you need syncrosy for this table in your id column? Are you planning on removing, replacing, and/or adding new rows. If so, this column will not remain in sync and you'll find that whatever steps you take to fix this problem (could be as easy as dropping the column and recreating it) will need to be done as often as you are removing and adding data.
In short, we need more information about what you're trying to accomplish.

Related

sql move row from one table into another

I know how to do this, but I am not sure if it is wise, so I ask: I have one table that stores any issues with software that we use at work. If the problem becomes resolved, should I move that row to a resolved issue table, or should I only insert the issue's table pk, and whenever I query open issues use an outer join? Just looking for industry standard on this.
I think you should take one column with name status and update this column as per your choice .and use trigger to maintain this table history .
Moving rows around is almost always a bad idea. If you add additional information regarding resolved issues (e.g., who resolved it, when was it resolved, etc.), having an additional "resolutions" table with a foreign key to the "issues" table might be a good idea. Otherwise, I'd just add a boolean field is_resolved to the "issues" table and set it to true when the issue is resolved.
Maybe add a column with a boolean: "Resolved". Set to true when the issue is resolved and find all resolved rows with "WHERE resolved=true".

Force auto increment to follow another suequence

I have a table containing invoices. Those invoices are inserted via a VB.NET program I have written. Using auto increment my invoices get their invoiceid.
I now have invoiceid's like
1055
1056
1057
...
I also have another database (from another bought software program) that generate invoices, and I use a cron job to import certain invoices into my main table, but they get the prefix 99. Those invoices do not have the same sequence. I do not use auto increment to number them, but I use the original ID (from the other database) + the 99 prefix. In my main table I have invoiceid's from the secondary database like this:
992013055
992013064
992013078
So, this makes that my main table looks like this:
992013055
1055
1056
992013064
1057
992013078
...
Now the thing is, I want the auto increment only follow the 'main' sequence. So in the example above, the next auto increment value should be 1058, but using normal auto increment it would be 992013079.
Is there a way to force this? Or should I "reset" the auto increment value after the cron job, something like "ALTER TABLE invoices AUTO_INCREMENT=bla +1". I guess 'bla' should be a new select to get the highest value in the column that does not start with 99, not sure how to do this. Off course when I get to invoice 9900, this would be a problem.
Thank you
i would create another column or table that links in this external id and do not try to layer them in to your id column like you are attempting.
for instance add a column called external_id and simply populate that one with the 99 values - and keep your autoincrement value chugging along properly with your regular numbers.
No you can't. Even if you tried the ALTER TABLE invoices AUTO_INCREMENT=bla +1 command, it would not work, as MySQL makes sure the value of AUTO_INCREMENT is higher than the highest value in the column.
Basically you shouldn't use the value in your autoincremented column for anything else than primary key, so that you don't really need to care about what these values are and what sequence they follow. After all nobody sees them and they're only used by computer.
If however you used these values for something that's visible for the end user... well your users will just need to adjust to the fact, that their invoces start from 99 now. Either that, or you need to renumber them, which is hell of a work and likely to introduce errors to your data.

Create columns that autoincrement with name in a MySQL Database

I know that this might seem like a strange question, but let me try and explain it. I have a database table called 'plan' and in it the first column is called 'username' and the columns after it are called 'question1', 'question2' and so on. I now need to add a hundred or so more columns named like this, but it would be nice to have a sql statement that would automatically do that for me.
I know this wasn't set up in the best way, but if you have a solution, please let me know :)
There isn't any SQL command or feature that would do this automatically; sure you can generate the alter table statements and add the columns programmatically; however, your design would be terribly flawed.
Instead of adding columns, you should create a table containing the question, the user_id (or username, whatever is the PK) to hold the records. If you need to identify a question by number (or ID), simply add another column called question_id.
Write the query in sql to excel. Seperate the incrementing number. Drag down until excel row 100. Hard to explain but i guess you ll figure it out. You'll have 100 incrementing add column sql statements. copy paste run it on a query tool.

phpMyAdmin - autoincrementing ID

Quick question regarding phpMyAdmin and autoincrementing. If I add a product and it is assigned id 1, then I delete it before adding another, the next one is given the id 2 even though I deleted id 1. I hope this makes sense.
Does this matter?
No, this is normal (My)SQL behaviour.
Its not the way it works. The auto increment value is stored in a variable, and it only adds one to that, instead of checking the largest one and adding one to that. Its the normal behavior, it looks messy but it works fine.
Consider the example where you had 1000 rows, and deleted the row ID=1. Would you expect the next entry to be given an ID of 1001 ? or 1 ?
This is working as intended, though if you absolutely must, you can manually set the value just as you would any other column in MySQL.

Saving auto increment in MySQL

I am trying to sync between 2 tables:
I have active table where has auto_increment, and I have archive table with the same values.
I would like both ID's to be unique (between the tables as well) - I mean, I would like to save auto incremenet, and if I UNION both table I still have uniqness. How can I do that?
Is there a possibility to save auto increment when mysql is off?
First off, this sounds like a bad idea - you shouldn't have an auto_increment in your archive table, since all the data is presumably copied directly from the live table (including its IDs).
However, here are some hacky solutions:
You can change the current AUTO_INCREMENT of a table using:
ALTER TABLE tbl_name AUTO_INCREMENT = some_new_value
Set it to a large offset for one of the tables, and you'll be safe for a while. Except when you reach the offset, having forgot this little hack, the whole house of cards will fall on your head.
You can simply add an offset by a constant when selecting (e.g. SELECT id + 1000000 AS id) in one of the tables. Still a hack, but at least it's closer to the surface, and when you eventually reach an overlapping area, it's easier to fix.
Finally, you can select the maximum ID from one table, then offset all the IDs in the other table by this value during the same select. Make sure you have a large enough datatype for the ID though.