3个表上的错误代码1005错误号121我做错了什么?

时间:2010-09-21 01:47:55

标签: sql mysql mysql-error-1005

代码:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;

- 表mydbSEX


CREATE  TABLE IF NOT EXISTS `mydb`.`SEX` (
  `idSex` INT NOT NULL AUTO_INCREMENT ,
  `descrip` VARCHAR(15) NOT NULL ,
  PRIMARY KEY (`idSex`) )
ENGINE = InnoDB;

- 表mydbTEAMS


CREATE  TABLE IF NOT EXISTS `mydb`.`TEAMS` (
  `idTeam` INT NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(50) NOT NULL ,
  `idSex` INT NOT NULL ,
 PRIMARY KEY (`idTeam`) ,
  INDEX `ID_SEX` (`idSex` ASC) ,
  CONSTRAINT `ID_SEX`
    FOREIGN KEY (`idSex` )
    REFERENCES `mydb`.`SEX` (`idSex` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

- 表mydbCHAMPIONSHIPS


CREATE  TABLE IF NOT EXISTS `mydb`.`CHAMPIONSHIPS` (
  `idChampionship` INT NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(100) NOT NULL ,
  `totalPlayingDays` INT NULL ,
  `startDate` DATE NULL ,
  `EndDate` DATE NULL ,
  `active` CHAR NULL ,
  `idSex` INT NOT NULL ,
  PRIMARY KEY (`idChampionship`) ,
  INDEX `ID_SEX` (`idSex` ASC) ,
  CONSTRAINT `ID_SEX`
    FOREIGN KEY (`idSex` )
    REFERENCES `mydb`.`SEX` (`idSex` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

- 表mydbTEAMS_PER_CHAMPIONSHIP


CREATE  TABLE IF NOT EXISTS `mydb`.`TEAMS_PER_CHAMPIONSHIP` (
  `idTeam` INT NOT NULL ,
  `idChampionship` INT NOT NULL ,
  PRIMARY KEY (`idTeam`, `idChampionship`) ,
  INDEX `ID_TEAM` (`idTeam` ASC) ,
  INDEX `ID_CHAMP` (`idChampionship` ASC) ,
  CONSTRAINT `ID_TEAM`
    FOREIGN KEY (`idTeam` )
    REFERENCES `mydb`.`TEAMS` (`idTeam` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `ID_CHAMP`
    FOREIGN KEY (`idChampionship` )
    REFERENCES `mydb`.`CHAMPIONSHIPS` (`idChampionship` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

- 表mydbMATCHES


CREATE  TABLE IF NOT EXISTS `mydb`.`MATCHES` (
  `idMatch` INT NOT NULL ,
  `idChampionship` INT NOT NULL ,
  `idTeam` INT NOT NULL ,
  `date` TIMESTAMP NULL DEFAULT NULL ,
  `gols` INT NULL DEFAULT -1 ,
  `isLocal` CHAR NULL DEFAULT 'N' ,
  PRIMARY KEY (`idMatch`, `idChampionship`, `idTeam`) ,
  INDEX `ID_TEAM_X_CHAMP` (`idChampionship` ASC, `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;

- 表mydbPLAYERS


CREATE  TABLE IF NOT EXISTS `mydb`.`PLAYERS` (
  `idPlayer` INT NOT NULL ,
  `name` VARCHAR(45) NULL ,
  `lastname` VARCHAR(45) NULL ,
  `performance` VARCHAR(45) NULL ,
  `idTeam` INT NOT NULL ,
  `idSex` INT NOT NULL ,
  PRIMARY KEY (`idPLayer`) ,
  INDEX `ID_TEAM` (`idTeam` ASC) ,
  INDEX `ID_SEX` (`idSex` ASC) ,
  CONSTRAINT `ID_TEAM`
    FOREIGN KEY (`idTeam` )
    REFERENCES `mydb`.`TEAMS` (`idTeam` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `ID_SEX`
    FOREIGN KEY (`idSex` )
    REFERENCES `mydb`.`SEX` (`idSex` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

- 表mydbMASTERS


CREATE  TABLE IF NOT EXISTS `mydb`.`MASTERS` (
  `idPlayer` INT NOT NULL ,
  `idMatch` INT NOT NULL ,
  `idChampionship` INT NOT NULL ,
  `idteam` INT NOT NULL ,
  `gols` INT NOT NULL DEFAULT -1 ,
  PRIMARY KEY (`idPlayer`, `idMatch`, `idChampionship`, `idTeam`) ,
  INDEX `ID_MATCH` (`idMatch` ASC, `idChampionship` ASC, `idTeam` ASC) ,
  INDEX `ID_PLAY` (`idPLayer` ASC) ,
  CONSTRAINT `ID_MATCH`
    FOREIGN KEY (`idMatch` , `idChampionship` , `idTeam` )
    REFERENCES `mydb`.`MATCHES` (`idMatch` , `idChampionship` , `idTeam` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `ID_PLAY`
    FOREIGN KEY (`idPlayer` )
    REFERENCES `mydb`.`PLAYERS` (`idPlayer` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

- 表mydbSANCTIONS


CREATE  TABLE IF NOT EXISTS `mydb`.`SANCTIONS` (
  `idSanction` INT NOT NULL ,
  `descrip` VARCHAR(100) NOT NULL DEFAULT 'A DEFINIR' ,
  PRIMARY KEY (`idSanction`) )
ENGINE = InnoDB;

- 表mydbSANCTIONS_PLAYERS


CREATE  TABLE IF NOT EXISTS `mydb`.`SANCIONS_PLAYERS` (
  `idPlayer` INT NOT NULL ,
  `idMatch` INT NOT NULL ,
  `idChampionship` INT NOT NULL ,
  `idTeam` INT NOT NULL ,
  `idSanction` INT NOT NULL ,
  PRIMARY KEY (`idPlayer`, `idMatch`, `idChampionship`, `idTeam`) ,
  INDEX `ID_PLAY` (`idPlayer` ASC) ,
  INDEX `ID_MATCH` (`idMatch` ASC, `idChampionship` ASC, `idTeam` ASC) ,
  INDEX `ID_SANC` (`idSanction` ASC) ,
  CONSTRAINT `ID_PLAY`
    FOREIGN KEY (`idPlayer` )
    REFERENCES `mydb`.`PLAYERS` (`idPlayer` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `ID_MATCH`
    FOREIGN KEY (`idMatch` , `idChampionship` , `idTeam` )
    REFERENCES `mydb`.`MATCHES` (`idMatch` , `idChampionship` , `idTeam` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `ID_SANC`
    FOREIGN KEY (`idSanction` )
    REFERENCES `mydb`.`SANCTIONS` (`idSanction` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

1 个答案:

答案 0 :(得分:0)

我注意到了这一点:


CREATE  TABLE IF NOT EXISTS `mydb`.`MATCHES` (
  `idMatch` INT NOT NULL ,
  `idChampionship` INT NOT NULL ,
  `idTeam` INT NOT NULL ,
  `date` TIMESTAMP NULL DEFAULT NULL ,
  `gols` INT NULL DEFAULT -1 ,
  `isLocal` CHAR NULL DEFAULT 'N' ,
  PRIMARY KEY (`idMatch`, `idChampionship`, `idTeam`) ,
  INDEX `ID_TEAM_X_CHAMP` (`idChampionship` ASC, `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;

    FOREIGN KEY (`idChampionship` , `idMatch` )
    REFERENCES `mydb`.`TEAMS_PER_CHAMPIONSHIP` (`idChampionship` , `idTeam` )

我认为你的密钥和参考文献不匹配。这可能无法解决您的整个问题,但仍有待修复:)。如果您的错误随着更新而改变,请告诉我。

编辑:我发现了别的东西。

列于MySQL Reference

InnoDB需要外键和引用键的索引,以便外键检查速度快,不需要表扫描。在引用表中,必须有一个索引,其中外键列以相同的顺序列为第一列。如果引用表不存在,则会自动在引用表上创建此索引。 (这与某些旧版本形成对比,其中必须显式创建索引或创建外键约束失败。)如果给定,则使用index_name,如前所述。

CREATE  TABLE IF NOT EXISTS `mydb`.`TEAMS_PER_CHAMPIONSHIP` (
  `idTeam` INT NOT NULL ,
  `idChampionship` INT NOT NULL ,
  PRIMARY KEY (`idTeam`, `idChampionship`) ,
  INDEX `ID_TEAM` (`idTeam` ASC) ,
  INDEX `ID_CHAMP` (`idChampionship` ASC) ,
  CONSTRAINT `ID_TEAM`
    FOREIGN KEY (`idTeam` )
    REFERENCES `mydb`.`TEAMS` (`idTeam` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `ID_CHAMP`
    FOREIGN KEY (`idChampionship` )
    REFERENCES `mydb`.`CHAMPIONSHIPS` (`idChampionship` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE  TABLE IF NOT EXISTS `mydb`.`MATCHES` (
  `idMatch` INT NOT NULL ,
  `idChampionship` INT NOT NULL ,
  `idTeam` INT NOT NULL ,
  `date` TIMESTAMP NULL DEFAULT NULL ,
  `gols` INT NULL DEFAULT -1 ,
  `isLocal` CHAR NULL DEFAULT 'N' ,
  PRIMARY KEY (`idMatch`, `idChampionship`, `idTeam`) ,
  INDEX `ID_TEAM_X_CHAMP` (`idChampionship` ASC, `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;

根据该引用文本,引用的键需要与引用的tabled上的顺序相同。您的订单顺序与主键不同。