Reset Identity column in SQL Server

前端 未结 10 794
遥遥无期
遥遥无期 2021-01-18 00:59

I am making an SQL database that stores contacts. I want to be able to delete contacts, and the correct id for each contact is crucial for my software connecting to it. Lets

10条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 01:09

    I wrote an application to handle a multi-level sales program. Of course, people drop out. In ours, people had to be inserted as well.

    You're on the right track with one modification.

    The identity number (ID) and the sequence number (seq) are two different things. They have no relationship with each other at all.

    Never change an ID. Once assigned, always assigned.

    Create a column (cNEXT) in your table for the sequence and populate it with IDs. "What ID is to be the next one in this sequence?"

    Shuffle the IDs in cNEXT around, reassigning cNEXT, any time you want. Any stored proc can do that.

    Then you also have the flexibility to create non-sequential chains of IDs. This is useful when people move to different regions or get promotions to different groups.

    Hope this helps! :)

提交回复
热议问题