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
Multicolumn primary keys
Auto-increment columns and sequences
Revision

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 as PK), we place the keywords PRIMARY KEY after the name and type of the column:

CREATE TABLE movie (
   id INT PRIMARY KEY,
   title VARCHAR(40)
);

Quite simple, right?

Exercise

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

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

  • film_id which is an integer number and the primary key,
  • title which is a field of up to 64 characters,
  • year which is an integer and
  • genre which is a text of up to 10 characters.

 

Stuck? Here's a hint!

Type

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