Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Primary keys
2. Creating tables with single-column primary keys
Foreign keys
Updating and deleting with foreign keys
Summary

Instruction

You may not be aware of it, but you already know a few constraint types. Primary keys, foreign keys, and NOT NULL are all constraints.

Before we get to know new constraint types, let's do a recap of what we already know about primary and foreign keys.

A primary key uniquely identifies a given row in a table. It may consist of one or more columns. When creating a table, we can specify a single-column primary key in the following way:

CREATE TABLE swimming_pool (
  id integer PRIMARY KEY,
  address text,
  rating integer
);

Look a the definition of the first column. After the data type, we added the keywords PRIMARY KEY. This way, the id column becomes the single-column primary key for the swimming_pool table.

Exercise

An international accounting company needs to create a simple database to keep track of their employees. For now, create a table named employee with the following fields:

  1. id – An integer value.
  2. first_name – Up to 32 characters.
  3. last_name – Up to 32 characters.
  4. department – Up to 32 characters.

The id column should be the primary key.