views:

43

answers:

1
CREATE  TABLE IF NOT EXISTS `mydb`.`MATCHES` ( 
  `idMatch` INT NOT NULL , 
  `idChampionship` INT NOT NULL , 
  `idWinningTeam` INT NOT NULL ,
  `idWLoosingTeam` INT NOT NULL , 
  `date` TIMESTAMP NULL DEFAULT NULL , 
  `goalsWinningTeam` INT NULL DEFAULT -1 ,
  `goalsLoosingTeam` INT NULL DEFAULT -1 , 
  `played` CHAR NULL DEFAULT 'Y' , 
  PRIMARY KEY (`idMatch`) , 
  INDEX `ID_TEAM_X_CHAMP` (`idMatch` ASC) , 
  CONSTRAINT `ID_TEAM_X_CHAMP` 
  FOREIGN KEY (`idChampionship` , `idMatch` ) 
  REFERENCES `mydb`.`TEAMS_PER_CHAMPIONSHIP` (`idChampionship` , `idTeam` ) 
  ON DELETE NO ACTION 
  ON UPDATE NO ACTION) 
  ENGINE = InnoDB;

I'm trying to make a matches table and I'm not sure how to set winning and losing team,
both by idTeam (can I use same foreign key for both?) I have a Team table a Championship
table and teams_per_champsionship table (for indexing).

schema available


Thanks you very much

A: 

An example schema:

Team Table
----------
TeamID pk
rest of the team information

Championship Table
------------------
ChampID  pk
rest of the championship info

Match Table
-----------
MatchID pk
winningTeamID fk
losingTeamID fk
rest of the match info

ChampMatchTable
---------------
ChampId fk (dups)
MachID fk

OR

You could put the ChampID in the match table and not have the ChampMatchTable.

original answer below

Yes you can not use the same key for both. They both are keys to the same table but they are different (since each team is different.)

Not clear what the idChamionship key points to. Is this a table that describes an event which has matches?

Hogan
I should romve idChampionship and idmatch should be the primarykey and index, and 2 foreign keys one for each team, how would that look like?
Ignacio
I think (not sure) that you just need what you have. idwinningteam points to the winning team and idloosingteam points to the loosing team. idmatch should stay... it is the key for this table.
Hogan
could you give a hand with sql for this? thank you vry much
Ignacio
I think the issue is one of data design not the sql part. Let me write what I think you are doing...
Hogan
@ignacio see updated answer.
Hogan
Hogan I understand where you are going but I can not get there, would you like to see the whole schema sript, I know is just this table the one with problems The big picture would be a db to manage a football o soccer or any amateur leagues
Ignacio
Ignacio -- yes if you have more info that would make your question clearer by all means post it to the question
Hogan