ORACLE 之 TRUNCATE TABLE

本文详细介绍了 SQL 中 TRUNCATE 语句的使用方法及其与 DELETE 和 DROP TABLE 的区别。TRUNCATE 用于清空表中的所有数据,但不会删除表结构,并且无法回滚操作。此外,还讲解了 TRUNCATE 在不同场景下的应用,如保留或清除物化视图日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TRUNCATE


Caution:

You cannot roll back a TRUNCATE statement.

Purpose

Use the TRUNCATE statement to remove all rows from a table or cluster. By default, Oracle Database also deallocates all space used by the removed rows except that specified by the MINEXTENTS storage parameter and sets the NEXT storage parameter to the size of the last extent removed from the segment by the truncation process.

Removing rows with the TRUNCATE statement can be more efficient than dropping and re-creating a table. Dropping and re-creating a table invalidates dependent objects of the table, requires you to regrant object privileges on the table, and requires you to re-create the indexes, integrity constraints, and triggers on the table and respecify its storage parameters. Truncating has none of these effects.


See Also:

  • DELETE and DROP TABLE for information on other ways to drop table data from the database

  • DROP CLUSTER for information on dropping cluster tables


Prerequisites

To truncate a table or cluster, the table or cluster must be in your schema or you must have DROP ANY TABLE system privilege.

Syntax


truncate::=
Description of truncate.gif follows
Description of the illustration truncate.gif

Semantics


TABLE Clause

Specify the schema and name of the table to be truncated. This table cannot be part of a cluster. If you omit schema , then Oracle Database assumes the table is in your own cluster.

  • You can truncate index-organized tables and temporary tables. When you truncate a temporary table, only the rows created during the current session are removed.

  • Oracle Database changes the NEXT storage parameter of table to be the size of the last extent deleted from the segment in the process of truncation.

  • Oracle Database also automatically truncates and resets any existing UNUSABLE indicators for the following indexes on table : range and hash partitions of local indexes and subpartitions of local indexes.

  • If table is not empty, then the database marks UNUSABLE all nonpartitioned indexes and all partitions of global partitioned indexes on the table.

  • For a domain index, this statement invokes the appropriate truncate routine to truncate the domain index data.


    See Also:

    Oracle Data Cartridge Developer's Guide for more information on domain indexes

  • If a regular or index-organized table contains LOB columns, then all LOB data and LOB index segments are truncated.

  • If table is partitioned, then all partitions or subpartitions, as well as the LOB data and LOB index segments for each partition or subpartition, are truncated.


    Note:

    When you truncate a table, Oracle Database automatically removes all data in the table's indexes and any materialized view direct-path INSERT information held in association with the table. This information is independent of any materialized view log. If this direct-path INSERT information is removed, then an incremental refresh of the materialized view may lose data.

Restrictions on Truncating Tables
  • You cannot individually truncate a table that is part of a cluster. You must either truncate the cluster, delete all rows from the table, or drop and re-create the table.

  • You cannot truncate the parent table of an enabled referential integrity constraint. You must disable the constraint before truncating the table. An exception is that you can truncate the table if the integrity constraint is self-referential.

  • If a domain index is defined on table , then neither the index nor any index partitions can be marked IN_PROGRESS .


MATERIALIZED VIEW LOG Clause

The MATERIALIZED VIEW LOG clause lets you specify whether a materialized view log defined on the table is to be preserved or purged when the table is truncated. This clause permits materialized view master tables to be reorganized through export or import without affecting the ability of primary key materialized views defined on the master to be fast refreshed. To support continued fast refresh of primary key materialized views, the materialized view log must record primary key information.


Note:

The keyword SNAPSHOT is supported in place of MATERIALIZED VIEW for backward compatibility.


PRESERVE

Specify PRESERVE if any materialized view log should be preserved when the master table is truncated. This is the default.


PURGE

Specify PURGE if any materialized view log should be purged when the master table is truncated.


See Also:

Oracle Database Advanced Replication for more information about materialized view logs and the TRUNCATE statement


CLUSTER Clause

Specify the schema and name of the cluster to be truncated. You can truncate only an indexed cluster, not a hash cluster. If you omit schema , then the database assumes the cluster is in your own schema.

When you truncate a cluster, the database also automatically deletes all data in the indexes of the cluster tables.


STORAGE Clauses

The STORAGE clauses let you determine what happens to the space freed by the truncated rows. The DROP STORAGE clause and REUSE STORAGE clause also apply to the space freed by the data deleted from associated indexes.


DROP STORAGE

Specify DROP STORAGE to deallocate all space from the deleted rows from the table or cluster except the space allocated by the MINEXTENTS parameter of the table or cluster. This space can subsequently be used by other objects in the tablespace. Oracle Database also sets the NEXT storage parameter to the size of the last extent removed from the segment in the truncation process. This is the default.


REUSE STORAGE

Specify REUSE STORAGE to retain the space from the deleted rows allocated to the table or cluster. Storage values are not reset to the values when the table or cluster was created. This space can subsequently be used only by new data in the table or cluster resulting from insert or update operations. This clause leaves storage parameters at their current settings.

If you have specified more than one free list for the object you are truncating, then the REUSE STORAGE clause also removes any mapping of free lists to instances and resets the high-water mark to the beginning of the first extent.

Examples


Truncating a Table: Example

The following statement removes all rows from a hypothetical copy of the sample table hr.employees and returns the freed space to the tablespace containing employees :

TRUNCATE TABLE employees_demo; 

The preceding statement also removes all data from all indexes on employees and returns the freed space to the tablespaces containing them.


Retaining Free Space After Truncate: Example

The following statement removes all rows from all tables in the personnel cluster, but leaves the freed space allocated to the tables:

TRUNCATE CLUSTER personnel REUSE STORAGE;

The preceding statement also removes all data from all indexes on the tables in the personnel cluster.


Preserving Materialized View Logs After Truncate: Example

The following statements are examples of truncate statements that preserve materialized view logs:

TRUNCATE TABLE sales_demo PRESERVE MATERIALIZED VIEW LOG; 

TRUNCATE TABLE orders_demo;
<think>好的,我现在需要处理用户关于Oracle数据库中TRUNCATE TABLE语句的用法和相关问题的查询。首先,我要确认用户的需求是什么。用户明确提到想了解TRUNCATE TABLE的用法、示例以及注意事项,可能还隐含想了解如何恢复被TRUNCATE的数据,或者与其他类似命令如DELETE的区别。 接下来,我需要回忆TRUNCATE TABLE的基本知识。TRUNCATE属于DDL操作,会立即释放表空间,并且无法通过ROLLBACK回滚。这一点和DELETE不同,DELETE是DML,可以回滚,而且不会释放存储空间。另外,TRUNCATE会重置表的HWM(高水位线),而DELETE不会。这些是需要强调的重要区别。 然后,我需要考虑使用场景。用户可能需要快速清空大表,这时候TRUNCATE更高效。但需要注意权限问题,因为TRUNCATE需要DROP ANY TABLE权限,或者表在用户的schema中。此外,外键约束可能会影响TRUNCATE的执行,如果有被其他表引用的主键,需要用CASCADE选项,或者先禁用约束。 关于示例,需要给出基本的语法,比如TRUNCATE TABLE table_name; 或者带选项的,比如TRUNCATE TABLE table_name REUSE STORAGE; 或者TRUNCATE TABLE table_name DROP STORAGE; 另外,分区表的TRUNCATE示例也需要提及,比如TRUNCATE分区。 注意事项方面,除了权限和回滚问题,还要提到触发器不会触发,索引会被截断,但结构保留,以及闪回查询的限制,因为TRUNCATE会重置数据字典中的信息,导致无法直接闪回。 用户可能还关心数据恢复的方法,比如使用闪回数据库、ODU工具,或者备份恢复。这里需要引用提供的参考链接中的信息,比如ODU工具可以恢复TRUNCATE的表,但具体步骤可能不在当前回答范围内,可以简要提及。 最后,生成相关问题,考虑用户可能的后续疑问,比如与DELETE的区别,恢复方法,性能影响,权限问题,以及分区处理等。需要确保问题覆盖用户可能关心的各个方面,帮助用户深入理解。 需要检查是否有遗漏的重要点,比如触发器的影响,是否记录日志(TRUNCATE用极少的日志),以及是否影响物化视图等。同时,确保语法示例正确,注意事项全面,并且引用了提供的参考资料中的恢复方法,比如ODU工具。 现在组织内容,分用法、示例、注意事项几个部分,结构清晰,使用中文,正确应用LaTeX格式,如命令用$TRUNCATE$,但根据用户要求,行内数学表达式用$...$,可能这里不需要,但如果有涉及数学的地方需要注意。不过TRUNCATE是SQL语句,可能不需要数学公式,但如果有分区或存储参数的计算,可能需要用LaTeX。比如分区表的示例,可以展示语法结构。 确认引用部分正确添加,用户提供的参考链接中提到ODU恢复TRUNCATE表,所以在注意事项或恢复方法部分添加引用标识[^1]。同时,确保回答真实可靠,避免错误信息,比如TRUNCATE是否可回滚(不可,因为是DDL),或者是否需要commit(不需要,自动提交)。 总结步骤:先介绍TRUNCATE的作用和特点,然后给出语法示例,接着详细列出注意事项,最后生成相关问题,确保覆盖用户可能的需求点。</think>### Oracle数据库TRUNCATE TABLE用法详解 #### 一、基本用法 $TRUNCATE TABLE$ 是Oracle中用于快速清空表数据的DDL命令,具有以下特性: 1. 立即释放表空间(默认$DROP STORAGE$模式) 2. 重置表的**高水位线(HWM)**[^1] 3. 不触发DELETE触发器 4. 自动提交事务,无法回滚 5. 比DELETE操作快10倍以上(特别对大表) #### 二、语法示例 ```sql -- 基础语法 TRUNCATE TABLE employees; -- 保留存储空间(REUSE STORAGE) TRUNCATE TABLE sales REUSE STORAGE; -- 分区表操作 TRUNCATE TABLE orders PARTITION (p2023); ``` #### 三、注意事项 1. **权限要求** - 需要$DROP ANY TABLE$系统权限 - 或表在用户自己的schema中 2. **外键约束** 如果表被其他表的外键引用,需使用级联操作: ```sql TRUNCATE TABLE parent_table CASCADE; ``` 3. **不可恢复性** 数据删除后无法通过常规方法恢复,但可通过以下方式: - 闪回数据库(需提前配置) - 专业工具恢复(如ODU工具)[^1] - 数据库备份恢复 4. **索引处理** 所有索引会被自动截断,但索引结构保留 5. **日志差异** 仅记录少量数据字典日志,不生成UNDO日志 #### 四、典型应用场景 1. 测试环境数据清理 2. 临时表数据清空 3. 数据归档前的预处理 4. 快速释放表空间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值