Alternate Keys in Relational Databases

An alternate key in relational database design is a candidate key that has not been selected as a table’s primary key. Like the chosen primary key, an alternate key must be unique and minimal: its attributes can identify every row, and none can be removed without losing that property.

The distinction is conceptual. SQL database systems usually implement alternate business identifiers with appropriate NOT NULL and UNIQUE constraints, but a nullable unique field is not automatically a reliable identifier for every row. This article distinguishes candidate keys, primary keys, alternate keys, foreign keys and physical indexes, including database-engine differences that matter when writing SQL.

Candidate keys come before the primary-key decision

Suppose a product table contains an internal product_id and a stable SKU that business rules require to be present and distinct for every product. Both attributes might be candidate keys if either alone can uniquely identify the products in every valid database state. The designer could choose product_id as the primary key and treat the SKU as an alternate key.

Not every field that looks distinct in today’s data is a candidate key. For instance, a person’s name combined with a hiring date may be unique in a small current dataset but is not necessarily guaranteed to remain unique when more employees are hired. An optional, shared or recycled external number may also fail as a lifetime business identifier. Candidate-key analysis must be grounded in actual valid-state business rules, not a one-time query showing zero duplicates.

Uniqueness and minimality explained

A superkey is any set of attributes that identifies a row uniquely; it may contain unnecessary extra attributes. A candidate key is a minimal superkey, meaning no proper subset still has the same identifying guarantee. An alternate key is any such candidate key that was not chosen as the primary key.

For example, in a table representing enrolments, assume there can be only one enrolment for each (student_id, course_id) combination. That pair may be a candidate key. The larger set (student_id, course_id, grade) is a superkey, but not minimal if grade is unnecessary to identify a row. A separate generated enrolment_id could serve as the primary key, leaving the student-course pair as an alternate key.

Changing business rules can change the analysis. If a student can retake the same course, the original pair may no longer uniquely identify an enrolment and a term or attempt number might need to be included.

Primary, alternate, unique and foreign keys compared

Term Meaning What to verify
Superkey Any identifying set of attributes, possibly containing extras Whether a proper subset also identifies rows
Candidate key Minimal identifying set of attributes Uniqueness and minimality in every permitted data state
Primary key One candidate key designated as the table’s main identifier Non-null values and a single primary-key constraint
Alternate key A remaining candidate key not chosen as primary Distinct, complete business identity and enforced constraint
UNIQUE constraint SQL rule enforcing distinct column values or tuples NULL, collation and index behavior by database engine
Foreign key Rule constraining child values to permitted parent references Approved target key and delete/update actions

A composite key uses more than one attribute, regardless of whether it serves as a primary, candidate or alternate key. A surrogate key is a generated identifier often used as the primary key. These classifications describe different properties and should not be treated as competing labels for the same idea.

Implementing an alternate key in PostgreSQL

When an alternate identifier is required and the business rules guarantee its stability and distinctness, an explicit UNIQUE constraint alongside NOT NULL communicates the intent. This example uses a generated primary key and an alternate SKU.

CREATE TABLE products (
  product_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  sku TEXT NOT NULL,
  description TEXT,
  CONSTRAINT uq_products_sku UNIQUE (sku)
);

Under the stated business rule, sku is a candidate key and becomes an alternate key when product_id is chosen as primary. The constraint rejects insertions or updates that would duplicate a non-null SKU. An application should still decide how to handle concurrency and a uniqueness-violation error.

For a multi-tenant system in which codes need only be unique inside each organisation, a composite UNIQUE (organisation_id, product_code) may be appropriate. Mark the identifying columns NOT NULL if missing values should not be accepted. Do not impose global uniqueness when the business requires uniqueness only within a tenant.

NULL behavior differs across SQL engines

For the distinction between enforcing values with a unique key constraint and choosing a business identifier, see the related guide.

SQL UNIQUE constraints are not identical to conceptual candidate keys. In PostgreSQL, an ordinary nullable UNIQUE column can contain multiple NULLs because NULLs are treated as distinct for uniqueness by default. PostgreSQL also supports UNIQUE NULLS NOT DISTINCT when the application intends missing values to conflict.

SQL Server’s ordinary single-column nullable UNIQUE constraint allows at most one NULL; for multiple absent values with uniqueness enforced only for present values, a filtered unique index can be used. MySQL also has different NULL handling from SQL Server. These differences mean that a blanket rule saying an alternate key “allows exactly one NULL” or “allows unlimited NULLs” is incorrect.

A genuinely identifying alternate key should be present for each row under its business rule. Declare the relevant columns NOT NULL where that is required, and test the behavior on the actual engine and version rather than relying on abstract statements about all DBMS products.

Alternate keys in normalization and functional dependencies

Functional dependencies and normal-form analysis consider all candidate keys, not just whichever one was selected as primary. A superkey determinant is valid for Boyce-Codd Normal Form (BCNF) regardless of whether it is the primary key or an alternate key.

Third Normal Form (3NF) has a more permissive formal rule. For every nontrivial dependency X → A, either X must be a superkey or A must be a prime attribute (part of a candidate key). BCNF requires X to be a superkey for every nontrivial functional dependency. A table can therefore be in 3NF but not BCNF when the non-superkey determinant controls a prime attribute. The existence of multiple candidate keys may make this distinction visible.

It would be wrong to say that dependence on an alternate candidate key automatically violates 3NF. An alternate key remains a candidate key in the mathematical analysis, regardless of what the database designer calls the primary key.

Foreign keys and alternate-key references

In common SQL engines, a suitable UNIQUE constraint can be used as a parent key for foreign-key references, subject to engine-specific rules. It is often simpler to reference an internal primary key, while a stable alternate business key can still support external integrations or user-facing lookups.

Declaring a UNIQUE constraint does not automatically create a foreign-key relationship. The referencing table must declare the appropriate FOREIGN KEY and the designer must choose permitted update and delete actions. Cascades are not an inherent property of an alternate key and should be tested for their full downstream effect.

Do unique indexes automatically improve performance?

PostgreSQL and SQL Server normally create supporting indexes for UNIQUE constraints. These can make equality lookup and some join patterns efficient, but maintaining indexes consumes storage and increases work on writes. A unique index is not guaranteed to improve every query; column order, predicates, data distribution and execution plans determine how it is used.

A composite key such as (organisation_id, account_code) may support searches beginning with organisation_id more readily than queries filtering only on account_code, depending on the engine and workload. Inspect actual query plans before adding more indexes, and do not create a unique index merely because existing values happen to be distinct.

A UNIQUE index is an integrity rule with an enforcement effect, not a harmless performance hint. If future legitimate data violates it, inserts or updates will fail. Decide first whether uniqueness is a genuine invariant of the business domain.

Common real-world alternate-key choices

Entity Possible alternate key Important caveat
Product SKU May be unique only within a merchant or catalogue
Customer Stable membership identifier Verify assignment, reissuance and completeness rules
Invoice (issuer_id, invoice_number) Numbers might repeat across issuers
Employee Internal payroll identifier Check uniqueness across contractors, affiliates and historical rehires
User account (tenant_id, username) Check case sensitivity, rename rules and nullability

Emails, phone numbers, passport numbers and human names need special care. They may be optional, change over time, be shared, use different comparison rules or lack a worldwide one-to-one guarantee. Even when a value is unique in the present table, it is not necessarily a sound long-term alternate key.

A checklist for selecting and enforcing alternate keys

  1. Document the business rule that guarantees uniqueness in all valid states.
  2. Confirm minimality, including whether the identifier is composite and any part can be removed.
  3. Specify whether the identifier may be absent or change during the entity’s lifetime.
  4. Choose the primary key and record which candidate keys remain alternate.
  5. Apply engine-appropriate NOT NULL, UNIQUE and any necessary foreign-key constraints.
  6. Test case sensitivity, NULLs, duplicates, concurrent inserts and migration from existing data.
  7. Measure the index’s effects on read plans, write overhead and operational costs.

Where the rules are uncertain, work with subject-matter experts before selecting constraints. A misplaced alternate key may be more damaging than no extra key at all if it rejects legitimate records or encourages workarounds that weaken data quality.

Conclusion

An alternate key is a minimal candidate identifier that was not selected as the primary key. Its real value is preserving another meaningful uniqueness invariant through a deliberate database rule. The SQL implementation is usually a UNIQUE constraint on the relevant required columns, but NULL behavior, comparison semantics, indexes and foreign-key support must be understood for the chosen engine.

Model keys from the data’s meaning, not from a coincidental lack of duplicates in today’s table. That produces more reliable constraints, clearer relationships and safer application behavior as the data evolves.

  • img