Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Primary keys – the basics
Single-column primary keys
8. Syntax revisited
Multi-column primary keys
NOT NULL
Review

Instruction

Great, let's study the theory for a while.

We usually choose a single column to become our primary key. If we want to inform our database that a given column is supposed to be the primary key (often abbreviated in ERDs as PK), we place the keywords PRIMARY KEY after the name and type of the column:

CREATE TABLE movie (
   id integer PRIMARY KEY,
   title varchar(40)
);

Quite simple, right?

Exercise

You are now going to create a table with a primary key.

Let's reconstruct our movie table so that it has the following columns:

  1. film_id – an integer number and the primary key.
  2. title – a field of up to 64 characters.
  3. year – an integer.
  4. genre – a field of up to 10 characters.

Stuck? Here's a hint!

Type:

CREATE TABLE movie (
   film_id integer PRIMARY KEY,
   title varchar(64),
   year integer,
   genre varchar(10)
);