首页 > 开发 > 综合 > 正文

删除数据

2024-07-21 02:49:13
字体:
来源:转载
供稿:网友
删除数据

  我们有两种方式删除数据,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;GOSELECT * INTO dbo.Orders FROM TSQLFundamentals2008.Sales.Orders;SELECT * INTO dbo.Customers FROM TSQLFundamentals2008.Sales.Customers;ALTER TABLE dbo.Customers ADDCONSTRAINT PK_Customers PRIMARY KEY(custid);ALTER TABLE dbo.Orders ADDCONSTRAINT PK_Orders PRIMARY KEY(orderid),CONSTRAINT FK_Orders_Customers FOREIGN KEY(custid) REFERENCES dbo.Customers(custid);-- 删除数据-- 方法一:DELETE(完整模式日志,性能低)DELETE FROM dbo.OrdersWHERE orderdate<'20070101';-- 基于联接的DELETEDELETE FROM orders FROM dbo.Orders AS ordersLEFT JOIN dbo.Customers AS customers ON orders.custid= customers.custidWHERE customers.country= N'USA';-- 方法三:TRUNCATE(最小模式日志,性能高;重置标识种子)TRUNCATE TABLE dbo.Orders;


上一篇:动态SQL基础

下一篇:插入数据

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表