對這文章發表回應
發表限制: 非會員 可以發表
建立唯一的條件約束 Create Unique Constraints
您可以使用 SQL Server 2017 SQL Server 2017 或 Transact-SQL SQL Server Management Studio ,在 Transact-SQL Transact-SQL 中建立唯一條件約束,確保在沒有參與主索引鍵之特定資料行中輸入的值不會重複。 You can create a unique constraint in SQL Server 2017 SQL Server 2017 by using Transact-SQL SQL Server Management Studio or Transact-SQL Transact-SQL to ensure no duplicate values are entered in specific columns that do not participate in a primary key. 建立唯一條件約束會自動建立對應的唯一索引。 Creating a unique constraint automatically creates a corresponding unique index.
開始之前 Before You Begin
安全性 Security
權限 Permissions
需要資料表的 ALTER 權限。 Requires ALTER permission on the table.
使用 SQL Server Management Studio Using SQL Server Management Studio
若要建立唯一條件約束 To create a unique constraint
在物件總管中,以滑鼠右鍵按一下要加入唯一條件約束的資料表,然後按一下 [設計]。 In Object Explorer, right-click the table to which you want to add a unique constraint, and click Design.
在 [資料表設計工具] 功能表上,按一下 [索引/索引鍵]。 On the Table Designer menu, click Indexes/Keys.
在 [索引/索引鍵] 對話方塊中,按一下 [加入]。 In the Indexes/Keys dialog box, click Add.在 [一般] 底下的方格中,按一下 [類型],然後從屬性右邊的下拉式清單方塊中選擇 [唯一索引鍵]。 In the grid under General, click Type and choose Unique Key from the drop-down list box to the right of the property.
在 [檔案] 功能表上,按一下 [儲存 <資料表名稱>]。 On the File menu, click Save table name.
使用 Transact-SQL Using Transact-SQL
若要建立唯一條件約束 To create a unique constraint
在 [物件總管] 中,連接到 Database Engine Database Engine的執行個體。
In Object Explorer, connect to an instance of Database Engine Database Engine.在標準列上,按一下 [新增查詢]。 On the Standard bar, click New Query.
複製下列範例並將其貼到查詢視窗中,然後按一下 [執行]。 Copy and paste the following example into the query window and click Execute. 此範例會建立
TransactionHistoryArchive4
資料表並且在TransactionID
資料行上建立唯一條件約束。 The example creates the tableTransactionHistoryArchive4
and creates a unique constraint on the columnTransactionID
.USE AdventureWorks2012;
GO
CREATE TABLE Production.TransactionHistoryArchive4
(
TransactionID int NOT NULL,
CONSTRAINT AK_TransactionID UNIQUE(TransactionID)
);
GO
若要在現有的資料表上建立唯一條件約束 To create a unique constraint on an existing table
在 [物件總管] 中,連接到 Database Engine Database Engine的執行個體。 In Object Explorer, connect to an instance of Database Engine Database Engine.
在標準列上,按一下 [新增查詢]。 On the Standard bar, click New Query.
複製下列範例並將其貼到查詢視窗中,然後按一下 [執行]。 Copy and paste the following example into the query window and click Execute. 此範例會在
PasswordHash
資料表中的PasswordSalt
和Person.Password
資料行上建立唯一條件約束。
The example creates a unique constraint on the columnsPasswordHash
andPasswordSalt
in the tablePerson.Password
.USE AdventureWorks2012;
GO
ALTER TABLE Person.Password
ADD CONSTRAINT AK_Password UNIQUE (PasswordHash, PasswordSalt);
GO
若要在新的資料表中建立唯一條件約束 To create a unique constraint in an new table
在 [物件總管] 中,連接到 Database Engine Database Engine的執行個體。 In Object Explorer, connect to an instance of Database Engine Database Engine.
在標準列上,按一下 [新增查詢]。 On the Standard bar, click New Query.
複製下列範例並將其貼到查詢視窗中,然後按一下 [執行]。 Copy and paste the following example into the query window and click Execute. 此範例會建立資料表並且在
TransactionID
資料行上定義唯一條件約束。 The example creates a table and defines a unique constraint on the columnTransactionID
.USE AdventureWorks2012;
GO
CREATE TABLE Production.TransactionHistoryArchive2
(
TransactionID int NOT NULL,
CONSTRAINT AK_TransactionID UNIQUE(TransactionID)
);
GO如需詳細資訊,請參閱 ALTER TABLE (Transact-SQL)、 CREATE TABLE (Transact-SQL) 和 table_constraint (Transact-SQL)。 For more information, see ALTER TABLE (Transact-SQL), CREATE TABLE (Transact-SQL), and
table_constraint (Transact-SQL).
原文出處:建立唯一的條件約束 | Microsoft Docs