Saturday, May 1, 2010

Unique key

0 comments
In relational database design, a unique key can uniquely identify each row in a table, and is closely related to the Superkey concept. A unique key comprises a single column or a set of columns. No two distinct rows in a table can have the same value (or combination of values) in those columns if NULL values are not used. Depending on its design, a table may have arbitrarily many unique keys but at most one primary key.

Unique keys do not enforce the NOT NULL constraint in practice. Because NULL is not an actual value (it represents the lack of a value), when two rows are compared, and both rows have NULL in a column, the column values are not considered to be equal. Thus, in order for a unique key to uniquely identify each row in a table, NULL values must not be used.
 Example:

CREATE TABLE table_name (


id_col INT,
col2 CHARACTER VARYING(20),
key_col SMALLINT,
CONSTRAINT key_unique UNIQUE(key_col),
)

0 comments: