Jump to content

Auto Increment Question


DeathDisease

Recommended Posts

Hello, i'm wondering how to keep a accurate auto increment in MYSQL. For example say I had an ID field "INT" thats auto increment and I have 5 rows:

 

|ID 1| Row 1

|ID 2| Row 2

|ID 3| Row 3

|ID 4| Row 4

|ID 5| Row 5

 

Now say I delete "Row 4":

 

|ID 1| Row 1

|ID 2| Row 2

|ID 3| Row 3

|ID 5| Row 4

 

So my question is how do I keep the ID# with the corresponding row#? Is it possible to do this, and how so? If not is their another way to accomplish this?

Link to comment
Share on other sites

No, it's not possible. Treat auto-increment as if it didn't exist. Do not even think of messing with it. It's an essential part of making database tables relational. Messing with autoincremented values may render your database useless.

 

OK, so what do you really want to do and why?

Link to comment
Share on other sites

For the love of Ober! Why does every n00b have the insatiable desire to do this? I don't understand it.

 

We get this question about once a week. Its still a bad idea this week. Feel free to check back next week and see if it makes sense then.

 

Seriously though. This isn't what PK's are for. If you feel the need to store this info (it should be calculated on the fly), add another column like 'sort_order' and use that.

Link to comment
Share on other sites

You can't. Incrementing fields are not meant to be relied upon for such consistencies. If you need to rely upon consistent id's your more than likely going about something the wrong way.

 

What are you attempting to achieve?

Link to comment
Share on other sites

how do I keep the ID# with the corresponding row#?

You don't.

 

The physical location of a record is irrelevant. Your application should work even if they are scattered randomly throughout the table

 

ID 5 may stay at row 5 but when you add a new record it could take the place of a deleted one, so you then have

 

|ID 1| Row 1

|ID 2| Row 2

|ID 3| Row 3

|ID 6| Row 4

|ID 5| Row 5

 

If you want the records in ID sequence, use "... ORDER BY ID" in your query.

Link to comment
Share on other sites

No. In a HTML table I want to keep each table row numbered in a column to the left of the table data, so that table row one would be #1 and table row 2 would be #2... and so on. So that when a row is deleted or added that it will still be at the right count. If that makes ne sense.  :P

Link to comment
Share on other sites

Actually what you want is that the first row displayed shows as #1, the second shows as #2 etc. regardless of the record id (physical location in the database).  Just add a loop counter and display its value as you display each record.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.