Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL server identity column values start at 0 instead of 1

I've got a strange situation with some tables in my database starting its IDs from 0, even though TABLE CREATE has IDENTITY(1,1). This is so for some tables, but not for others. It has worked until today.

I've tried resetting identity column:

DBCC CHECKIDENT (SyncSession, reseed, 0); 

But new records start with 0. I have tried doing this for all tables, but some still start from 0 and some from 1.

Any pointers?

(i'm using SQL Server Express 2005 with Advanced Services)

like image 288
Muxa Avatar asked Apr 07 '09 08:04

Muxa


People also ask

Does SQL ID start at 0 or 1?

Bookmark this question. Show activity on this post.

How do I start an identity column from 1?

Step 1 : Create a table named school. CREATE TABLE school ( student_id INT IDENTITY, student_name VARCHAR(200), marks INT ); Here, the 'student_id' column of the table starts from 1 as the default value of seed is 1 and each row is incremented by 1. Step 2 : Insert some value into a table.

How do I change the identity column in SQL?

You cannot alter a column to be an IDENTITY column. What you'll need to do is create a new column which is defined as an IDENTITY from the get-go, then drop the old column, and rename the new one to the old name.


1 Answers

From DBCC CHECKIDENT

DBCC CHECKIDENT ( table_name, RESEED, new_reseed_value ) 

If no rows have been inserted to the table since it was created, or all rows have been removed by using the TRUNCATE TABLE statement, the first row inserted after you run DBCC CHECKIDENT uses new_reseed_value as the identity. Otherwise, the next row inserted uses new_reseed_value + the current increment value.

So, this is expected for an empty or truncated table.

like image 73
gbn Avatar answered Sep 19 '22 12:09

gbn