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

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - David

TFS Server 不存在时,如何去除对应Server的Workspaces 更改已有程序集中的资源 记录:如何删除TFS Project SQL – Character data (varchar) 又好久没写东西了! Powershell: Send mail with attachment 【Update】 - David SSIS: Execute SSIS Package in command prompt 【Update】 随感 Windows Workflow Foundation 了解 通过代码将Word 2007 template (dotx)文档转换Word 2007 (docx)文档(续) 通过代码将Word 2007 template (dotx)文档转换Word 2007 (docx)文档 创建Word2007文档而无需安装Word2007 Visual WebGUI - 如何处理MessageBox的DialogResult值 Visual WebGUI - 如何将Dialog的结果传回主窗体 Visual WebGUI Report - 导出到PDF Visual WebGUI 在 Vista IIS 7 中的配置 项目管理片语 - Schedule 在IIS 7设置ASP.NET 1.1(在Vista中使用VS2003) 给儿子的歌!
SQL – TRUNCATE vs DELETE
David · 2009-10-12 · via 博客园 - David

还是sqlservercentral.com的问题与解答。

问题:

Consider the following statements:

    create table #t (i int identity(1,1) not null, c varchar(5) not null)
    insert into #t (c) select 'one'
    truncate table #t
    insert into #t (c) select 'two'
    delete #t
    insert into #t (c) select 'three'
    select * from #t

What will be the result of the select query?
A. 1 , three
B. 2 , three
C. 3 , three

你的答案会是哪个呢?我又选错了,选了C。如果两次都是DELETE的话,那么C不会错咯。

那么A呢?经过了两次INSERT,不会还是1了吧。如果两次都是TRUNCATE的话,那么A也不会错了。:),或者DELETE在前,TRUNCATE在后,也没问题。

单选题,A和C都没了,那答案就会是B咯。就像上面两句的假设,一样,由因得果咯。

我们看题,先是创建了一个TABLE,有两个列,列 i 是整型的,并可自增长。列 c 是字符型的。

然后,就把 'one' 插入到TABLE。这时,TABLE里面就有一行。其中列 i 是1,这个是自增长的。

接着,把TABLE #t 给TRUNCATE 了。TRUNCATE这个动作呢,就会把列 i 的计数器给复原为起始值了。这里,TABLE没记录了,列 i 的计数器会回归为1。

再插入一个 'two' 到TABLE。TABLE又有了一行记录,由于上一步的TRUNCATE,列 i 的计数器复位了,所以这个记录的列 i 还会是1。

真费事,刚插入进去,又要DELETE了。但这次DELETE,不会复位列 i 的计数器。

又插一次 'three' 。由于列 i 的计数器没有在上一步被复位,所以这时这一行新加的记录里面的列 i 将会是2。

也就是select * from #t 的结果,会是 2 , three 。

这个问题想说明,TRUNCATE 与 DELETE 的分别:TRUNCATE 会复位自增长列的计数器,而DELETE不会。