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

Boom! The insertion failed because we already have a row with the name value '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 for the table.

Exercise

It's time for you to create your own board_game table with a UNIQUE column. Remember the columns we have:

  • id is always auto-generated and is the primary key.
  • name is a text of up to 32 characters (and it must be unique!).
  • genre is also a text of up to 32 characters.
  • min_players and min_age are integers.
  • price 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!

The price column should be of type decimal(5, 2), id should be GENERATED ALWAYS AS IDENTITY PRIMARY KEY and name should be UNIQUE.