SQL 重新播种有效,但自动增量从 0 开始

发布于 2024-09-08 00:38:54 字数 285 浏览 5 评论 0原文

我有一些表,其中有一个身份列,我正在尝试重新播种。重新播种有效(我认为),但是当将新数据项插入表中时,标识列从 0 开始。

我的重新播种代码是:

DBCC CHECKIDENT(MyTable, RESEED, 0)

表的标识规范是:

  • Identity Increment = 1 个
  • 身份种子 = 1

快速说明 我正在重新播种之前对表执行删除操作

,请帮忙

I have some table that have an identity column that I am trying to reseed. The reseeding works (I think) but when a new data item is inserted into the table the identity column starts at 0.

My code to reseed is:

DBCC CHECKIDENT(MyTable, RESEED, 0)

The Identity Specifications for the tables are:

  • Identity Increment = 1
  • Identity Seed = 1

QUICK NOTE I am performing a delete on the tables prior to reseeding

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你怎么这么可爱啊 2024-09-15 00:38:54

当种子初始化为 0 时,似乎“未初始化”或截断的表(意味着没有数据插入到表中)将从 0 开始。但是当数据已插入到表中并且使用删除来清除所有数据时表的行。重新播种到 0 只会使数据库的最后一个种子为 0,下一个种子为 1。

这就是复制该问题的示例:

-- Script to create a test table
IF EXISTS(SELECT 1 FROM Information_Schema.Tables WHERE TABLE_SCHEMA = 'dbo' 
            AND TABLE_NAME = 'SeedTest') BEGIN

    DROP TABLE SeedTest

END

-- Create a Test Seed Table
CREATE TABLE [dbo].[SeedTest](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Value] [varchar](255) NOT NULL,
 CONSTRAINT [PK_SeedTest] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


-- When a table is truncated or "Not Initialized" (meaning no data EVER inserted)
-- An initial reseed of 0 will make the first identity insert value = 0.
DBCC CHECKIDENT (SeedTest, RESEED, 0)

GO

INSERT INTO SeedTest([Value]) VALUES('Test')
SELECT * FROM SeedTest

GO

-- If you truncate the table and reseed the same effect will occur (first identity insert value = 0).
TRUNCATE TABLE SeedTest

GO

DBCC CHECKIDENT (SeedTest, RESEED, 0)

GO

INSERT INTO SeedTest([Value]) VALUES('Test')

SELECT * FROM SeedTest


-- When Deleting records from a table (Foreign key constraints may prevent a truncate)
-- Reseeding to 0 will set the last seed to 0 and make the next seed = 1
DELETE FROM SeedTest

GO

DBCC CHECKIDENT (SeedTest, RESEED, 0)

GO

INSERT INTO SeedTest([Value]) VALUES('Test')
SELECT * FROM SeedTest

GO

It appears that "uninitialized" or truncated tables (meaning no data has ever been inserted into the table) when the seed is initialized to 0 will start at 0. But when data has been inserted into a table and a delete is used to clear all rows of the table. Reseeding to 0 will only leave the database's last seed at 0 with the next seed being 1.

That said here is an example replicating the issue:

-- Script to create a test table
IF EXISTS(SELECT 1 FROM Information_Schema.Tables WHERE TABLE_SCHEMA = 'dbo' 
            AND TABLE_NAME = 'SeedTest') BEGIN

    DROP TABLE SeedTest

END

-- Create a Test Seed Table
CREATE TABLE [dbo].[SeedTest](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Value] [varchar](255) NOT NULL,
 CONSTRAINT [PK_SeedTest] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


-- When a table is truncated or "Not Initialized" (meaning no data EVER inserted)
-- An initial reseed of 0 will make the first identity insert value = 0.
DBCC CHECKIDENT (SeedTest, RESEED, 0)

GO

INSERT INTO SeedTest([Value]) VALUES('Test')
SELECT * FROM SeedTest

GO

-- If you truncate the table and reseed the same effect will occur (first identity insert value = 0).
TRUNCATE TABLE SeedTest

GO

DBCC CHECKIDENT (SeedTest, RESEED, 0)

GO

INSERT INTO SeedTest([Value]) VALUES('Test')

SELECT * FROM SeedTest


-- When Deleting records from a table (Foreign key constraints may prevent a truncate)
-- Reseeding to 0 will set the last seed to 0 and make the next seed = 1
DELETE FROM SeedTest

GO

DBCC CHECKIDENT (SeedTest, RESEED, 0)

GO

INSERT INTO SeedTest([Value]) VALUES('Test')
SELECT * FROM SeedTest

GO
对你的占有欲 2024-09-15 00:38:54

我认为您正在寻找的是:

DBCC CHECKIDENT(MyTable, RESEED, -1)

这仅在表已增加的情况下,即已添加行然后删除。下一个插入的行现在将增加 0。

如果表没有行,请使用:
DBCC CHECKIDENT(MyTable, RESEED, 0)

I think what you are looking for is:

DBCC CHECKIDENT(MyTable, RESEED, -1)

This is only if the Table has been incremented, that is row had been added and then deleted. The next inserted row will now be incremented giving 0.

In the case the table didn't have rows, use:
DBCC CHECKIDENT(MyTable, RESEED, 0)

非要怀念 2024-09-15 00:38:54

使用该命令,您可以告诉 IDENTITY 将自身设置回 0 作为其新种子。

不会返回到原始定义 (IDENTITY(1,1)),而是返回到您在 DBCC 命令中指定为第三个参数的值。

如果您想返回使用 1 作为种子值,请使用:

DBCC CHECKIDENT(MyTable, RESEED, 1)

如果您想使用 100,请使用:

DBCC CHECKIDENT(MyTable, RESEED, 100)

您使用 DBCC CHECKIDENT 定义和设置的值将成为第一个当您向该表中插入行时,用于 IDENTITY 列的新值。

当您查看 MSDN 联机丛书文档时,您可以看到:

DBCC CHECKIDENT 
( 
    table_name
        [ , { NORESEED | { RESEED [ ,new_reseed_value ] } } ]
)

新的重新种子值

是用作标识列当前值的新值。

因此,实际上是您定义了 IDENTITY 列的新值 - 如果您像在帖子中那样传入 0,那么它将是 0 - 毕竟这就是您所要求的......

Using that command, you're telling the IDENTITY to set itself back to 0 as its new seed.

It will not go back to the original definition (IDENTITY(1,1)), but to the value you specify as the third parameter in the DBCC command.

If you want to go back to using a 1 as your seed value, use:

DBCC CHECKIDENT(MyTable, RESEED, 1)

If you want to go to 100, use:

DBCC CHECKIDENT(MyTable, RESEED, 100)

That value that you defined and set by using DBCC CHECKIDENT will then be the first new value used for the IDENTITY column when you insert a row into that table.

When you check the MSDN Books Online documentation, you can see:

DBCC CHECKIDENT 
( 
    table_name
        [ , { NORESEED | { RESEED [ ,new_reseed_value ] } } ]
)

new_reseed_value

Is the new value to use as the current value of the identity column.

So it's really you who defines the new value of the IDENTITY column - if you pass in 0 as you do in your post, it will be 0 - that's what you asked for, after all...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文