SQL: Creating Databases and Tables - Primary Keys and Foreign Keys

Primary Keys

A primary key is a column or a group of columns that is used to identify a row uniquely in a table.

  • A table can have only one primary key.

  • When you add a primary key to a table, PostgreSQL creates a unique index on the column or a group of columns used to define the primary key

Syntax

-- Primary Key
CREATE TABLE table_name (
column_name data_type PRIMARY KEY,
column_name data_type, ...);

Foreign Keys

A foreign key is a field or group of fields in a table that unique identifies a row in another table.

  • In other words, a foreign key is defined in a table that refers to the primary key of the other table.

  • The table that contains the foreign key is called referencing table or child table. And the table to which the foreign key references is called referenced table or parent table

  • A table can have multiple foreign keys depending on its relationships with other tables.

  • Define foreign keys through a foreign key constraint