简体   繁体   English

SQL Server自动增量

[英]SQL Server Auto-increment

I am using 3-4 tables and using autoincrement on the primaryk ey in all of them. 我正在使用3-4个表,并在所有表中对primary ey使用自动增量。 I want all of the tables to start with 1 我希望所有表格都以1开头

For example student table id starts from 1, and Registration table starts from 1. 例如, student表ID从1开始, Registration从1开始。

Right now what happens auto increments start with the last id of student table. 现在,会发生什么情况,自动递增将从学生表的最后一个ID开始。 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. 例如,学生表中的LastRow ID为100,因此Registeration将从101开始。我希望它从1开始如何进行。

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命令以重置自动增量主键的值,然后从1重新开始。

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

If number = 0 then in the next insert the auto increment field will contain value 1 如果number = 0,则在下一个插入中,自动递增字段将包含值1

If number = 101 then in the next insert the auto increment field will contain value 102 如果number = 101,则在下一个插入中,自动递增字段将包含值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. 我的意思是,如果值1已经存在,则尝试在该表上插入时将出现错误。

I don't think that this is possible in most RDBMS. 我认为这在大多数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. 即使是,我也不建议您这样做-如果表中已经有行,则应以MAX_ROWS + 1开头。

to start the seed from 1 use: 从1开始使用种子:

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! 正如Einav所说,要谨慎处理这个问题!

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 . 如果您的学生表为空,并且由于测试或由于数据库的年度擦除等原因而试图重置计数器,则可以使用DBCC CHECKIDENT命令(如果使用的是MS SQL Server),也可以对MySql使用alter table(auto_increment) 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); 对于您的情况,您将使用:DBCC CHECKIDENT(“学生”,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! 确保您也已经备份了数据库!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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