Skip to content

Commit 232ddde

Browse files
Merge pull request #29376 from WilliamDAssafMSFT/20231220-jzabroski-patch-25
20231220 jzabroski patch 25
2 parents 1e9efc3 + dc1513b commit 232ddde

1 file changed

Lines changed: 44 additions & 27 deletions

File tree

docs/relational-databases/tables/create-unique-constraints.md

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Create unique constraints"
33
description: "This article shows you how to create unique constraints using SQL Server Management Studio and Transact-SQL."
44
author: WilliamDAssafMSFT
55
ms.author: wiassaf
6-
ms.date: "09/27/2023"
6+
ms.date: 12/20/2023
77
ms.service: sql
88
ms.subservice: table-view-index
99
ms.topic: conceptual
@@ -19,38 +19,38 @@ monikerRange: "=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||
1919

2020
[!INCLUDE [sqlserver2016-asdb-asdbmi](../../includes/applies-to-version/sqlserver2016-asdb-asdbmi.md)]
2121

22-
You can create a unique constraint in [!INCLUDE[ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE[ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE[tsql](../../includes/tsql-md.md)] to ensure no duplicate values are entered in specific columns that don't participate in a primary key. Creating a unique constraint automatically creates a corresponding unique index.
22+
You can create a unique constraint in [!INCLUDE [ssnoversion](../../includes/ssnoversion-md.md)] by using [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] or [!INCLUDE [tsql](../../includes/tsql-md.md)] to ensure no duplicate values are entered in specific columns that don't participate in a primary key. Creating a unique constraint automatically creates a corresponding unique index.
2323

2424
> [!NOTE]
2525
> For information on unique constraints in Azure Synapse Analytics, see [Primary key, foreign key, and unique key in Azure Synapse Analytics](/azure/sql-data-warehouse/sql-data-warehouse-table-constraints).
2626
27-
## <a name="Security"></a><a name="Permissions"></a> Permissions
27+
## <a id="Security"></a> <a name="Permissions"></a> Permissions
2828

2929
Requires ALTER permission on the table.
3030

31-
## <a name="SSMSProcedure"></a> Use SQL Server Management Studio (SSMS)
31+
## <a id="SSMSProcedure"></a> Use SQL Server Management Studio (SSMS)
3232

33-
### To create a unique constraint using SSMS
33+
### <a id="to-create-a-unique-constraint-using-ssms"></a> Create a unique constraint using SSMS
3434

3535
1. In **Object Explorer**, right-click the table to which you want to add a unique constraint, and select **Design**.
3636

37-
2. On the **Table Designer** menu, select **Indexes/Keys**.
37+
1. On the **Table Designer** menu, select **Indexes/Keys**.
3838

39-
3. In the **Indexes/Keys** dialog box, select **Add**.
39+
1. In the **Indexes/Keys** dialog box, select **Add**.
4040

41-
4. In the grid under **General**, select **Type** and choose **Unique Key** from the drop-down list box to the right of the property, and then select **Close**.
41+
1. In the grid under **General**, select **Type** and choose **Unique Key** from the dropdown list box to the right of the property, and then select **Close**.
4242

43-
5. On the **File** menu, select **Save** _table name_.
43+
1. On the **File** menu, select **Save _table name_**.
4444

45-
## <a name="TsqlExample"></a><a name="TsqlProcedure"></a> Use Transact-SQL
45+
## <a id="TsqlExample"></a> <a name="TsqlProcedure"></a> Use Transact-SQL
4646

47-
### To create a unique constraint using Transact-SQL
47+
### <a id="to-create-a-unique-constraint-using-transact-sql"></a> Create a unique constraint using Transact-SQL
4848

49-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
49+
1. In **Object Explorer**, connect to an instance of [!INCLUDE [ssDE](../../includes/ssde-md.md)].
5050

51-
2. On the Standard bar, select **New Query**.
51+
1. On the **Standard** bar, select **New Query**.
5252

53-
3. Copy and paste the following example into the query window and select **Execute**. The example creates the table `TransactionHistoryArchive4` and creates a unique constraint on the column `TransactionID`.
53+
1. Copy and paste the following example into the query window and select **Execute**. The example creates the table `TransactionHistoryArchive4` and creates a unique constraint on the column `TransactionID`.
5454

5555
```sql
5656
USE AdventureWorks2022;
@@ -63,13 +63,13 @@ Requires ALTER permission on the table.
6363
GO
6464
```
6565

66-
### To create a unique constraint on an existing table
66+
### <a id="to-create-a-unique-constraint-on-an-existing-table"></a> Create a unique constraint on an existing table
6767

68-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
68+
1. In **Object Explorer**, connect to an instance of [!INCLUDE [ssDE](../../includes/ssde-md.md)].
6969

70-
2. On the Standard bar, select **New Query**.
70+
1. On the **Standard** bar, select **New Query**.
7171

72-
3. Copy and paste the following example into the query window and select **Execute**. The example creates a unique constraint on the columns `PasswordHash` and `PasswordSalt` in the table `Person.Password`.
72+
1. Copy and paste the following example into the query window and select **Execute**. The example creates a unique constraint on the columns `PasswordHash` and `PasswordSalt` in the table `Person.Password`.
7373

7474
```sql
7575
USE AdventureWorks2022;
@@ -80,13 +80,13 @@ Requires ALTER permission on the table.
8080
8181
```
8282

83-
### To create a unique constraint on a new table
83+
### <a id="to-create-a-unique-constraint-on-a-new-table"></a> Create a unique constraint on a new table
8484

85-
1. In **Object Explorer**, connect to an instance of [!INCLUDE[ssDE](../../includes/ssde-md.md)].
85+
1. In **Object Explorer**, connect to an instance of [!INCLUDE [ssDE](../../includes/ssde-md.md)].
8686

87-
2. On the Standard bar, select **New Query**.
87+
1. On the **Standard** bar, select **New Query**.
8888

89-
3. Copy and paste the following example into the query window and select **Execute**. The example creates a table and defines a unique constraint on the column `TransactionID`.
89+
1. Copy and paste the following example into the query window and select **Execute**. The example creates a table and defines a unique constraint on the column `TransactionID`.
9090

9191
```sql
9292
USE AdventureWorks2022;
@@ -97,10 +97,27 @@ Requires ALTER permission on the table.
9797
CONSTRAINT AK_TransactionID UNIQUE(TransactionID)
9898
);
9999
GO
100+
```
101+
102+
### Create a unique constraint on a nullable column
103+
104+
1. In **Object Explorer**, connect to an instance of [!INCLUDE [ssDE](../../includes/ssde-md.md)].
105+
106+
1. On the **Standard** bar, select **New Query**.
107+
108+
1. Copy and paste the following example into the query window and select **Execute**. The example creates a [filtered](../indexes/create-filtered-indexes.md) unique constraint using the `CREATE UNIQUE INDEX` syntax, only enforcing uniqueness on non-`NULL` values.
109+
110+
```sql
111+
USE AdventureWorks2022;
112+
GO
113+
CREATE UNIQUE INDEX UQ_AdventureWorksDWBuildVersion
114+
ON dbo.AdventureWorksDWBuildVersion (DBVersion)
115+
WHERE (DBVersion IS NOT NULL);
116+
GO
100117
```
101118

102-
## Next steps
103-
104-
- [ALTER TABLE &#40;Transact-SQL&#41;](../../t-sql/statements/alter-table-transact-sql.md)
105-
- [CREATE TABLE &#40;Transact-SQL&#41;](../../t-sql/statements/create-table-transact-sql.md)
106-
- [table_constraint &#40;Transact-SQL&#41;](../../t-sql/statements/alter-table-table-constraint-transact-sql.md)
119+
## Related content
120+
121+
- [ALTER TABLE (Transact-SQL)](../../t-sql/statements/alter-table-transact-sql.md)
122+
- [CREATE TABLE (Transact-SQL)](../../t-sql/statements/create-table-transact-sql.md)
123+
- [table_constraint (Transact-SQL)](../../t-sql/statements/alter-table-table-constraint-transact-sql.md)

0 commit comments

Comments
 (0)