mySQL数据库的外键问题

时间:2016-04-09 07:13:44

标签: mysql database mysql-workbench

我正在为我班级的数据库工作。它将与网站一起使用,并且必须跟踪用户及其密码,会议及其活动以及演示者。

使用mySQL工作台,我创建了一个ERR图,并将所有表链接在一起,使事件表中的conferenceID与会议桌中的conferenceID匹配,依此类推,试图让外键工作。 / p>

当试图构建数据库时,编译器说:

  

"#1005 - 无法创建表格' mydb'。'事件' (错误:150"外键约束是非法形成的")"

我想知道我的外键出了什么问题以及我应该如何修复它们。

应该链接的是什么:

  • 用户ID'中的用户ID桌子和桌子用户ID在' userEvents'表
  • sigGroup1-5 in' user& sigID in' SIGS'
  • event1-20在' userEvents'桌子和桌子eventsID in' events'表
  • presenter'中的presenterID桌子和桌子presenterID in' events'表
  • confrenceID in' confrence'桌子和桌子confrenceID in' events'表
  • roomID in' room'桌子和桌子roomID in' events'表
  • tag1-5 in' events'桌子和桌子标签ID在'标签'表
  • 场地ID在' confrence'桌子和桌子场地ID在' room'表
  • 主持人'主持人'桌子和桌子confrenceID in' confrence'表

如果需要,我可以发布ERR图。

提前感谢您的帮助。 FK对我来说一直都很棘手。

这是我的sql代码。除了FK之外,一切都应该没问题。

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,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`tags`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`tags` ;

CREATE TABLE IF NOT EXISTS `mydb`.`tags` (
  `tagsID` INT NOT NULL,
  `Tags` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`tagsID`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`confrence`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`confrence` ;

CREATE TABLE IF NOT EXISTS `mydb`.`confrence` (
  `confrenceID` INT NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `desc` VARCHAR(200) NULL,
  `venue` VARCHAR(75) NOT NULL,
  `venueID` INT NOT NULL,
  `googleLink` VARCHAR(250) NULL,
  PRIMARY KEY (`confrenceID`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`room`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`room` ;

CREATE TABLE IF NOT EXISTS `mydb`.`room` (
  `roomID` INT NOT NULL,
  `venueID` INT NOT NULL,
  `roomNumber` INT NOT NULL,
  `roomName` VARCHAR(45) NOT NULL,
  `capacity` INT NOT NULL,
  PRIMARY KEY (`roomID`),
  INDEX `fk_room_confrence_idx` (`venueID` ASC),
  CONSTRAINT `fk_room_confrence`
    FOREIGN KEY (`venueID`)
    REFERENCES `mydb`.`confrence` (`confrenceID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`presenter`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`presenter` ;

CREATE TABLE IF NOT EXISTS `mydb`.`presenter` (
  `presenterID` INT NOT NULL,
  `confrenceID` INT NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `link` VARCHAR(200) NOT NULL,
  `events_eventsID` INT NOT NULL,
  PRIMARY KEY (`presenterID`),
  INDEX `fk_presenter_confrence_idx` (`confrenceID` ASC),
  CONSTRAINT `fk_presenter_confrence`
    FOREIGN KEY (`confrenceID`)
    REFERENCES `mydb`.`confrence` (`confrenceID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`events`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`events` ;

CREATE TABLE IF NOT EXISTS `mydb`.`events` (
  `eventsID` INT NOT NULL,
  `name` VARCHAR(45) NOT NULL,
  `presenterID` INT NOT NULL,
  `startTime` INT NOT NULL,
  `endTime` INT NOT NULL,
  `desc` VARCHAR(250) NULL,
  `tag1` INT NULL,
  `tag2` INT NULL,
  `tag3` INT NULL,
  `tag4` INT NULL,
  `tag5` INT NULL,
  `roomID` INT NULL,
  `confrenceID` INT NULL,
  `date` DATE NULL,
  PRIMARY KEY (`eventsID`),
  INDEX `fk_events_tags_idx` (`tag1` ASC, `tag2` ASC, `tag3` ASC, `tag4` ASC, `tag5` ASC),
  INDEX `fk_events_confrence_idx` (`confrenceID` ASC),
  INDEX `fk_events_room_idx` (`roomID` ASC),
  INDEX `fk_events_presenter_idx` (`presenterID` ASC),
  CONSTRAINT `fk_events_tags`
    FOREIGN KEY (`tag1` , `tag2` , `tag3` , `tag4` , `tag5`)
    REFERENCES `mydb`.`tags` (`tagsID` , `tagsID` , `tagsID` , `tagsID` ,  `tagsID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
    CONSTRAINT `fk_events_confrence`
    FOREIGN KEY (`confrenceID`)
    REFERENCES `mydb`.`confrence` (`confrenceID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_events_room`
    FOREIGN KEY (`roomID`)
    REFERENCES `mydb`.`room` (`roomID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_events_presenter`
    FOREIGN KEY (`presenterID`)
    REFERENCES `mydb`.`presenter` (`presenterID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`userEvents`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`userEvents` ;

CREATE TABLE IF NOT EXISTS `mydb`.`userEvents` (
  `userID` INT NOT NULL,
  `event1` INT NULL,
  `event2` INT NULL,
  `event3` INT NULL,
  `event4` INT NULL,
  `event5` INT NULL,
  `event6` INT NULL,
  `event7` INT NULL,
  `event8` INT NULL,
  `event9` INT NULL,
  `event10` INT NULL,
  `event11` INT NULL,
  `event12` INT NULL,
  `event13` INT NULL,
  `event14` INT NULL,
  `event15` INT NULL,
  `event16` INT NULL,
  `event17` INT NULL,
  `event18` INT NULL,
  `event19` INT NULL,
  `event20` INT NULL,
  PRIMARY KEY (`userID`),
  INDEX `fk_userEvents_events_idx` (`event1` ASC, `event2` ASC, `event3` ASC, `event4` ASC, `event5` ASC, `event6` ASC, `event7` ASC, `event8` ASC, `event9` ASC, `event10` ASC, `event11` ASC, `event12` ASC, `event13` ASC, `event14` ASC, `event15` ASC, `event16` ASC, `event17` ASC, `event18` ASC, `event19` ASC, `event20` ASC),
  CONSTRAINT `fk_userEvents_events`
    FOREIGN KEY (`event1` , `event2` , `event3` , `event4` , `event5` , `event6` , `event7` , `event8` , `event9` , `event10` , `event11` , `event12` , `event13` , `event14` , `event15` , `event16` , `event17` , `event18` , `event19` , `event20`)
    REFERENCES `mydb`.`events` (`eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID` , `eventsID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`SIGS`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`SIGS` ;

CREATE TABLE IF NOT EXISTS `mydb`.`SIGS` (
  `sigID` INT NOT NULL,
  `sigName` VARCHAR(3) NOT NULL,
  PRIMARY KEY (`sigID`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`user` ;

CREATE TABLE IF NOT EXISTS `mydb`.`user` (
  `userID` INT NOT NULL,
  `username` VARCHAR(30) NOT NULL,
  `type` INT NOT NULL,
  `password` VARCHAR(30) NOT NULL,
  `sigGroup1` INT NULL,
  `sigGroup1Admin` INT NULL,
  `sigGroup2` INT NULL,
  `sigGroup2Admin` INT NULL,
  `sigGroup3` INT NULL,
  `sigGroup3Admin` INT NULL,
  `sigGroup4` INT NULL,
  `sigGroup4Admin` INT NULL,
  `sigGroup5` INT NULL,
  `sigGroup5Admin` INT NULL,
  PRIMARY KEY (`userID`),
  INDEX `fk_user_SIGS_idx` (`sigGroup1` ASC, `sigGroup2` ASC, `sigGroup3` ASC, `sigGroup4` ASC, `sigGroup5` ASC),
  CONSTRAINT `fk_user_userEvents`
    FOREIGN KEY (`userID`)
    REFERENCES `mydb`.`userEvents` (`userID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_user_SIGS`
    FOREIGN KEY (`sigGroup1` , `sigGroup2` , `sigGroup3` , `sigGroup4` , `sigGroup5`)
    REFERENCES `mydb`.`SIGS` (`sigID` , `sigID` , `sigID` , `sigID` , `sigID`)
    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;

-- -----------------------------------------------------
-- Data for table `mydb`.`user`
-- -----------------------------------------------------
START TRANSACTION;
USE `mydb`;
INSERT INTO `mydb`.`user` (`userID`, `username`, `type`, `password`, `sigGroup1`, `sigGroup1Admin`, `sigGroup2`, `sigGroup2Admin`, `sigGroup3`, `sigGroup3Admin`, `sigGroup4`, `sigGroup4Admin`, `sigGroup5`, `sigGroup5Admin`)     VALUES (1, 'admin', 1, 'admin', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

COMMIT;

0 个答案:

没有答案