Primary Key constraint

The PRIMARY KEY constraint specifies that the constrained columns' values must uniquely identify each row.

Unlike other constraints which have very specific uses, the PRIMARY KEY constraint should be used for every table because it provides an intrinsic structure to the table's data. This both makes it easier to understand, as well as improving query performance.

Note:

A table's primary key can only be specified in the CREATE TABLE statement. It cannot be changed later using ALTER TABLE, though it is possible to go through a process to create a new table with the new primary key you want and then migrate the data.

Details

  • Tables can only have one primary key.
  • To ensure each row has a unique identifier, the PRIMARY KEY constraint combines the properties of both the UNIQUE and NOT NULL constraints. The properties of both constraints are necessary to make sure each row's primary key columns contain distinct sets of values.

    • The properties of the UNIQUE constraint ensure that each value is distinct from all other values.
    • However, because NULL values never equal other NULL values, the UNIQUE constraint is not enough (two rows can appear the same if one of the values is NULL). To prevent the appearance of duplicated values, the PRIMARY KEY constraint also enforces the properties of the Not Null constraint.
  • The columns in the PRIMARY KEY constraint are used to create its primary index, which CockroachDB uses by default to access the table's data.

This index does not take up additional disk space (unlike secondary indexes, which do) because CockroachDB uses the primary index to structure the table's data in the key-value layer. For more information, see our blog post SQL in CockroachDB: Mapping Table Data to Key-Value Storage.

  • For optimal performance, we recommend defining a primary key for every table.

If you create a table without defining a primary key, CockroachDB uses a unique identifier for each row, which it then uses for the primary index. Because you cannot meaningfully use this unique row identifier column to filter table data, it does not offer any performance optimization. This means you will always have improved performance by defining a primary key for a table. For more information, see our blog post Index Selection in CockroachDB.

Syntax

PRIMARY KEY constraints can be defined at the table level. However, if you only want the constraint to apply to a single column, it can be applied at the column level.

Column level

CREATETABLEtable_name(column_namecolumn_typePRIMARY KEYcolumn_constraints,column_deftable_constraints))
ParameterDescription
table_nameThe name of the table you're creating.
column_nameThe name of the Primary Key column.
column_typeThe Primary Key column's data type.
column_constraintsAny other column-level constraints you want to apply to this column.
column_defDefinitions for any other columns in the table.
table_constraintsAny table-level constraints you want to apply.

Example

  1. > CREATE TABLE orders (
  2. order_id INT PRIMARY KEY,
  3. order_date TIMESTAMP NOT NULL,
  4. order_mode STRING(8),
  5. customer_id INT,
  6. order_status INT
  7. );

Table level

CREATETABLEtable_name(column_def,CONSTRAINTnamePRIMARY KEY(column_name,)table_constraints)
ParameterDescription
table_nameThe name of the table you're creating.
column_defDefinitions for any other columns in the table.
nameThe name you want to use for the constraint, which must be unique to its table and follow these identifier rules.
column_nameThe name of the column you want to use as the PRIMARY KEY.The order in which you list columns here affects the structure of the primary index.
table_constraintsAny other table-level constraints you want to apply.

Example

  1. > CREATE TABLE IF NOT EXISTS inventories (
  2. product_id INT,
  3. warehouse_id INT,
  4. quantity_on_hand INT NOT NULL,
  5. PRIMARY KEY (product_id, warehouse_id)
  6. );

Usage example

  1. > CREATE TABLE IF NOT EXISTS inventories (
  2. product_id INT,
  3. warehouse_id INT,
  4. quantity_on_hand INT NOT NULL,
  5. PRIMARY KEY (product_id, warehouse_id)
  6. );
  1. > INSERT INTO inventories VALUES (1, 1, 100);
  1. > INSERT INTO inventories VALUES (1, 1, 200);
  1. pq: duplicate key value (product_id,warehouse_id)=(1,1) violates unique constraint "primary"
  1. > INSERT INTO inventories VALUES (1, NULL, 100);
  1. pq: null value in column "warehouse_id" violates not-null constraint

See also

Was this page helpful?
YesNo