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)
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



