Truncate Table Là Gì

  -  

In this article

Applies to:

*
SQL Server (all supported versions)
*
Azure Squốc lộ Database
*
Azure SQL Managed Instance
*
Azure Synapse Analytics
*
Analytics Platform System (PDW)

Removes all rows from a table or specified partitions of a table, without logging the individual row deletions. TRUNCATE TABLE is similar to the DELETE statement with no WHERE clause; however, TRUNCATE TABLE is faster and uses fewer system and transaction log resources.

Bạn đang xem: Truncate table là gì

*
Transact-Squốc lộ Syntax Conventions

Syntax

-- Syntax for SQL Server and Azure SQL Database TRUNCATE TABLE database_name.schema_name.table_name < WITH ( PARTITIONS ( < , ...n > ) ) > < ; > ::= TO -- Syntax for Azure Synapse Analytics and Parallel Data Warehouse TRUNCATE TABLE database_name.schema_name.table_name <;>
Note

To view Transact-Squốc lộ syntax for SQL Server năm trước và earlier, see Previous versions documentation.


Arguments

database_name

Is the name of the database.

schema_name

Is the name of the schema to which the table belongs.

table_name

Is the name of the table to truncate or from which all rows are removed. table_name must be a literal. table_name cannot be the OBJECT_ID() function or a variable.

WITH ( PARTITIONS ( < , ...n > ) )

Applies to: SQL Server ( SQL Server năm 2016 (13.x) through current version)

Specifies the partitions to truncate or from which all rows are removed. If the table is not partitioned, the WITH PARTITIONS argument will generate an error. If the WITH PARTITIONS clause is not provided, the entire table will be truncated.

can be specified in the following ways:

Provide the number of a partition, for example: WITH (PARTITIONS (2))

Provide the partition numbers for several individual partitions separated by commas, for example: WITH (PARTITIONS (1, 5))

Provide both ranges và individual partitions, for example: WITH (PARTITIONS (2, 4, 6 TO 8))

can be specified as partition numbers separated by the word TO, for example: WITH (PARTITIONS (6 TO 8))

To truncate a partitioned table, the table & indexes must be aligned (partitioned on the same partition function).

Remarks

Compared to lớn the DELETE statement, TRUNCATE TABLE has the following advantages:

Less transaction log space is used.

The DELETE statement removes rows one at a time & records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used lớn store the table data và records only the page deallocations in the transaction log.

Fewer locks are typically used.

When the DELETE statement is executed using a row lochồng, each row in the table is locked for deletion. TRUNCATE TABLE always locks the table (including a schema (SCH-M) lock) and page but not each row.

Without exception, zero pages are left in the table.

After a DELETE statement is executed, the table can still contain empty pages. For example, empty pages in a heap cannot be deallocated without at least an exclusive (LCK_M_X) table loông chồng. If the delete operation does not use a table lochồng, the table (heap) will contain many empty pages. For indexes, the delete operation can leave empty pages behind, although these pages will be deallocated quickly by a background cleanup process.

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition lớn its data, use the DROPhường TABLE statement.

Xem thêm: Trò Chơi Game Bắn Súng Ngắm, Choi Game Sat Thu Sung Ngan Online

If the table contains an identity column, the counter for that column is reset lớn the seed value defined for the column. If no seed was defined, the mặc định value 1 is used. To retain the identity counter, use DELETE instead.


Restrictions

You cannot use TRUNCATE TABLE on tables that:

Are referenced by a FOREIGN KEY constraint. You can truncate a table that has a foreign key that references itself.

Participate in an indexed view.

Are published by using transactional replication or merge replication.

Are system-versioned temporal.

Are referenced by an EDGE constraint.

For tables with one or more of these characteristics, use the DELETE statement instead.

TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions. For more information, see CREATE TRIGGER (Transact-SQL).

In Azure Synapse Analytics and Analytics Platsize System (PDW):

TRUNCATE TABLE is not allowed within the EXPLAIN statement.

TRUNCATE TABLE cannot be ran inside of a transaction.

Truncating Large Tables

ceds.edu.vn SQL Server has the ability to drop or truncate tables that have more than 128 extents without holding simultaneous locks on all the extents required for the drop.

Permissions

The minimum permission required is ALTER on table_name. TRUNCATE TABLE permissions default to lớn the table owner, members of the sysadmin fixed VPS role, & the db_owner và db_ddladmin fixed database roles, & are not transferable. However, you can incorporate the TRUNCATE TABLE statement within a module, such as a stored procedure, and grant appropriate permissions khổng lồ the module using the EXECUTE AS clause.

Examples

A. Truncate a Table

The following example removes all data from the JobCandidate table. SELECT statements are included before & after the TRUNCATE TABLE statement to lớn compare results.

Xem thêm: Công Viên Thực Vật Cảnh Việt Nam, Khu Vườn Thực Vật Cảnh Đẹp Mê Hồn Ở Hà Nội

USE AdventureWorks2012; GO SELECT COUNT(*) AS BeforeTruncateCount FROM HumanResources.JobCandidate; GO TRUNCATE TABLE HumanResources.JobCandidate; GO SELECT COUNT(*) AS AfterTruncateCount FROM HumanResources.JobCandidate; GO

B. Truncate Table Partitions

Applies to: Squốc lộ Server ( Squốc lộ Server năm nhâm thìn (13.x) through current version)

The following example truncates specified partitions of a partitioned table. The WITH (PARTITIONS (2, 4, 6 TO 8)) syntax causes partition numbers 2, 4, 6, 7, và 8 to lớn be truncated.

TRUNCATE TABLE PartitionTable1 WITH (PARTITIONS (2, 4, 6 TO 8)); GO

See Also

DELETE (Transact-SQL)DROP.. TABLE (Transact-SQL)IDENTITY (Property) (Transact-SQL)