Question

I am having a dilemma. I have a table with a column called ID and it is the primary key. It is in automated increments so 1, 2, 3, and so forth and the table is 1-8.

A former programmer deleted row 7 and so now there is a gap between 6 and 8. I need to change the value of 8 to 7.

I have tried several things:

  • I can not edit ID 8 because it is an identity column

  • I have tried to query:

    set identity_insert mytable ON

then delete the row and re-add and edit the ID as other threads have recommended. However, the automated number kicked in, and now it is row 9 and I still can not edit the ID. I am not sure what I am doing wrong here. Is there a way to do this? Thanks in Advance.

Was it helpful?

Solution

You probably need to ask why it matters that the numbers are sequential with no gaps: SQL's IDENTITY type isn't really for this sort of functionality as you're finding out.

That said, if you do need to fill the gaps in, you're nearly there -- the other thing you need to do is reseed the identity field with a new starting number. In SQL Server this is achieved with DBCC CHECKIDENT.

Don't though make this a programmatic thing to fire -- this should be a DBA task only when you need to clean the data up, and it would be fairly rare in practice (in ten years, I've only ever used it once. And that was on a dev database; it's never been used on live environments). If you find you're reseeding regularly then you need to rethink the behaviour of the application.

OTHER TIPS

No-one actually answered the question: "how to turn off autonumber in sql?"

PhpMyAmin shows the following SQL when i change an integer field to an autonum...

ALTER TABLE course CHANGE Test Test INT( 11 ) NOT NULL AUTO_INCREMENT

..and this when i remove the autonum...

ALTER TABLE course CHANGE Test Test INT( 11 ) NOT NULL

In MS ACCESS SQL i used

ALTER TABLE course ALTER Record_ID INTEGER; to turn off the auto number.

if you turn the autonumber back on you'll find it resumes a the last number you had. to reset the autonumber to 1, you need to re-seed it (and your table should be empty)

Maybe that will help the next person that lands here.

Try to find the missing gaps and insert using that ID. This could prove useful and is quite well explained http://www.codediesel.com/mysql/sequence-gaps-in-mysql/

I am not sure what I am doing wrong here.

You are using IDENTITY for something else than it was meant to be used.

As others said before me in the comments, the purpose of IDENTITY is to create unique numbers, not pretty-looking numbers.
If you need pretty numbers, an IDENTITY column is the wrong tool for that. You should create a second (non-identity) column and generate the pretty number with some other method.

In general, it's almost never a good idea to change the value of a primary key (no matter if it's an IDENTITY column or just a "regular" primary key).

Just think of these scenarios:

  • What do you do when there are other tables that reference the table you're changing?
    You'd have to find all other tables where the value is used and change it there as well.
    (think of a Products table with a ProductId, plus an Orders table which uses the same ProductId as well)

  • What do you do when there are already 100,000 records in the database and then someone deletes row 7? Change the values in row 8 to 100,000?

  • Then, imagine that row 7 of 100,000 is deleted and that column is referenced by other tables.

  • Do you really want to do this every time someone deletes a row?

You see, the guys at Microsoft had a reason when they decided not to allow editing IDENTITY values.
You do NOT want to do this...trust me!

There are valid cases (but poor/deficient DB design) where this is applicable.

E.g. a table with low rowcount but moves up through IDs extremely rapidly. You could hit the integer max +ve value.

Short term fix can be to rekey the IDs. Long term is to either prevent rapid ID seed movement or use a better key datatype.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top