---
title: "Truncate & Trunc 的区别"
author: "wing123"
date: "2010-10-13T15:38:41Z"
tags: ["Oracle", "SQL"]
categories: ["企业架构"]
slug: "truncate-trunc"
draft: false
lastmod: "2010-10-13T15:38:41Z"
id: 783835
url: "https://wing123.iteye.com/blog/783835"
---

truncate table tablename

是[]()**ORACLE**的一个[]()**SQL**[]()**语句**!

可以直接[]()**删除**全表数据!**删除**以后无法回滚!**删除**数据的时候,保证没有子表数据,存在子表数据使用级联**删除**,不起作用!  
并且**删除**数据以后的相关序列可以自动重置(需要重写)!  
其执行效率远远高于DELETE操作!  
这就是和DELETE的比较!  
1.不可以下条件**删除**  
2.如果有子表，就不会**删除**，不过可以撤销外健约束～

注意：并且**删除**数据以后的相关序列可以自动重置(需要重写)  
这个意思：

也就是说在使用表中存在所谓的自增字段的时候的!  
可以自动是在MSSQLSERVER中的意思!  
也就是说,在SQLServer中当使用了自增字段后!  
假设现在表中有10行记录了  
使用DEL和TRUCATE**删除**数据以后,再INSERT数据的时候,那个自增字段的数据是不一样的!  
使用DEL的是11  
而使用TRUCATE的是1  
这就是区别!这主要是在SQLSERVER中的差异!  
**ORACLE**中要使下一个数据为1则必须重置SEQ就可以了!  
当然**Oracle**中不重置也可以继续使用

**Trunc**总共有两个类型 Number & date  
**TRUNC** (number)  
The following example truncate numbers:  
SELECT **TRUNC**(15.79,1) "Truncate" FROM DUAL;  
Truncate  
\----------  
15.7  
SELECT **TRUNC**(15.79,-1) "Truncate" FROM DUAL;  
Truncate  
\----------  
10

**TRUNC** (date)  
The **TRUNC** (date) function returns date with the time portion of the day truncated  
to the unit specified by the format model fmt. If you omit fmt, then date is  
truncated to the nearest day.  
Examples  
The following example truncates a date:  
SELECT **TRUNC**(TO\_DATE(’27-OCT-92’,’DD-MON-YY’), ’YEAR’)  
"New Year" FROM DUAL;  
New Year  
\---------  
01-JAN-92