简体   繁体   English

SQL截断,删除,删除建议

[英]SQL Truncate, Delete, Drop advise

I have a table in a SQL db that I want to remove the data from? 我想从中删除数据的SQL数据库中有一个表? I want to keep the columns though. 我想保留列。

eg my table has 3 columns, Name, Age, Date. 例如,我的表有3列,即名称,年龄,日期。 I don't want to remove these, i just want to remove the data. 我不想删除这些,我只想删除数据。

Should I should Truncate, Delete or Drop? 我应该截断,删除还是删除?

Don't drop - it will delete the data and the definition. 不要删除-它将删除数据和定义。
If you delete - the data is gone and auto-increment values go on from the last value. 如果删除-数据消失了,并且自动增加值从上一个值开始继续。
If you truncate - then it is like you just did create the table. 如果截断-就像您刚刚创建了表一样。 No data and all counters resetted 无数据且所有计数器已重置

Truncate is very fast - like quick format of the table. 截断非常快-就像表格的快速格式一样。 It does not require any extra space when deleting. 删除时不需要任何额外的空间。 You can not rollback his operation. 您无法回滚他的操作。 You can not specify conditions. 您不能指定条件。 This is best choice for deleting all data from table. 这是从表中删除所有数据的最佳选择。

Delete is much slower and you need extra space for this operation, because you must be able to rollback the data. 删除要慢得多,并且此操作需要额外的空间,因为您必须能够回滚数据。 If you need to delete all data, use truncate. 如果需要删除所有数据,请使用截断。 If you need to specify conditions, use delete. 如果需要指定条件,请使用删除。

Drop table - you can delete data from table by dropping and creating it again like you would truncate it, but it is slower and you can have some other problems, like foreign key dependencies. 删除表-您可以通过删除和重新创建表中的数据来删除表中的数据,就像您要截断它一样,但是它速度较慢,并且可能会遇到其他一些问题,例如外键依赖关系。 I definitely don't recommend this operation. 我绝对不推荐此操作。

delete from TableName should do the trick delete from TableName应该可以解决问题

DROPing the table will remove the colums too. 删除表也将删除列。

Delete: The DELETE Statement is used to delete rows from a table. 删除: DELETE语句用于从表中删除行。

Truncate: The SQL TRUNCATE command is used to delete all the rows from the table and free the space containing the table. 截断: SQL TRUNCATE命令用于删除表中的所有行并释放包含表的空间。

* Drop : * The SQL DROP command is used to remove an object from the database. * Drop * SQL DROP命令用于从数据库中删除对象。 If you drop a table, all the rows in the table is deleted and the table structure is removed from the database 如果删除表,则会删除表中的所有行,并从数据库中删除表结构

Truncate the table. 截断表。 That would be good option in your case 在您的情况下,这将是一个不错的选择

We can rollback the data in conditions of Delete, Truncate & Drop. 我们可以在Delete,Truncate和Drop的条件下回滚数据。 But must be used Begin Transaction before executing query Delete, Drop & Truncate. 但是必须在执行查询“删除,删除和截断”之前使用“开始事务”。

Here is example : 这是示例:

Create Database Ankit

Create Table Tbl_Ankit(Name varchar(11))

insert into tbl_ankit(name) values('ankit');
insert into tbl_ankit(name) values('ankur');
insert into tbl_ankit(name) values('arti');

Select * From Tbl_Ankit

/*======================For Delete==================*/
Begin Transaction
Delete From Tbl_Ankit where Name='ankit'

Rollback
Select * From Tbl_Ankit

/*======================For Truncate==================*/
Begin Transaction
Truncate Table Tbl_Ankit 

Rollback
Select * From Tbl_Ankit

/*======================For Drop==================*/
Begin Transaction
Drop Table Tbl_Ankit 

Rollback
Select * From Tbl_Ankit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM