0

I want to update three fields/columns in my database table. One of them is primary key "id", I made it autoincrement using visual studio's set column properties:

Identity Specification: Yes
(Is Identity)         : Yes
Identity Seed         :  1
Identity Increment    :  1

Everything going ok, but after checking for 2 times that every thing is going fine I emptied the contents of the table so that the columns can be newly populated. But first row autoincrement column got the value 3. I deleted the row and tried again but It became 4 after row was updated. I don't know what is happening. My linq code has nothing to do with it I suppose, because its just insertion.

Code behind linq:

//DLCountryies - .dbml filename
// tblcountry - tablename
// CountryName and CountryCode are Column names
//txtCountryName - textbox

using (DLCountryiesDataContext countries = new DLCountryiesDataContext()) 
{                                                                         
            tblcountry country = new tblcountry
            {
                CountryName = txtCountryName.Text.Trim(),
                CountryCode = txtCountryCode.Text.Trim()
            };

            countries.tblcountries.InsertOnSubmit(country);
            countries.SubmitChanges();
} 
2
  • You can use DBCC CHECKIDENT in some simple TSQL to reset your identity, open up management studio and fire up a new query. Enter DBCC CHECKIDENT("YourTableName", RESEED, 0). This will reset your auto field. Commented Oct 19, 2012 at 18:51
  • You also shouldn't pay attention so much to silly ident numbers. Commented Oct 19, 2012 at 18:52

3 Answers 3

2

You're actually seeing the proper behavior for auto-increment columns. The identity value isn't determined by using the current values in the table. SQL Server tracks identity values elsewhere.

Those values don't reset after deleting records. The column simply continues counting from the last value.

Sign up to request clarification or add additional context in comments.

6 Comments

"The column simply continues counting from the last value" But after deleting the row last value is preserved ?
@rd4code - Yes. The value isn't determined by using data in the table. SQL Server tracks the identity values elsewhere.
@rd4code - you can reset that identity by using DBCC CHECKIDENT.
@Justin Niessner Restarting the server will also do (reseed,0) ?
@rd4code - No. The only way to reset is to reseed the identity column. A quick google search (or reading other answers here) will tell you how to do that.
|
1

That is normal read about DBCC CHECKIDENT in BOL.

This will allow you to reseed your primary key:

http://msdn.microsoft.com/en-us/library/ms176057.aspx

From BOL:

USE AdventureWorks2012; GO DBCC CHECKIDENT ("Person.AddressType", RESEED, 0); GO

Comments

0

This is the normal behavior of autoincrement

If you want to reset autoincrement seeds, you can do

  1. Truncate the table
    eg. TRUNCATE TABLE tblcountries

  2. Use DBCC CHECKIDENT
    eg.

    DELETE FROM tblcountries

    DBCC CHECKIDENT('tblcountries',RESEED, 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.