How get 1 back to ID column?

Now, I have a table, tTest at a MS SQL server database. Only 2 columns for it, ID and Name. ID is the primary key, and its seed is 1 and will  automatically increase by 1 when a new record is inserted. The test data is: 

ID  Name

1    A

2    B

3    C

After deleting all rows, I would like to insert another row "1  X". But I can only get "4  X". How to get "1  X"? Thanks a lot

March 25th, 2015 5:01pm

You've set your column to be IDENTITY. That's causing the behavior you described, and functioning as intended.

If you want to insert a 1 into the column, you'll have to either remove the identity column, or SET IDENTITY_INSERT ON.

Why do you need your IDs to begin at 1 again?

Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 5:34pm


After deleting all rows, I would like to insert another row "1  X". But I can only get "4  X". How to get "1  X"? Thanks a lot

Why cant you try with Truncate Table, as long as you want to delete all rows and no foreign keys on table?

Try the below

Truncate Table tTest

March 25th, 2015 9:04pm

one other way :)  --- after deleting all the rows -- reseed the identity value to 0. test this script..

create table AA(id int identity(1,1),name varchar(20))
go
insert into AA
values ('oldvalue')
go 10
Delete from AA
go
--reset the identity value
Dbcc checkident(AA,reseed,0)
go
---new insert
insert into AA
values ('newvalue')
go 10
--check the values
select * from AA
go
--clean up
drop table AA

Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 10:20pm

If you delete ID=2 only like

DELETE FROM tbl WHERE ID=2, do you want the new inserted row to get value=2?

March 26th, 2015 2:29am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics