I have a table chess_games
which has the following rows that are relevant for this question:
id | tournamentID | whiteUserID | blackUserID | outcome
I would like to have the following check:
A new tuple may only be inserted, when outcome == -1, tournamentID == NULL and (whiteUserID, blackUserID) is unique.
Note that (whiteUserID, blackUserID) is not a key as it has not to be unique if torunamentID is set or outcome != -1.
Is this possible with a MySQL database? Please also note if the solution is MySQL specific or if it is standard SQL.
Although I would like a solution that works with MySQL, I am also interested in a SQL solution.