Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Defining columns with UNIQUE
Adding and removing UNIQUE constraints
Summary

Instruction

All right! Let's get down to learning the UNIQUE constraint. Applying this constraint means that the values in a given column must be ... well, unique. In other words, each value in a UNIQUE column can only be used once. Here's how you specify the UNIQUE column when creating a table:

CREATE TABLE board_game (
  id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name varchar(32) UNIQUE,
  genre varchar(32),
  min_players integer,
  min_age integer,
  price decimal(5, 2)
);

As you can see, the name column has the UNIQUE constraint. It is placed after the type of the column.

Exercise

Our users added some games to the board_game table. Select all the information from the table and study the name column.

As you will see, all the values are unique: each of them appears only once.