Primary Key Constraints in Relational Databases¶
Source: Primary and Foreign Key Constraints - SQL Server | Microsoft Docs
Overview¶
A table typically has a column or combination of columns that contain values that uniquely identify each row in the table. This column, or columns, is called the primary key (PK
) of the table and enforces the entity integrity of the table. Because primary key constraints guarantee unique data, they are frequently defined on an identity column.
When you specify a primary key constraint for a table, the Database Engine enforces data uniqueness by automatically creating a unique index for the primary key columns. This index also permits fast access to data when the primary key is used in queries. If a primary key constraint is defined on more than one column, values may be duplicated within one column, but each combination of values from all the columns in the primary key constraint definition must be unique.
As shown in the following illustration, the ProductID
and VendorID
columns in the Purchasing.ProductVendor
table form a composite primary key constraint for this table. This makes sure that every row in the ProductVendor
table has a unique combination of ProductID
and VendorID
. This prevents the insertion of duplicate rows.
-
A table can contain only one primary key constraint.
-
A primary key cannot exceed 16 columns and a total key length of 900 bytes.
-
The index generated by a primary key constraint cannot cause the number of indexes on the table to exceed 999 nonclustered indexes and 1 clustered index.
-
If clustered or nonclustered is not specified for a primary key constraint, clustered is used if there no clustered index on the table.
-
All columns defined within a primary key constraint must be defined as not null. If nullability is not specified, all columns participating in a primary key constraint have their nullability set to not null.
-
If a primary key is defined on a CLR user-defined type column, the implementation of the type must support binary ordering.
Appendix: Links¶
- Databases
- SQL
- T-SQL
- SQL Server
- Foreign Key Constraints in Relational Databases
- CHECK Constraints in Relational Databases
- Database Design Best Practices
Backlinks:
list from [[Primary Key Constraints in Relational Databases]] AND -"Changelog"