Introduction
UNIQUE
5. UNIQUE vs. PRIMARY KEY
NOT NULL
CHECK
DEFAULT
Summary

Instruction

Boom! The insertion failed because we already have a row with the name 'Monopoly'. Our new constraint takes care of that.

UNIQUE is indeed very similar to PRIMARY KEY which we learned in the previous part. We sometimes say that UNIQUE creates an alternate key.

Exercise

It's time for you to create your own table board_game with the UNIQUE column. Let us remind you about the columns we have:

  • id which is an IDENTITY and the primary key,
  • name is a text of up to 32 characters (must be unique!),
  • genre which is also a text of up to 32 characters,
  • min_players and min_age which are integers,
  • price which is a decimal value of up to 5 digits, 2 of which are after the decimal point.
Remember that the UNIQUE constraint is placed after the column type!

Stuck? Here's a hint!

Type

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

Console

Code editor

Result

TableConsole