Unique key
   HOME

TheInfoList



OR:

In relational database management systems, a unique key is a
candidate key A candidate key, or simply a key, of a relational database is a minimal superkey. In other words, it is any set of columns that have a unique combination of values in each row (which makes it a superkey), with the additional constraint that removin ...
that is not the
primary key In the relational model of databases, a primary key is a ''specific choice'' of a ''minimal'' set of attributes ( columns) that uniquely specify a tuple ( row) in a relation ( table). Informally, a primary key is "which attributes identify a recor ...
of the relation. All the candidate keys of a relation can uniquely identify the records of the relation, but only one of them is used as the primary key of the relation. The remaining candidate keys are called unique keys because they can uniquely identify a record in a relation. Unique keys can consist of multiple columns. Unique keys are also called
alternate key In the relational model of databases, a primary key is a ''specific choice'' of a ''minimal'' set of attributes (columns) that uniquely specify a tuple ( row) in a relation (table). Informally, a primary key is "which attributes identify a record, ...
s. Unique keys are an alternative to the primary key of the relation. Generally, the unique keys have a UNIQUE constraint assigned to it in order to prevent duplicates (a duplicate entry is not valid in a unique column). Alternate keys may be used like the primary key when doing a single-table select or when filtering in a ''where'' clause, but are not typically used to join multiple tables.


Summary

Keys provide the means for database users and application software to identify, access and update information in a database table. ''There may be several keys in any given table.'' For example, in a table of employees, both employee number and login name are individually unique. The enforcement of a key constraint (i.e. a uniqueness constraint) in a table is also a data integrity feature of the database. The DBMS prevents updates that would cause duplicate key values and thereby ensures that tables always comply with the desired rules for uniqueness. Proper selection of keys when designing a database is therefore an important aspect of database integrity. A relational database table may have one or more available unique keys (formally called
candidate key A candidate key, or simply a key, of a relational database is a minimal superkey. In other words, it is any set of columns that have a unique combination of values in each row (which makes it a superkey), with the additional constraint that removin ...
s). One of those keys per table may be designated the
primary key In the relational model of databases, a primary key is a ''specific choice'' of a ''minimal'' set of attributes ( columns) that uniquely specify a tuple ( row) in a relation ( table). Informally, a primary key is "which attributes identify a recor ...
; other keys are called
alternate key In the relational model of databases, a primary key is a ''specific choice'' of a ''minimal'' set of attributes (columns) that uniquely specify a tuple ( row) in a relation (table). Informally, a primary key is "which attributes identify a record, ...
s. Any key may consist of one or more attributes. For example, a
Social Security Number In the United States, a Social Security number (SSN) is a nine-digit number issued to U.S. citizens, permanent residents, and temporary (working) residents under section 205(c)(2) of the Social Security Act, codified as . The number is issued to ...
might be a single attribute key for an employee; a combination of flight number and date might be a key consisting of two attributes for a scheduled flight. There are several types of keys used in
database model A database model is a type of data model that determines the logical structure of a database. It fundamentally determines in which manner data can be stored, organized and manipulated. The most popular example of a database model is the relation ...
ing and implementations. At the most basic definition, "a key is a unique identifier", so ''unique'' key is a pleonasm. Keys that are within their originating entity are unique within that entity. Keys that migrate to another entity may or may not be unique, depending on the design and how they are used in the other table. Foreign keys may be the primary key in another table; for example a PersonID may become the EmployeeID in the Employee table. In this case, the EmployeeID is both a foreign key and the unique primary key, meaning that the tables have a 1:1 relationship. In the case where the person entity contained the biological father ID, the father ID would not be expected to be unique because a father may have more than one child. Here is an example of a primary key becoming a foreign key on a related table. ID migrates from the Author table to the Book table. Author Table Schema: Author(ID, Name, Address, Born) Book Table Schema: Book(ISBN, AuthorID, Title, Publisher, Price) Here ID serves as the primary key in the table 'Author', but also as AuthorID serves as a Foreign Key in the table 'Book'. The Foreign Key serves as the link, and therefore the connection, between the two related tables in this sample database. In a relational database, a candidate key uniquely identifies each row of data values in a database table. A candidate key comprises a single
column A column or pillar in architecture and structural engineering is a structural element that transmits, through compression (physical), compression, the weight of the structure above to other structural elements below. In other words, a column i ...
or a set of columns in a single database table. No two distinct rows or data records in a database table can have the same data value (or combination of data values) in those candidate key columns since NULL values are not used. Depending on its design, a database table may have many candidate keys but at most one candidate key may be distinguished as the primary key. A key constraint applies to the set of tuples in a table at any given point in time. A key is not necessarily a unique identifier across the population of all ''possible'' instances of tuples that could be stored in a table but it does imply a data integrity rule that duplicates should not be allowed in the database table. Some possible examples of keys are
Social Security Number In the United States, a Social Security number (SSN) is a nine-digit number issued to U.S. citizens, permanent residents, and temporary (working) residents under section 205(c)(2) of the Social Security Act, codified as . The number is issued to ...
s,
ISBN The International Standard Book Number (ISBN) is a numeric commercial book identifier that is intended to be unique. Publishers purchase ISBNs from an affiliate of the International ISBN Agency. An ISBN is assigned to each separate edition a ...
s, vehicle registration numbers or user login names. In principle any key may be referenced by foreign keys. Some SQL
DBMS In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases ...
s only allow a foreign key constraint against a primary key but most systems will allow a foreign key constraint to reference any key of a table.


Defining keys in SQL

The definition of keys in SQL: ALTER TABLE ADD CONSTRAINT ( ... ) Likewise, keys can be defined as part of the CREATE TABLE SQL statement. CREATE TABLE table_name ( id_col INT, col2 CHARACTER VARYING(20), key_col SMALLINT NOT NULL, ... CONSTRAINT key_unique UNIQUE(key_col), ... ) CREATE TABLE table_name ( id_col INT PRIMARY KEY, col2 CHARACTER VARYING(20), ... key_col SMALLINT NOT NULL UNIQUE, ... )


Differences between Primary Key constraint and Unique constraint

Primary Key constraint # A primary key ''cannot'' allow null (a primary key cannot be defined on columns that allow nulls). # Each table cannot have more than one primary key. # On some RDBMS a primary key generates a
clustered index A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure. Indexes are used to quickly locate data withou ...
by default. Unique constraint # A unique constraint can be defined on columns that allow nulls, in which case rows that include nulls may not actually be unique across the set of columns defined by the constraint. # Each table can have multiple unique constraints. # On some RDBMS a unique constraint generates a
nonclustered index A database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure. Indexes are used to quickly locate data withou ...
by default. Note that unlike the PRIMARY KEY constraint a UNIQUE constraint does not imply NOT NULL for the columns participating in the constraint. NOT NULL must be specified to make the column(s) a key. It is possible to put UNIQUE constraints on nullable columns but the SQL standard states that the constraint does not guarantee uniqueness of nullable columns (uniqueness is not enforced for rows where any of the columns contains a null). According to the SQL standard a unique constraint does not enforce uniqueness in the presence of nulls and can therefore contain several rows with identical combinations of nulls and non-null values — however not all RDBMS implement this feature according to the SQL standard.


See also

*
Globally Unique Identifier A universally unique identifier (UUID) is a 128-bit label used for information in computer systems. The term globally unique identifier (GUID) is also used. When generated according to the standard methods, UUIDs are, for practical purposes, u ...
* Persistent Object Identifier


References


External links


Relation Database terms of reference, Keys
An overview of the different types of keys in an RDBMS {{DEFAULTSORT:Unique Key Data modeling