how to reset the primary key restraint

  • I am deleting rows out of a table, when I insert values back in the primary key is starting the number 1 + the last one that was deleted. I deleted 57 rows, when I inserted the values the primary key started counting at 58 and went to 115. Is there a simple procedure or function that does this?

  • If you want again a starting value of 1, take a look at BOL for DBCC CHECKIDENT.

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • Truncate table also resets identities

  • Alternatively, after performing the delete, go into EM, design table and set the Identity Seed of the primary key to something else (eg 2) and click on Save. Then set the Identity Seed back to 1, Save again, and your numbering will again start from 1.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • simple advice:

    don't be bothered by autogenerated numbers, you'll not care for GUIDs you generate as well, would you?

    deleting rows when using IDENTITY always will give you "holes" in your sequence, but why think about it? it will not impact functionality!

    and lastly: the correct term is "constraint" 😉

    best regards,

    chris.

  • You seem to be using the "Identity" column for more than "Idnetity". Can you explain why? Their are many ways to generate a series of integers, terporary or permanant. You might want to consider an alternative to altering the identity column.

    -Isaiah


    -Isaiah

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply