PostgreSQL

Primary Key

A key is an attribute or set of attributes that are used to uniquely identify a tuple in a relation. Keys can also be used to establish relations between other relations and columns. The values of a key are called key values.

A PRIMARY KEY is the attribute that identifies a each row in a table. If multiple attributes form a primary key this is known as a composite key. There can only be one primary key per table. Each primary key value must be unique and cannot be NULL.

Syntax

CREATE TABLE <table-name> (
    <column-name> <column-type> PRIMARY KEY,
    <column-name> <column-type>,
    ...
);

Example

CREATE TABLE Companies (
    name TEXT PRIMARY KEY,
    type TEXT,
    founded INT,
    hq TEXT,
);

Composite Keys

A composite key is the set of attributes that identifies a each row in a table.

CREATE TABLE Companies (
    name TEXT,
    type TEXT,
    founded INT,
    hq TEXT,
    PRIMARY KEY(Name, Founded)
);