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
Delete all the records
提问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_name
use 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:
Then another window will open with a script. Delete the line of "where", because you want to delete all rows. Then click Execute.
然后另一个窗口将打开一个脚本。删除“where”这一行,因为要删除所有行。然后单击执行。
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 TableName
and recreate it, if one has create table query; rather than deleting records one by one, using delete from
statement because that can be time consuming.
当表非常大drop table TableName
时,如果有创建表查询,最好删除表本身并重新创建它;而不是一个一个地删除记录,使用delete from
语句,因为这可能很耗时。