码迷,mamicode.com
首页 > 其他好文 > 详细

删除数据

时间:2014-09-18 13:00:03      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   数据   div   sp   代码   

  我们有两种方式删除数据,DELETE语句和TRUNCATE语句。需要注意它们的区别,DELETE语句是以完整模式记录日志的,所以它的性能相对较低。而TRUNCATE语句是以最小模式来记录日志的,并且它会重置标识种子。下面是示例代码。

USE tempdb;
GO

-- 准备测试数据
IF OBJECT_ID(dbo.Orders,U) IS NOT NULL DROP TABLE dbo.Orders;
IF OBJECT_ID(dbo.Customers,U) IS NOT NULL DROP TABLE dbo.Customers;
GO

SELECT * INTO dbo.Orders FROM TSQLFundamentals2008.Sales.Orders;
SELECT * INTO dbo.Customers FROM TSQLFundamentals2008.Sales.Customers;

ALTER TABLE dbo.Customers ADD
CONSTRAINT PK_Customers PRIMARY KEY(custid);

ALTER TABLE dbo.Orders ADD
CONSTRAINT PK_Orders PRIMARY KEY(orderid),
CONSTRAINT FK_Orders_Customers FOREIGN KEY(custid) REFERENCES dbo.Customers(custid);

-- 删除数据
-- 方法一:DELETE(完整模式日志,性能低)
DELETE FROM dbo.Orders
WHERE orderdate<20070101;

-- 基于联接的DELETE
DELETE FROM orders 
FROM dbo.Orders AS orders
LEFT JOIN dbo.Customers AS customers ON orders.custid= customers.custid
WHERE customers.country= NUSA;

-- 方法三:TRUNCATE(最小模式日志,性能高;重置标识种子)
TRUNCATE TABLE dbo.Orders;

 

删除数据

标签:style   blog   color   ar   for   数据   div   sp   代码   

原文地址:http://www.cnblogs.com/mcgrady/p/3978907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!