phpMyAdmin - autoincrementing ID - mysql

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.

Related

Switch values in MySQL table

I have a table in MySQL with a field "Ordering" These are just auto incremented numbers. Now I wonder if there is a query to change the values from the last to the first...
So the entry with ordering 205 should become 1, 204 -> 2 and so on...
It's actually not an auto-increment. The problem is I started adding projects from the current website. From page 1 to page 20, but the first item on page 1 is the latest. The way I added the new projects, the newest is on the last page..
If the ordering field is switched, the new items added will be correctly numbered again and added to the front page. It's just a wrong way I started adding old projects...
Structure
Examples of the content
I can't comment due to limitations, but i really agree with #Abhik Chakraborty.
You don't want to do this. Just use the order by as he suggested.
Example:
SELECT * FROM tableName
ORDER BY columnName DESC;
Just in case you would like to know more about it: http://www.w3schools.com/sql/sql_orderby.asp
Try this as one statement call:
SET #MaxSort := (SELECT MAX(Ordering) FROM MyTable);
UPDATE MyTable t set t.Ordering = (#MaxSort + 1 - t.Ordering);
This will work if field doesn't have unique constraint.
But this field, should not be an auto_increment field at first place. Auto increment is increasing NOT decreasing counter. Except if you just try to fix existing data and the new records will be increasing.
Additional explanation
Thanks for pointing it out. Multiple query inside single query statement doesn't work with php_mysqli and it is not used because of potential MySQL injection attack if servers allows it. Maybe you can setup PHPMyAdmin to use PHP PDO.
I can use multiple queries, but I'm using PHP PDO or DBeaver database manager.
I can only suggest to supply MaxSort manually (since this is one time job anyway):
UPDATE
MyTable t
set
t.Ordering = 254 - t.Ordering + 1;

Lookups inside tables.. still 'evil' if I use bound columns? What is less evil?

What if I have it where I (in the design mode for my table, in a particular field) use the combo box to lookup the ID and the value and then make the value my bound column?
I understand I'm losing the ability to see relationships that I've made in this way on the relationships database tool. Am I losing anything else?
In other articles I've researched, they talk about queries not working correctly or the database being too rigid/hard to change after the fact. I don't necessarily see those problems.(although I'm still learning Access! =) )
When I build a Query to test if it returns the ID or the value, it returns the value, so what's the real problem here?
If I understand you correctly, you have a domain table that looks something like this:
DomainTable
-----------
DomainID
DomainValue
The rows of your table might look something like this:
1 Male
2 Female
3 Other
And you have another table where you want to include either the DomainID or the DomainValue.
Generally, you would use the DomainID. This allows you to make a change to the domain value without having to make the change in hundreds of places.
On the other hand, if you want changes to the domain values to not be reflected in your other tables, you would use the DomainValue. This is important when creating a log file, as one example.

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.

Fixing auto-increment ID in MySQL table

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.