Recent from talks
Nothing was collected or created yet.
Unique key
View on WikipediaIn relational database management systems, a unique key is a candidate key. 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 keys. Unique keys are an alternative to the primary key of the relation. In SQL, the unique keys have a UNIQUE constraint assigned to them 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
[edit]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 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 | Like a compound key, but the individual attributes need not be simple keys. |
| 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.[citation needed] |
| 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",[1] 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 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 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
[edit]The definition of keys in SQL:
ALTER TABLE <table identifier>
ADD [ CONSTRAINT <constraint identifier> ]
{ PRIMARY KEY | UNIQUE } ( <column name> [ {, <column name>}... ] )
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
[edit]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 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 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[2] 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.[3][4]
See also
[edit]- Globally unique identifier (GUID), also known as universally unique identifier (UUID)
- Persistent object identifier (POID), a unique identifier of a record on a table, used as the primary key
References
[edit]- ^ Awad, Elias (1985), Systems Analysis and Design, Second Edition, Richard D. Irwin, Inc., ISBN 0-256-02824-9
- ^ Summary of ANSI/ISO/IEC SQL Archived April 25, 2012, at the Wayback Machine
- ^ "Constraints - SQL Database Reference Material - Learn sql, read an sql manual, follow an sql tutorial, or learn how to structure an SQL query!". www.sql.org. Retrieved 16 August 2018.
- ^ "Comparison of different SQL implementations". troels.arvin.dk. Retrieved 16 August 2018.
External links
[edit]- Relation Database terms of reference, Keys: An overview of the different types of keys in an RDBMS
Unique key
View on GrokipediaCREATE TABLE with UNIQUE clauses or ALTER TABLE ADD CONSTRAINT, and violations trigger errors to block invalid inserts or updates.[4][5]
In broader contexts, such as Azure Cosmos DB, unique keys extend this concept to NoSQL environments by enforcing uniqueness within logical partitions, supporting scalable data models while preserving integrity across distributed systems.[9] Unique keys play a critical role in database design by supporting referential integrity indirectly—through foreign keys referencing them—and optimizing query performance via underlying indexes automatically created by most RDBMS.[6][3]
Introduction
Definition
A unique key is a database constraint in relational databases that enforces uniqueness across one or more columns in a table, ensuring that no two rows contain identical values in those columns.[6] This mechanism prevents duplicate entries while allowing the constrained columns to accept NULL values, with multiple NULLs permitted since NULL is treated as distinct from other values and from itself in standard SQL semantics.[3] Unlike primary keys, which prohibit NULLs entirely, unique keys provide a flexible way to maintain distinctness without mandating non-nullability.[3] Unique keys contribute to data integrity by guaranteeing the uniqueness of non-primary identifiers, such as email addresses or usernames in a users table, where duplicates could otherwise lead to inconsistencies in data referencing or application logic. For instance, enforcing a unique key on an email column ensures that each user record has a distinct email value (excluding NULLs), supporting reliable lookups and preventing errors in user authentication systems. The concept of unique keys originated in the early relational database models proposed by E.F. Codd in his 1970 paper, where they align with candidate keys—minimal sets of attributes that uniquely identify tuples and from which a primary key is selected.[10] These ideas were formalized in the SQL language through the ANSI SQL-86 standard, which introduced the UNIQUE constraint as part of its integrity mechanisms.[11]Role in Data Integrity
Unique keys contribute to data integrity within relational databases by ensuring that no two rows contain identical values in the specified column or set of columns, thereby preventing the insertion of duplicate records that could lead to data inconsistencies, such as multiple entries representing the same real-world entity.[12] This mechanism upholds the uniqueness of data attributes, allowing databases to maintain accurate representations of entities without the risks associated with redundant or conflicting information.[13] By providing reliable unique identifiers, unique keys indirectly support referential integrity in database relationships, as foreign keys in other tables can reference these constraints to establish valid links between data sets.[3] Additionally, they contribute to reducing data redundancy and mitigating update or delete anomalies, where changes to duplicated values might otherwise require modifications across multiple locations, potentially leading to errors or inconsistencies.[12] Unique keys are particularly suited to business rules that demand uniqueness in non-mandatory fields, such as secondary phone numbers or email addresses, where null values are permissible but duplicates among non-null entries must be avoided.[12] This flexibility makes them ideal for scenarios like customer contact management, where optional yet unique attributes enhance data reliability without enforcing completeness on every record.[3] In database design, the ability to apply multiple unique constraints to a single table enables the creation of flexible schemas for complex entities, accommodating various business requirements while centralizing integrity rules in the database structure for easier maintenance and enforcement.[12] This approach promotes scalable and robust data models that adapt to evolving needs without compromising overall integrity.[13]Core Concepts in Relational Databases
SQL Specification
The unique key, formally known as a UNIQUE constraint in the SQL standard, is defined in ANSI SQL-92 (ISO/IEC 9075:1992) as a constraint that ensures all non-NULL values in a specified column or set of columns are distinct across all rows in a table.[14] This constraint applies to either a single column or multiple columns forming a composite unique key, enforcing data uniqueness at the table level without restricting the constraint to subsets of rows or specific conditions beyond non-duplication.[14] Subsequent standards, including SQL:2016 (ISO/IEC 9075-1:2016), maintain this core definition while incorporating refinements to SQL's foundational integrity mechanisms, such as options for NULL handling in unique constraints (NULLS DISTINCT or NULLS NOT DISTINCT), ensuring compatibility and evolution in relational database management.[15] To declare a UNIQUE constraint, the SQL standard requires the use of the UNIQUE clause within a CREATE TABLE statement, either inline with a column definition (e.g., column_name datatype UNIQUE) or as a table-level constraint (e.g., CONSTRAINT constraint_name UNIQUE (column_name)).[14] Similarly, the ALTER TABLE statement supports adding a UNIQUE constraint post-table creation via ADD UNIQUE (column_name) or ADD CONSTRAINT constraint_name UNIQUE (column_name).[14] While the standard does not mandate the automatic creation of an index, most compliant implementations generate one to enforce the constraint efficiently, as uniqueness verification relies on rapid duplicate detection across the table.[1] The scope of a UNIQUE constraint is confined to the entire table, with uniqueness enforced at the individual row level during insert, update, or delete operations that affect the constrained columns.[15] NULL values are explicitly ignored in uniqueness checks, meaning multiple rows may contain NULL in the constrained column(s) without violating the constraint, as NULL is not considered equal to another NULL or to any non-NULL value.[14] This treatment aligns with SQL's three-valued logic, where NULL represents an unknown value and does not participate in equality comparisons for constraint enforcement.[1] SQL:1999 (ISO/IEC 9075-2:1999) enhanced UNIQUE constraints by introducing support for deferrable attributes, allowing the constraint to be specified as DEFERRABLE or NOT DEFERRABLE.[15] A DEFERRABLE UNIQUE constraint permits temporary violations during a transaction, with enforcement deferred until the transaction commits (e.g., via INITIALLY DEFERRED or SET CONSTRAINTS), providing greater flexibility for complex data manipulations without immediate failure.[1] This evolution from SQL-92's immediate enforcement model was carried forward and refined in later standards like SQL:2016, emphasizing transactional integrity while accommodating practical database operations.Properties and Constraints
A unique key enforces uniqueness across the values in one or more specified columns within a single table, ensuring that no two rows share the same non-NULL combination of values in those columns.[4] This property aligns with the SQL standard, which permits multiple NULL values per constraint, as NULL is not considered equal to any value, including another NULL.[4] Unique keys can be composite, involving multiple columns, where uniqueness is evaluated based on the combined values of all included columns.[16] In SQL's three-valued logic, NULL represents an unknown value, and comparisons involving NULL yield UNKNOWN rather than TRUE or FALSE; thus, NULL ≠ NULL, allowing multiple rows to contain NULL in the constrained columns without violating the uniqueness rule.[4] Per the SQL standard, this handling allows multiple NULL values, though some implementations like SQL Server allow only one. It is commonly adopted in systems like PostgreSQL and Oracle to support partial data scenarios.[4][16] Database management systems enforce unique keys by rejecting INSERT or UPDATE operations that would introduce duplicate non-NULL values in the constrained columns, typically raising a constraint violation error.[4][16] This enforcement is immediate and relies on an underlying index structure, such as a B-tree index, to efficiently check for duplicates during data modifications.[4] Unique keys have inherent limitations: they apply only within a single table and cannot enforce uniqueness across multiple tables, requiring separate mechanisms like foreign keys for inter-table relationships.[4] Additionally, unlike primary keys in some database systems, unique keys are not inherently clustered, meaning they do not automatically organize the table's physical storage order based on the key values.[17]Comparisons with Other Keys
Versus Primary Key
A primary key is a specialized form of unique key that enforces both uniqueness and non-nullability on one or more columns, serving as the primary identifier for rows in a table, while a unique key enforces only uniqueness and permits null values (with some DBMS variations allowing multiple nulls).[4] Unlike unique keys, which can be defined on multiple columns or sets within the same table, a primary key is restricted to exactly one per table to maintain a single, authoritative reference point for entity identification.[18] Selection of a primary key typically prioritizes a stable, efficient identifier such as an auto-incrementing integer column, which facilitates joins and queries across tables, whereas unique keys are applied to secondary attributes that must remain distinct but do not serve as the core row locator, such as an ISBN in a books table.[4] This distinction ensures that primary keys support referential integrity as targets for foreign keys, a role that unique keys can also fulfill.[4] In terms of implementation implications, primary keys often default to a clustered index in many relational database management systems (DBMS), physically ordering the table data for optimized range queries and storage efficiency, while unique keys are usually implemented as non-clustered indexes, which maintain separate data structures for lookups without altering the table's physical order. This indexing behavior enhances primary keys' performance in join operations but may impose additional overhead for unique keys on large datasets.[18]| Aspect | Primary Key | Unique Key |
|---|---|---|
| NULL Allowance | Not allowed; all values must be non-null.[4] | Allowed (multiple NULLs in most DBMS, per SQL standard).[4] |
| Multiplicity per Table | Only one allowed.[18] | Multiple allowed.[4] |
| Default Indexing | Often clustered by default (e.g., in SQL Server, MySQL InnoDB). | Typically non-clustered.[18] |
| Integrity Role | Enforces entity integrity as the main row identifier; target for foreign keys.[4] | Enforces attribute-level uniqueness; can be a target for foreign keys.[4] |
Versus Foreign Key
A unique key constraint ensures that the values in one or more columns within a single table are distinct, thereby preventing duplicate entries and maintaining data uniqueness at the table level.[12] In contrast, a foreign key constraint establishes and enforces referential integrity across multiple tables by requiring that values in a column (or set of columns) match an existing primary key or unique key value in a referenced table.[19] This distinction is fundamental: unique keys operate internally to a table to avoid redundancy in source data, such as ensuring no two employees share the same email address, while foreign keys manage inter-table relationships, for instance, by validating that an order's product identifier corresponds to an actual entry in the products table.[4] Although foreign keys can reference either a primary key or a unique key in the parent table—allowing flexibility in scenarios where a non-primary column needs to serve as a reference point—unique keys themselves do not inherently point to or validate against other tables.[12] For example, in a database schema, a foreign key in an orders table might link to a unique key on the product_code column in the products table, ensuring that only valid, unique product codes are used without duplicating product data.[20] This overlap enables unique keys to support relationships but underscores their primary role in intra-table enforcement rather than cross-table validation.[4] A common pitfall arises from conflating the two: applying a unique key where a foreign key is needed fails to check referential integrity, potentially resulting in orphaned records that reference non-existent data in related tables.[12] Conversely, using a foreign key without ensuring the referenced column has a unique or primary key constraint can lead to inconsistencies, as foreign keys require the target to enforce uniqueness to prevent multiple matches.[19] Proper application of both constraints together strengthens overall data integrity by combining internal uniqueness with reliable relational links.[4]Practical Implementation
Syntax for Creation
In standard SQL, unique constraints can be defined during table creation using theCREATE TABLE statement, either inline with a column definition or as a table-level constraint for single or multiple columns.<citation_url>https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf For a single column, the syntax is:
CREATE TABLE example (
id INTEGER,
email VARCHAR(255) UNIQUE
);
CREATE TABLE example (
id INTEGER,
email VARCHAR(255) UNIQUE
);
CREATE TABLE example (
id INTEGER,
first_name VARCHAR(100),
last_name VARCHAR(100),
UNIQUE (first_name, last_name)
);
```[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
To add a unique constraint to an existing table, the `ALTER TABLE` statement employs the `ADD CONSTRAINT` clause, optionally naming the constraint for easier management:
```sql
ALTER TABLE example ADD CONSTRAINT uk_email UNIQUE (email);
```[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
Unique constraints can be modified or removed using `ALTER TABLE`. To drop a named unique constraint, the syntax is:
```sql
ALTER TABLE example DROP CONSTRAINT uk_email;
```[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
In advanced SQL implementations supporting the standard's deferrable constraint features, unique constraints may be declared as deferrable, allowing violation checks to be postponed until the end of a transaction, with options such as `DEFERRABLE INITIALLY DEFERRED` or `DEFERRABLE INITIALLY IMMEDIATE` to control timing:
```sql
ALTER TABLE example ADD CONSTRAINT uk_email UNIQUE (email) DEFERRABLE INITIALLY DEFERRED;
```[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
Violations of unique constraints during insert or update operations trigger standard SQL error handling via [SQLSTATE](/page/SQLSTATE) codes; for example, SQLSTATE '23505' indicates a unique violation in systems like [PostgreSQL](/page/PostgreSQL) that adhere to this convention.[](https://www.postgresql.org/docs/current/errcodes-appendix.html)
While the core syntax for creating and managing unique constraints remains consistent across database management systems compliant with the ISO/IEC 9075 SQL standard, variations exist in naming conventions for constraints and any vendor-specific extensions.
Unique constraints allow multiple NULL values, as NULL does not compare equal to itself or any other value under standard SQL semantics.[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
### Examples Across DBMS
In [MySQL](/page/MySQL), a unique key can be defined directly within a `CREATE TABLE` statement to enforce [uniqueness](/page/Uniqueness) on a column, such as an [email](/page/Email) field in a users table. For instance, the following SQL creates a table with an [integer](/page/Integer) [primary key](/page/Primary_key) and a unique constraint on the email column:
```sql
CREATE TABLE users (
id INT [PRIMARY KEY](/page/Primary_key),
email VARCHAR(255) UNIQUE
);
CREATE TABLE example (
id INTEGER,
first_name VARCHAR(100),
last_name VARCHAR(100),
UNIQUE (first_name, last_name)
);
```[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
To add a unique constraint to an existing table, the `ALTER TABLE` statement employs the `ADD CONSTRAINT` clause, optionally naming the constraint for easier management:
```sql
ALTER TABLE example ADD CONSTRAINT uk_email UNIQUE (email);
```[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
Unique constraints can be modified or removed using `ALTER TABLE`. To drop a named unique constraint, the syntax is:
```sql
ALTER TABLE example DROP CONSTRAINT uk_email;
```[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
In advanced SQL implementations supporting the standard's deferrable constraint features, unique constraints may be declared as deferrable, allowing violation checks to be postponed until the end of a transaction, with options such as `DEFERRABLE INITIALLY DEFERRED` or `DEFERRABLE INITIALLY IMMEDIATE` to control timing:
```sql
ALTER TABLE example ADD CONSTRAINT uk_email UNIQUE (email) DEFERRABLE INITIALLY DEFERRED;
```[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
Violations of unique constraints during insert or update operations trigger standard SQL error handling via [SQLSTATE](/page/SQLSTATE) codes; for example, SQLSTATE '23505' indicates a unique violation in systems like [PostgreSQL](/page/PostgreSQL) that adhere to this convention.[](https://www.postgresql.org/docs/current/errcodes-appendix.html)
While the core syntax for creating and managing unique constraints remains consistent across database management systems compliant with the ISO/IEC 9075 SQL standard, variations exist in naming conventions for constraints and any vendor-specific extensions.
Unique constraints allow multiple NULL values, as NULL does not compare equal to itself or any other value under standard SQL semantics.[](https://courses.cms.caltech.edu/cs123/sql99std/ansi-iso-9075-2-1999.pdf)
### Examples Across DBMS
In [MySQL](/page/MySQL), a unique key can be defined directly within a `CREATE TABLE` statement to enforce [uniqueness](/page/Uniqueness) on a column, such as an [email](/page/Email) field in a users table. For instance, the following SQL creates a table with an [integer](/page/Integer) [primary key](/page/Primary_key) and a unique constraint on the email column:
```sql
CREATE TABLE users (
id INT [PRIMARY KEY](/page/Primary_key),
email VARCHAR(255) UNIQUE
);
ALTER TABLE statement with a named constraint for better management and error reporting. An example adds a unique constraint on the ISBN column of a products table:
ALTER TABLE products ADD CONSTRAINT unique_isbn UNIQUE (isbn);
ALTER TABLE products ADD CONSTRAINT unique_isbn UNIQUE (isbn);
CREATE UNIQUE INDEX active_email_idx ON users (email) WHERE active = true;
CREATE UNIQUE INDEX active_email_idx ON users (email) WHERE active = true;
CREATE UNIQUE INDEX IX_products_code ON products (code);
CREATE UNIQUE INDEX IX_products_code ON products (code);
ALTER TABLE statement with an out-of-line constraint definition. For example, to ensure uniqueness across warehouse ID and name in a warehouses table:
ALTER TABLE warehouses ADD CONSTRAINT wh_unq UNIQUE (warehouse_id, warehouse_name);
ALTER TABLE warehouses ADD CONSTRAINT wh_unq UNIQUE (warehouse_id, warehouse_name);
SELECT ... AS OF with a timestamp or SCN, enabling diagnosis of erroneous transactions that caused duplicates without full recovery.[27]
