In SQL-delete the difference between table data drop, truncate and delete

First, the syntax in SQL

1. drop table table name eg: drop table dbo.Sys_Test
2. truncate table table name eg: truncate table dbo.Sys_Test
3. delete from table name where column name = value eg: delete from dbo.Sys_Test where test = 'test'

Second, drop, truncate, delete difference

1. drop (delete table): delete content and definitions to free up space. Simply put, the entire table is removed. It is impossible to add data in the future, unless a new table is added.
The drop statement will delete the constraints, triggers, and indexes that the structure of the table is dependent on; the stored procedures / functions that depend on the table will be retained, but its state will become: invalid.

2. truncate (clear the data in the table): delete the content, free space but not delete the definition (retain the data structure of the table). The difference with drop is that it only clears the table data.

   注意:truncate 不能删除行数据,要删就要把表清空。

3. delete (delete the data in the table): The delete statement is used to delete the rows in the table. The process of deleting a delete statement is to delete a row from the table each time, and at the same time, delete the row as a transaction record in the log to facilitate the rollback operation.

   truncate与不带where的delete :只删除数据,而不删除表的结构(定义)

4. truncate table deletes all rows in the table, but the table structure and its columns, constraints, indexes, etc. remain unchanged. The count value used for the new row ID is reset to the seed of this column. If you want to keep the logo count value, use delete instead.

   如果要删除表定义及其数据,请使用 drop table 语句。  

5. For the table referenced by the foreign key constraint, you cannot use truncate table, but you should use the delete statement without the where clause. Since truncate table is recorded in the log, it cannot activate the trigger.

6. Execution speed, generally speaking: drop> truncate> delete.

7. The delete statement is the database operation language (dml). This operation will be placed in the rollback segment and will only take effect after the transaction is submitted; if there is a corresponding trigger, it will be triggered when it is executed.

truncate and drop are database definition languages ​​(ddl). The operation takes effect immediately. The original data is not placed in the rollback segment and cannot be rolled back. The operation does not trigger the trigger.

Published 10 original articles · Likes0 · Visits 959

Guess you like

Origin blog.csdn.net/weixin_43572702/article/details/104747192