简体   繁体   English

SQL Server 的自动增量功能

[英]Auto Increment feature of SQL Server

I have created a table named as ABC.我创建了一个名为 ABC 的表。 It has three columns which are as follows:-它有三列,如下所示:-

在此处输入图片说明

The column number_pk (int) is the primary key of my table in which I have made the auto increment feature on for that column.number_pk (int)是我的表的主键,我在其中为该列启用了auto increment功能。

Now I have deleted two rows from that table say Number_pk= 5 and Number_pk =6 .现在我从该表中删除了两行,比如Number_pk= 5Number_pk =6

The table which I get now is like this:-我现在得到的表是这样的:-

在此处输入图片说明

Now if I again enter two new rows in this table with the same value I get the two new Number_pk starting from 7 and 8 ie,现在,如果我再次在这个表中输入两个具有相同值的新行,我会Number_pk starting from 7 and 8得到两个新的Number_pk starting from 7 and 8即,

在此处输入图片说明

My question is that what is the logic behind this since I have deleted the two rows from the table.我的问题是,这背后的逻辑是什么,因为我已经从表中删除了两行。 I know that a simple answer is because I have set the auto increment on for the primary key of my table.我知道一个简单的答案是因为我已经为我的表的主键设置了自动增量。 But I want to know is there any way that I can insert the two new entries starting from the last Number_pk without changing the design of my table?但我想知道有什么方法可以从最后一个 Number_pk 开始插入两个新条目而不改变我的表的设计?

And how the SQL Server manage this record since I have deleted the rows from the database??自从我从数据库中删除了行之后,SQL Server 是如何管理这条记录的??

Although you should not do this until their is a specific requirement.尽管您不应该这样做,直到它们成为特定要求。

1.) Get the max id: 1.) 获取最大 ID:

Declare @id int
Select @id = Max(Number_pk) From ABC
SET @id = @id + 1;

2.) And reset the Identity Column: 2.) 并重置身份列:

DBCC CHECKIDENT('ABC', RESEED, @id)

DBCC CHECKIDENT (Transact-SQL) DBCC CHECKIDENT (Transact-SQL)

The logic is guaranteeing that the generated numbers are unique.该逻辑保证生成的数字是唯一的。 An ID field does not neccessarily have to have a meaning, but rather is most often used to identify a unique record, thus making it easier to perform operations on it. ID 字段不一定具有含义,而是最常用于标识唯一记录,从而更容易对其执行操作。

If your database is designed properly, the deleted ID numbers would not have been possible to delete if they were referenced by any other tables in a foreign key relationship, thus preventing records from being orphaned in that way.如果您的数据库设计得当,如果已删除的 ID 号被外键关系中的任何其他表引用,则将无法删除它们,从而防止记录以这种方式孤立。

If you absolutely want to have entries sequences, you could consider issuing a RESEED , but as suggested, it would not really give you much advantages.如果你绝对想要条目序列,你可以考虑发布一个RESEED ,但正如建议的那样,它不会真正给你带来太多好处。

The identity record is "managed" because SQL Server will keep track of which numbers have been issued, regardless of whether they are still present or not.身份记录是“托管的”,因为 SQL Server 将跟踪已发出的编号,而不管这些编号是否仍然存在。

Should you ever want to delete all records from a table, there are two ways to do so (provided no foreign key relatsons exist):如果您想从表中删除所有记录,有两种方法可以这样做(前提是不存在外键关系):

DELETE FROM Table

DELETE just removes the records, but the next INSERTED value will continue where the ID numbering left of. DELETE只是删除记录,但下一个 INSERTED 值将继续 ID 编号左侧的位置。

TRUNCATE TABLE

TRUNCATE will actually RESEED the table, thus guaranteeing it starts again at the value you originally specified (most likely 1). TRUNCATE实际上会对表进行 RESEED,从而保证它以您最初指定的值(很可能是 1)重新开始。

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

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