Unique key
In database relational modeling and implementation, a unique key is a set of attributes within a relational database table, such that:
- the table does not have two distinct rows or records with the same values for these columns;
- this set of columns is minimal; i.e., removing any column from the key would result in duplicate values in the resulting subset.
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 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. One of those keys per table may be designated the primary key; other keys are called alternate keys.
Any key may consist of one or more attributes. For example, a Social Security Number 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 modeling and implementations.
Key Name | Definition |
Simple | A key made from only one attribute. |
Concatenated | A key made from more than one attribute joined together as a single key, such as part or whole name with a system generated number appended as often used for E-mail addresses. |
Compound | A key made from at least two attributes or simple keys, only simple keys exist in a compound key. |
Composite | A key containing at least one compound key with at least one other attribute or simple key. |
Natural | A key made from data that exists outside the current database. In other words, the data is not system generated, such as a social security number imported from another system. |
Surrogate | An artificial key made from data that is system assigned or generated when another candidate key exists. Surrogate keys are usually numeric ID values and often used for performance reasons. |
Candidate | A key that may become the primary key. |
Primary | The key that is selected as the primary key. Only one key within an entity is selected to be the primary key. This is the key that is allowed to migrate to other entities to define the relationships that exist among the entities. When the data model is instantiated into a physical database, it is the key that the system uses the most when accessing the table, or joining the tables together when selecting data. |
Alternate | A non-primary key that can be used to identify only one row in a table. Alternate keys may be used like a primary key in a single-table select. |
Foreign | A key that has migrated to another entity. |
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
Book Table Schema:
Book
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 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 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 Numbers, ISBNs, vehicle registration numbers or user login names.
In principle any key may be referenced by foreign keys. Some SQL DBMSs 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