Compartir a través de


How to: Create Unique Constraints

Create a unique constraint to ensure no duplicate values are entered in specific columns that do not participate in a primary key. While both a unique constraint and a primary key enforce uniqueness, you should attach a unique constraint instead of a primary key constraint to a table if:

  • You want to enforce uniqueness in a column or combination of columns. You can attach multiple unique constraints to a table, whereas you can attach only one primary key constraint to a table.

  • You want to enforce uniqueness in a column that allows null values. You can attach unique constraints to columns that allow null values, whereas you can attach primary key constraints only to columns that do not allow null values. When you attach a unique constraint to a column allowing null values, you ensure that at most one row will have a null value in the constrained column.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

To create a unique constraint

  1. In Server Explorer, select the table to which you want to add a unique constraint, and from the Database menu click Open Table Definition.

    The table opens in Table Designer.

  2. From the Table Designer menu, click Indexes/Keys.

  3. In the Indexes/Keys dialog box, click Add.

  4. In the grid, click Type and choose Unique Key from the drop-down list box to the right of the property.

The unique constraint is created in the database when you save the table.

See Also

Tasks

How to: Create Unique Indexes

How to: Delete Unique Constraints

Reference

Primary or Unique Key Properties

Concepts

Unique Constraints (Visual Database Tools)

Other Resources

Working with Constraints