SQL Server - Difference between TRUNCATE, DELETE and DROP

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程

TRUNCATE

TRUNCATE SQL query removes all rows from a table, without logging the individual row deletions. TRUNCATE is faster than the DELETE query.

The following example removes all data from the Customers table.

TRUNCATE TABLE Customers;
  1. TRUNCATE is a DDL command.
  2. TRUNCATE is executed using a table lock and the whole table is locked to remove all records.
  3. We cannot use the WHERE clause with TRUNCATE.
  4. TRUNCATE removes all rows from a table.
  5. Minimal logging in the transaction log, so it is faster performance-wise.
  6. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log.
  7. Identify the column is reset to its seed value if the table contains an identity column.
  8. To use Truncate on a table you need at least ALTER permission on the table.
  9. Truncate uses less transaction space than the Delete statement.
  10. Truncate cannot be used with indexed views.
  11. TRUNCATE is faster than DELETE.

DELETE

SQL DELETE query deletes all records from a database table. To execute a DELETE query, delete permissions are required on the target table. If you need to use a WHERE clause in a DELETE, select permissions are required as well.

The following query deletes all rows from the Customers table.

DELETE FROM Customers;
GO

The following SQL query deletes all rows from the Customers table where OrderID is greater than 1000.

DELETE FROM Customers WHERE OrderId > 1000;
GO
  1. DELETE is a DML command.
  2. DELETE is executed using a row lock, each row in the table is locked for deletion.
  3. We can use where clause with DELETE to filter & delete specific records.
  4. The DELETE command is used to remove rows from a table based on WHERE condition.
  5. It maintains the log, so it slower than TRUNCATE.
  6. The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row.
  7. Identity of column keep DELETE retains the identity.
  8. To use Delete you need DELETE permission on the table.
  9. Delete uses more transaction space than the Truncate statement.
  10. The delete can be used with indexed views.

DROP

DROP table query removes one or more table definitions and all data, indexes, triggers, constraints, and permission specifications for those tables. DROP command requires to ALTER permission on the schema to which the table belongs, CONTROL permission on the table, or membership in the db_ddladmin fixed database role.

The following SQL query drops the Customers table and its data and indexes from the current database.

DROP TABLE Customers;
  1. The DROP command removes a table from the database.
  2. All the tables' rows, indexes, and privileges will also be removed.
  3. No DML triggers will be fired.
  4. The operation cannot be rolled back.
  5. DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command.
  6. DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值