Add-UniqueKey

Creates a UNIQUE constraint on the specified column and table.

Syntax

Add-UniqueKey [-SchemaName <String>] [-TableName] <String> [-ColumnName] <String[]> [-Clustered] [-FillFactor <Int32>] [-Option <String[]>] [-On <String>] [-Name <String>] [<CommonParameters>]

Description

Creates a UNIQUE constraint on the specified column and table.
You can use UNIQUE constraints to make sure that no duplicate values are entered in specific columns that do not participate in a primary key. Although both a UNIQUE constraint and a PRIMARY KEY constraint enforce uniqueness, use a UNIQUE constraint instead of a PRIMARY KEY constraint when you want to enforce the uniqueness of a column, or combination of columns, that is not the primary key.

Parameters

Name Type Description Required? Pipeline Input Default Value
SchemaName String

The schema name of the target table. Defaults to dbo.

false false dbo
TableName String

The name of the target table.

true false
ColumnName String[]

The column(s) on which the index is based

true false
Clustered SwitchParameter

Creates a clustered index, otherwise non-clustered

false false False
FillFactor Int32

FillFactor as Integer

false false 0
Option String[]

An array of index options.

false false
On String

The value of the ON clause, which controls the filegroup/partition to use for the index.

false false
Name String

The name for the . If not given, a sensible name will be created.

false false

EXAMPLE 1

Add-UniqueKey -TableName Cars -ColumnName Year

Adds an unique constraint on column 'Year' in the table 'Cars'

EXAMPLE 2

Add-UniqueKey -TableName 'Cars' -ColumnName 'Year' -Option @('IGNORE_DUP_KEY = ON','ALLOW_ROW_LOCKS = OFF')

Adds an unique constraint on column 'Year' in the table 'Cars' with specified options