Creating unique constraints

A unique constraint helps maintain data integrity. It ensures that for specified columns, no two rows in a table will have the same non-null value. Unique constraints are created as indexes on a table.

The definition of unique constraints in the SQL standards specifies that the column definition shall not allow null values. By default, Adaptive Server defines the column as not allowing null values (if you have not changed this using sp_dboption) when you omit null or not null keywords in the column definition.

Do not confuse the unique and primary key integrity constraints with the information defined by sp_primarykey, sp_foreignkey, and sp_commonkey. The unique and primary key constraints actually create indexes to define unique or primary key attributes of table columns. sp_primarykey, sp_foreignkey, and sp_commonkey define the logical relationship of keys (in the syskeys table) for table columns, which you enforce by creating indexes and triggers.

Unique constraints create unique nonclustered indexes by default; primary key constraints create unique clustered indexes by default. You can declare either clustered or nonclustered indexes with either type of constraint.

For example, the following create table statement uses a table-level unique constraint to ensure that no two rows have the same values in the stor_id and ord_num columns:

create table my_sales
(stor_id     char(4),
ord_num      varchar(20),
date         datetime,
unique clustered (stor_id, ord_num)) 

There can be only one clustered index on a table, so you can specify only one unique clustered or primary key clustered constraint.

You can use the unique and primary key constraints to create unique indexes (including the with fillfactor, with max_rows_per_page, and on segment_name options) when enforcing data integrity. However, indexes provide additional capabilities.

StepsCreating a unique constraint

  1. In the table, select the Unique Constraints folder.

  2. Select File | New | Unique Constraint. The Unique Table Constraint Creation wizard opens.

The Unique Table Constraint Creation wizard asks for the following information:

Table 17-2: Inputs to Unique Table Constraint Creation wizard

Input

Description

Name

Name for the new constraint.

Columns

Select the columns that are part of the constraint.

Segment

Specify the segment that the constraint will be on.

Fill factor

Optionally, specify adjustment of the fill factor and a value. Mutually exclusive with Maximum Rows Per Page.

Rows per page

Optionally, specify a value for maximum rows per page. Mutually exclusive with Fill Factor.

Clustered index

If there are no clustered indexes on this table, you can designate this constraint to be clustered.

Page gap

A ration of filled pages to empty pages to be left during extended I/O allocation operations for the index created by the constraint.

Shortcut

Select the Unique Constraints folder. In the right pane, double-click the Add Unique Constraint icon.