| title | Create Unique Constraints | Microsoft Docs | |||
|---|---|---|---|---|
| ms.custom | ||||
| ms.date | 03/17/2020 | |||
| ms.prod | sql | |||
| ms.prod_service | database-engine, sql-database | |||
| ms.reviewer | ||||
| ms.technology | table-view-index | |||
| ms.topic | conceptual | |||
| f1_keywords |
|
|||
| helpviewer_keywords |
|
|||
| ms.assetid | a86f9d6f-f242-43be-b65d-b3435b71b62a | |||
| author | stevestein | |||
| ms.author | sstein | |||
| monikerRange | =azuresqldb-current||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017||=azuresqldb-mi-current |
[!INCLUDEtsql-appliesto-ss2016-asdb-xxxx-xxx-md]
You can create a unique constraint in [!INCLUDEssCurrent] by using [!INCLUDEssManStudioFull] or [!INCLUDEtsql] 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.
Note
See Primary key, foreign key, and unique key in Azure Synapse Analytics for information on unique constraints in Azure Synapse Analytics.
In This Topic
-
Before you begin:
-
To create a unique constraint, using:
Requires ALTER permission on the table.
-
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 Savetable name.
-
In Object Explorer, connect to an instance of [!INCLUDEssDE].
-
On the Standard bar, click New Query.
-
Copy and paste the following example into the query window and click Execute. The example creates the table
TransactionHistoryArchive4and creates a unique constraint on the columnTransactionID.USE AdventureWorks2012; GO CREATE TABLE Production.TransactionHistoryArchive4 ( TransactionID int NOT NULL, CONSTRAINT AK_TransactionID UNIQUE(TransactionID) ); GO
-
In Object Explorer, connect to an instance of [!INCLUDEssDE].
-
On the Standard bar, click New Query.
-
Copy and paste the following example into the query window and click Execute. The example creates a unique constraint on the columns
PasswordHashandPasswordSaltin the tablePerson.Password.USE AdventureWorks2012; GO ALTER TABLE Person.Password ADD CONSTRAINT AK_Password UNIQUE (PasswordHash, PasswordSalt); GO
-
In Object Explorer, connect to an instance of [!INCLUDEssDE].
-
On the Standard bar, click New Query.
-
Copy and paste the following example into the query window and click Execute. The example creates a table and defines a unique constraint on the column
TransactionID.USE AdventureWorks2012; GO CREATE TABLE Production.TransactionHistoryArchive2 ( TransactionID int NOT NULL, CONSTRAINT AK_TransactionID UNIQUE(TransactionID) ); GOFor more information, see ALTER TABLE (Transact-SQL), CREATE TABLE (Transact-SQL), and table_constraint (Transact-SQL).