简体   繁体   中英

sql-server primary key (ID) does not increment correctly

I am working on a project with C# and SQL Server 2016.

In SQL Server Management Studio I add a primary key to an existing table, by right clicking the table and choosing design table, it worked fine.

But as soon as I add some new users and deleted some users, it started behaving strange, I mean after id=4 next user should have id=5 but instead it was given id=7 .

Screenshot of SSMS

在此处输入图片说明

Actually you have deleted the users with id 5 and 6. The counter for the auto increment is not reset or reversed automatically. Its the normal and standard behavior.

可能删除了ID为5和6的记录

IDENITY T-SQL

Specifically the 'Reuse of values'

'For a given identity property with specific seed/increment, the identity values are not reused by the engine. If a particular insert statement fails or if the insert statement is rolled back then the consumed identity values are lost and will not be generated again. This can result in gaps when the subsequent identity values are generated. '

Hope this helps to explain

This may sound unbelievable, but if you want an incrementing record number, SQL Server has no support for you. A transaction that is rolled back or a server restart can leave holes in the numbers.

If you delete a row, you'll have to implement that manually. Say that records 1, 2 and 3 exist. You delete record 2. What number should the new order get? If you say 2, remember that means order 2 is created after order 3, which would confuse a lot of people.

It might not just be that the records have been deleted. Every time you try to INSERT data into that table the seed will be incremented. Take, as a very simple example:

CREATE TABLE #Sample (ID int IDENTITY(1,1), String char(1));
GO
INSERT INTO #Sample (String) VALUES ('A'); --This'll get ID 1
GO
INSERT INTO #Sample (String) VALUES ('AB'); --This'll get ID 2, but fail insertion (due to truncation)
GO
INSERT INTO #Sample (String) VALUES ('C'); --This'll be ID 3.
GO
--Let's Check.
SELECT *
FROM #Sample;
GO

--The SAme is tue for Transactions.
BEGIN TRANSACTION;
INSERT INTO #Sample (String) VALUES ('Q'); --This'll get ID 4
ROLLBACK;

INSERT INTO #Sample (String) VALUES ('S'); --This'll get ID 5

SELECT *
FROM #Sample;

GO
--DELETE
--Let's delete EVERYTHING
DELETE
FROM #Sample;

INSERT INTO #Sample (String) VALUES ('A'); --This'll get ID 6, Not 1

SELECT *
FROM #Sample;
GO
--Finally, howerver, truncate:
TRUNCATE TABLE #Sample;

--Truncate RESETS the seed.
INSERT INTO #Sample (String) VALUES ('A'); --This'll be 1;

SELECT *
FROM #Sample;

GO
--Clean up
DROP TABLE #Sample;

Hope that helps.

Assuming that you have used the identity feature in SQL server. When you try to insert a new record into a table having an identity column, the identity value will be increased even if the insert failed due.

  • Suppose I have a table where the identity column SeqNo have the current value as 9 and incremented by 1.
  • I'm inserting a new record but it failed due to some data issues.
  • So I fixed the issues are tried re-inserting the same.

Assuming no other inserts happened during this time, the SeqNo for the new record should be 10, but it will be 11.

because SQL Server won't roll back the Identity Seed if the insert fails. So you will have to manually re-seed the column

Same in the case of delete and insert. I have the Maximum SeqNo as 5.I deleted the SeqNo 5 and inserted a new record, but for my new record, the SeqNo will be 6 (or the next identity value)

So, if you want to specify some values to the identity column, try inserting with SET IDENTITY INSERT ON. like this

SET IDENTITY INSERT YourTable ON

INSERT INTO YourTable(SeqNo,Name)
VALUES(5,'ABC')

SET IDENTITY INSERT YourTable OFF

When we delete of cancel(by pressing ESC button while filling values in table), it takes it as increment, you can truncate your table(

NOTE: Truncate will delete all values from your table

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM