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

Instruction

Very well done! How about multi-column primary keys? How can we define them? The syntax is a bit different:

CREATE TABLE tennis_racket (
  manufacturer varchar(32),
  model varchar(32),
  rating integer,
  PRIMARY KEY (manufacturer, model)
);

Now instead of adding PRIMARY KEY after a column type, we added the phrase PRIMARY KEY (manufacturer, model) after the last column definition. Inside the parentheses, we provide the names of the previously-defined columns that constitute the primary key.

An example from the previous exercise would look like this:

CREATE TABLE employee (
  id integer,
  first_name varchar(32),
  last_name varchar(32),
  department varchar(32),
  PRIMARY KEY (id, department)
);

Exercise

The company now needs a table to keep track of its departments. Create a table named department with the following columns:

  1. country – Up to 32 characters.
  2. name – Up to 32 characters.
  3. address – Up to 64 characters.

The columns country and name should form the primary key.

Stuck? Here's a hint!

Use:

PRIMARY KEY (country, name)