简体   繁体   中英

SQL Server Auto-increment

I am using 3-4 tables and using autoincrement on the primaryk ey in all of them. I want all of the tables to start with 1

For example student table id starts from 1, and Registration table starts from 1.

Right now what happens auto increments start with the last id of student table. For eg LastRow ID in student table is 100 so Registeration will start from 101. I want it to start from 1 how to do that.

If you have some values already (tables not empty) then this is not possible. You could empty all your tables and run DBCC CHECKIDENT command to reset the value of your autoincrement primary key and restart from 1.

DBCC CHECKIDENT ("YourTableNameHere", RESEED, number);

If number = 0 then in the next insert the auto increment field will contain value 1

If number = 101 then in the next insert the auto increment field will contain value 102

But be aware that this command just resets the value of identity column, it does not check for conflicts . I mean, if the value 1 already exists then you will have errors when trying to insert on that table.

I don't think that this is possible in most RDBMS. Even if It is, I wouldn't advise you to do it - if there are already rows in the table you should start with MAX_ROWS + 1.

to start the seed from 1 use:

DBCC CHECKIDENT (students, RESEED, 0)

note: you have to be sure that the table is empty if you are doing this

As Einav said this subject is to be handled with caution!

If your student table is empty and you are trying to reset the counters because of testing, or because of a yearly wipe down of the database etc. then you can use the DBCC CHECKIDENT command(if you are using MS SQL Server ) or you can use alter table(auto_increment) for MySql http://dev.mysql.com/doc/refman/5.0/en/alter-table.html .

But be careful as any foreign key dependencies will also have to be reset - you can run into all sorts of trouble.

Here is a quick test scenario:

--Test variables & environment
DECLARE @TimeParam AS DATETIME
CREATE TABLE #Downtime (ID int identity, initial varchar(1))
INSERT INTO #Downtime VALUES('a')
INSERT INTO #Downtime VALUES('b')
INSERT INTO #Downtime VALUES('c')
INSERT INTO #Downtime VALUES('d')
INSERT INTO #Downtime VALUES('e')
INSERT INTO #Downtime VALUES('f')
INSERT INTO #Downtime VALUES('g')
INSERT INTO #Downtime VALUES('h')
INSERT INTO #Downtime VALUES('i')
INSERT INTO #Downtime VALUES('j')

--Queries
DBCC CHECKIDENT (#Downtime)
SELECT ID, initial FROM #Downtime
DELETE FROM #Downtime
DBCC CHECKIDENT (#Downtime, RESEED, 0)
INSERT INTO #Downtime VALUES('k')
SELECT ID, initial FROM #Downtime
DROP TABLE #Downtime

--results (first select)
ID  initial
1   a
2   b
3   c
4   d
5   e
6   f
7   g
8   h
9   i
10  j

--results (second select)
ID  initial
2   j

For your case you would use: DBCC CHECKIDENT ('Student', RESEED, 0);

Once again handle this command with caution and check all references to this primary key BEFORE running the command. Make sure you have taken backups of the database too!

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