SQL Server 标识列值从 0 而不是 1 开始 [英] SQL server identity column values start at 0 instead of 1

查看:51
本文介绍了SQL Server 标识列值从 0 而不是 1 开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的情况,我的数据库中的某些表的 ID 从 0 开始,即使 TABLE CREATE 具有 IDENTITY(1,1).对于某些表是这样,但对于其他表则不然.它一直工作到今天.

我已经尝试重置身份列:

DBCC CHECKIDENT (SyncSession, reseed, 0);

但是新记录从 0 开始.我已尝试对所有表执行此操作,但有些仍从 0 开始,有些仍从 1 开始.

有什么指点吗?

(我使用的是带有高级服务的 SQL Server Express 2005)

解决方案

来自 DBCC CHECKIDENT

DBCC CHECKIDENT ( table_name, RESEED, new_reseed_value )

<块引用>

如果没有插入行自创建以来的表,或所有已通过使用删除行TRUNCATE TABLE 语句,第一个运行 DBCC 后插入的行CHECKIDENT 使用 new_reseed_value 作为身份.否则,下一行插入使用 new_reseed_value +当前增量值.

因此,这是针对空表或截断表的预期.

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)

解决方案

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.

这篇关于SQL Server 标识列值从 0 而不是 1 开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆