Below is the match
table, which keeps record of all the matches between players from players table.
You can see that the winner_player
should be either player_1
or player_2
, I mean it should give error if I do insert any other player_id except player_1
or player_2
. But now I can insert any player_id (number) in this column.
match_id | player_1 | player_2 | winner_player |
---|---|---|---|
1 | 1 | 2 | 2 |
2 | 3 | 4 | 3 |
3 | 5 | 6 | 5 |
4 | 15 | 16 | 16 |
My question: how can I restrict winner_player
column to be same as either player_1
or player_2
CodePudding user response:
For other peoples help I am posting the @Stu's answer here:
CHECK (winner_player in (player_1, player_2))
learn more about check
constraint here https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS