SQL 删除所有记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5356755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:46:20  来源:igfitidea点击:

Delete all the records

sqlsql-server-2008

提问by kaveh

How to delete all the records in SQL Server 2008?

如何删除 SQL Server 2008 中的所有记录?

回答by PA.

To delete all records from a table without deleting the table.

删除表中的所有记录而不删除表。

DELETE FROM table_nameuse with care, there is no undo!

DELETE FROM table_name小心使用,不可撤销!

To remove a table

删除表格

DROP TABLE table_name

DROP TABLE table_name

回答by SQLMenace

from a table?

从一张桌子?

You can use this if you have no foreign keys to other tables

如果您没有其他表的外键,您可以使用它

truncate table TableName

or

或者

delete TableName

if you want all tables

如果你想要所有的桌子

sp_msforeachtable 'delete ?'

回答by Cyberguille

I can see the that the others answers shown above are right, but I'll make your life easy.

我可以看到上面显示的其他答案是正确的,但我会让你的生活更轻松。

I even created an example for you. I added some rows and want delete them.

我什至为你创建了一个例子。我添加了一些行并想删除它们。

You have to right click on the table and as shown in the figure Script Table a> Delete to> New query Editor widows:

您必须右键单击该表,如图所示 Script Table a> Delete to> New query Editor widows:

enter image description here

在此处输入图片说明

Then another window will open with a script. Delete the line of "where", because you want to delete all rows. Then click Execute.

然后另一个窗口将打开一个脚本。删除“where”这一行,因为要删除所有行。然后单击执行。

enter image description here

在此处输入图片说明

To make sure you did it right right click over the table and click in "Select Top 1000 rows". Then you can see that the query is empty.

为确保您正确执行此操作,请右键单击表格并单击“选择前 1000 行”。然后你可以看到查询是空的。

回答by sachind

Use the DELETE statement

使用 DELETE 语句

Delete From <TableName>

Eg:

例如:

Delete from Student;

回答by fdaines

If you want to reset your table, you can do

如果你想重置你的桌子,你可以这样做

truncate table TableName

truncate needs privileges, and you can't use it if your table has dependents (another tables that have FK of your table,

truncate 需要特权,如果您的表有依赖项(另一个表具有您的表的 FK,

回答by Dumitrescu Bogdan

For one table

一张桌子

truncate table [table name]

For all tables

对于所有表

EXEC sp_MSforeachtable @command1="truncate table ?"

回答by Shahzod1011

When the table is very large, it's better to delete table itself with drop table TableNameand recreate it, if one has create table query; rather than deleting records one by one, using delete fromstatement because that can be time consuming.

当表非常大drop table TableName时,如果有创建表查询,最好删除表本身并重新创建它;而不是一个一个地删除记录,使用delete from语句,因为这可能很耗时。