21、SQL Server数据库对象操作指南

SQL Server数据库对象操作指南

1. 唯一约束

唯一约束用于确保表中某列或多列组合的值是唯一的。可以使用 ALTER TABLE 语句来创建和删除唯一约束。

1.1 创建唯一约束

以下代码展示了如何使用 ALTER TABLE 语句创建唯一约束:

USE AdventureWorks2008R2  
GO 
--Drop the table if it currently exists  
IF OBJECT_ID('dbo.OrderDetails', 'U') IS NOT NULL  
  DROP TABLE dbo.OrderDetails; 
--Create the OrderDetails table 
CREATE TABLE dbo.OrderDetails 
(OrderID int NOT NULL  
   CONSTRAINT PK_ORDERDETAILS PRIMARY KEY CLUSTERED, 
OrderNumber int NULL, 
CustomerNumber int NOT NULL) 
--Alter the OrderDetails table to add the unique constraint  
ALTER TABLE dbo.OrderDetails  
ADD CONSTRAINT UQ_ORDER_CUSTOMER_NBR UNIQUE  
 (OrderNumber, CustomerNumber)
1.2 删除唯一约束 <
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值