Sdílet prostřednictvím


How to: Create Unique Constraints (Visual Database Tools)

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.

To create a unique constraint

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

    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.