I have 3 tables :
------------------------
| Competitions |
------------------------
| id (PK) | INT (auto++) |
| name | VARCHAR(100) |
------------------------
------------------------------------
| Matches |
------------------------------------
| id (PK) | INT (auto++) |
| blue1 | INT (FK -> teams.id) ** |
| blue2 | INT (FK -> teams.id) ** |
| blue3 | INT (FK -> teams.id) ** |
| red1 | INT (FK -> teams.id) ** |
| red2 | INT (FK -> teams.id) ** |
| red3 | INT (FK -> teams.id) ** |
| compID | INT (FK -> comps.id) |
------------------------------------
---------------------------------
| Teams |
---------------------------------
| id (PK) | INT |
| name | VARCHAR(100) |
| compID | INT (FK -> comps.id) |
---------------------------------
For the columns in Matches marked with a **, I want to foreign key to Teams.id, but I need to make sure that the compID in Matches and Teams are the same.
For example if Team 9987 is going to compID 1, it should not be red1,2,3 or blue1,2,3 in a match whose compID is 4.
How can I make this constraint?