惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - flyfish

Microsoft SQL Server 2005 -- 错误 29503。SQL Server 服务无法启动 模拟鼠标移动和左键单击 .C# 获取另一程序控件,改变值,触发事件 - flyfish - 博客园 在ASP程序中调用Web Service ASPNET2.0中读写Cookie的方法! - flyfish - 博客园 转 跨域读取Cookie和session之HttpWebRequest另类方法(网站API开发) ASP.NET WAP开发 用ASP.NET创建移动Web窗体[转] ASP.NET 2.0移动开发入门之使用模拟器 [WAP]dotNet在WAP应用开发中实现按指定页数翻页的解决方案 ntext replace sql SQLServer2005数据库还原到SQLServer2000 如何去除Google搜索结果病毒提示 Tomcat 6 连接 MS SQL 2005 Windows 2003远程桌面连接数限制 如何用SQL命令修改字段名称 FCKeditor详细的设置 log4net 配置与应用 两个sql server 2000的通用分页存储过程
SQL Server 自增字段归零等问题
flyfish · 2009-04-29 · via 博客园 - flyfish

今天在学习SQL Server 2005时遇到了这么一个问题: 练习创建了一个表Person,有5个字段,设置ID为主键,同时设为自增,如图

在多次插入数据后,自增的ID号比较混乱,而且删除数据后,自增的ID并不会归零,经过多方查阅,了解到有两种方法可以控制自增的字段:

方法一:    如果曾经的数据都不需要的话,可以直接清空所有数据,并将自增字段恢复从1开始计数

    truncate table 表名

方法二:   dbcc checkident (''table_name'', reseed, new_reseed_value)   当前值设置为   new_reseed_value。如果自创建表后没有将行插入该表,则在执行   DBCC   CHECKIDENT   后插入的第一行将使用   new_reseed_value   作为标识。否则,下一个插入的行将使用   new_reseed_value   +   1。如果   new_reseed_value   的值小于标识列中的最大值,以后引用该表时将产生   2627   号错误信息。

方法二 不会清空已有数据,操作比较灵活,不仅可以将自增值归零,也适用于删除大量连续行后,重新设置自增值并插入新的数据;或从新的值开始,当然不能和已有的冲突。

当表中有了一下数据后

执行语句   DBCC CHECKIDENT (''dbo.Person'', RESEED, 10) 后再通过Management Studio添加数据时,ID就会自动从11开始,也就是new_reseed_value+1开始


文章出处:http://www.diybl.com/course/7_databases/sql/sqlServer/200838/103319.html