Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
UNIQUE
7. UNIQUE on multiple columns
NOT NULL
CHECK
DEFAULT
Summary

Instruction

Okay, time to move on. Do you remember that we could include more than one column in the PRIMARY KEY? The same rule applies to the UNIQUE constraint. Take a look:

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

At the end of the instruction, we put the key word UNIQUE and within parentheses we provided the pair of columns which must be unique.

From now on, the pair of values: name and genre, must be unique for each row. Particular values, however, may be the same, as long as the whole pair is unique. So (name='ABC', genre='horror') and (name='ABC', genre='fantasy') will be both acceptable, just as in the case of PRIMARY KEYs on multiple columns. In other words, no two games of the same genre can have the same name, but if the genres are different, then identical names are allowed.

Exercise

Run the example from our instruction to create the new table.

Stuck? Here's a hint!

Simply press the button!