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 KEY
s 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.