MySql: Simple Problem with Creating Tables & Foreign Keys
I have problems with creating tables & foreign key. (errno:150) here is a screenshot Updated Image: http://img264.imagevenue.com/img.php?image=14866_db_122_223lo.jpg
Maybe the problem is the database design.
-- -----------------------------------------------------
-- Table `mydb`.`projekt_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_user` (
`UserID` INT NOT NULL ,
`Name` VARCHAR(45) NULL ,
PRIMARY KEY (`UserID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekt_auswahl`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_auswahl` (
`UserID` INT NOT NULL ,
`projekt_id` INT NOT NULL ,
`projekt_kategorie_id` INT NOT NULL ,
`projekt_attachment` VARCHAR(45) NULL ,
PRIMARY KEY (`UserID`, `projekt_id`, `projekt_kategorie_id`) ,
INDEX `UserID` (`UserID` ASC) ,
CONSTRAINT `UserID`
FOREIGN KEY (`UserID` )
REFERENCES `mydb`.`projekt_user` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekt_kategorien`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_kategorien` (
`pro开发者_StackOverflow社区jekt_kategorie_id` INT NOT NULL ,
`kategorie` VARCHAR(45) NULL ,
PRIMARY KEY (`projekt_kategorie_id`) ,
INDEX `projekt_kategorie_id` (`projekt_kategorie_id` ASC) ,
CONSTRAINT `projekt_kategorie_id`
FOREIGN KEY (`projekt_kategorie_id` )
REFERENCES `mydb`.`projekt_auswahl` (`projekt_kategorie_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekte`
-- -----------------------------------------------------
CREATE TABLE `projekte` (
`projekt_id` INT NOT NULL ,
`projektname` VARCHAR(45) NULL ,
`projekt_status` VARCHAR(45) NULL ,
PRIMARY KEY (`projekt_id`) ,
INDEX `projekt_id` (`projekt_id` ASC) ,
CONSTRAINT `projekt_id`
FOREIGN KEY (`projekt_id` )
REFERENCES `mydb`.`projekt_auswahl` (`projekt_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
You have your foreign keys in the wrong direction, and need to load the tables in order (parents before children):
-- -----------------------------------------------------
-- Table `mydb`.`projekt_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_user` (
`UserID` INT NOT NULL ,
`Name` VARCHAR(45) NULL ,
PRIMARY KEY (`UserID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekt_kategorien`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_kategorien` (
`projekt_kategorie_id` INT NOT NULL ,
`kategorie` VARCHAR(45) NULL ,
PRIMARY KEY (`projekt_kategorie_id`) ,
INDEX `projekt_kategorie_id` (`projekt_kategorie_id` ASC)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekte`
-- -----------------------------------------------------
CREATE TABLE `projekte` (
`projekt_id` INT NOT NULL ,
`projektname` VARCHAR(45) NULL ,
`projekt_status` VARCHAR(45) NULL ,
PRIMARY KEY (`projekt_id`) ,
INDEX `projekt_id` (`projekt_id` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekt_auswahl`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `projekt_auswahl` (
`UserID` INT NOT NULL ,
`projekt_id` INT NOT NULL ,
`projekt_kategorie_id` INT NOT NULL ,
`projekt_attachment` VARCHAR(45) NULL ,
PRIMARY KEY (`UserID`, `projekt_id`, `projekt_kategorie_id`) ,
INDEX `UserID` (`UserID` ASC) ,
CONSTRAINT `UserID`
FOREIGN KEY (`UserID` )
REFERENCES `projekt_user` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `projekt_kategorie_id`
FOREIGN KEY (`projekt_kategorie_id` )
REFERENCES `projekt_kategorien` (`projekt_kategorie_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `projekt_id`
FOREIGN KEY (`projekt_id` )
REFERENCES `projekte` (`projekt_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Had I paid more attention to the ERD you provided, I'd have seen that the "crows feet" were pointing upwards, when the convention is they always point down (or the left if necessary).
精彩评论