You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
23
23
24
24
> [!NOTE]
25
25
> 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).
## <aname="SSMSProcedure"></a> Use SQL Server Management Studio (SSMS)
31
+
## <aid="SSMSProcedure"></a> Use SQL Server Management Studio (SSMS)
32
32
33
-
### To create a unique constraint using SSMS
33
+
### <aid="to-create-a-unique-constraint-using-ssms"></a> Create a unique constraint using SSMS
34
34
35
35
1. In **Object Explorer**, right-click the table to which you want to add a unique constraint, and select **Design**.
36
36
37
-
2. On the **Table Designer** menu, select **Indexes/Keys**.
37
+
1. On the **Table Designer** menu, select **Indexes/Keys**.
38
38
39
-
3. In the **Indexes/Keys** dialog box, select **Add**.
39
+
1. In the **Indexes/Keys** dialog box, select **Add**.
40
40
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**.
42
42
43
-
5. On the **File** menu, select **Save**_table name_.
43
+
1. On the **File** menu, select **Save _table name_**.
44
44
45
-
## <aname="TsqlExample"></a><aname="TsqlProcedure"></a> Use Transact-SQL
45
+
## <aid="TsqlExample"></a><aname="TsqlProcedure"></a> Use Transact-SQL
46
46
47
-
### To create a unique constraint using Transact-SQL
47
+
### <aid="to-create-a-unique-constraint-using-transact-sql"></a> Create a unique constraint using Transact-SQL
48
48
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)].
50
50
51
-
2. On the Standard bar, select **New Query**.
51
+
1. On the **Standard** bar, select **New Query**.
52
52
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`.
54
54
55
55
```sql
56
56
USE AdventureWorks2022;
@@ -63,13 +63,13 @@ Requires ALTER permission on the table.
63
63
GO
64
64
```
65
65
66
-
### To createa 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
67
67
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)].
69
69
70
-
2. On the Standard bar, select**New Query**.
70
+
1. On the **Standard** bar, select**New Query**.
71
71
72
-
3. Copy and paste the following example into the query window andselect**Execute**. The example creates a unique constrainton the columns `PasswordHash`and`PasswordSalt`in the table `Person.Password`.
72
+
1. Copy and paste the following example into the query window andselect**Execute**. The example creates a unique constrainton the columns `PasswordHash`and`PasswordSalt`in the table `Person.Password`.
73
73
74
74
```sql
75
75
USE AdventureWorks2022;
@@ -80,13 +80,13 @@ Requires ALTER permission on the table.
80
80
81
81
```
82
82
83
-
### To createa 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
84
84
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)].
86
86
87
-
2. On the Standard bar, select**New Query**.
87
+
1. On the **Standard** bar, select**New Query**.
88
88
89
-
3. Copy and paste the following example into the query window andselect**Execute**. The example creates a table and defines a unique constrainton the column `TransactionID`.
89
+
1. Copy and paste the following example into the query window andselect**Execute**. The example creates a table and defines a unique constrainton the column `TransactionID`.
90
90
91
91
```sql
92
92
USE AdventureWorks2022;
@@ -97,10 +97,27 @@ Requires ALTER permission on the table.
97
97
CONSTRAINT AK_TransactionID UNIQUE(TransactionID)
98
98
);
99
99
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 andselect**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
0 commit comments