MySql Error: #105 (Code 150). When I create my database schema I receive an error code of 150.

邮差的信 提交于 2019-12-01 20:47:51

问题


DROP SCHEMA IF EXISTS `YouthMinistry` ;
CREATE SCHEMA IF NOT EXISTS `YouthMinistry` DEFAULT CHARACTER SET utf16 COLLATE utf16_general_ci ;
USE `YouthMinistry` ;

-- -----------------------------------------------------
-- Table `YouthMinistry`.`group`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`group` ;

CREATE TABLE IF NOT EXISTS `YouthMinistry`.`group` (
  `groupid` INT NOT NULL AUTO_INCREMENT ,
  `group_name` VARCHAR(100) NOT NULL ,
  `group_desc` VARCHAR(255) NULL ,
  PRIMARY KEY (`groupid`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `YouthMinistry`.`groupmembers`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`groupmembers` ;

CREATE TABLE IF NOT EXISTS `YouthMinistry`.`groupmembers` (
  `groupid` INT NOT NULL ,
  `memberid` INT NOT NULL ,
  PRIMARY KEY (`groupid`, `memberid`) ,
  INDEX `groupid_idx` (`groupid` ASC) ,
  CONSTRAINT `groupid`
    FOREIGN KEY (`groupid` )
    REFERENCES `YouthMinistry`.`group` (`groupid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `YouthMinistry`.`role`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`role` ;

CREATE TABLE IF NOT EXISTS `YouthMinistry`.`role` (
  `roleid` INT NOT NULL AUTO_INCREMENT ,
  `role_name` VARCHAR(100) NOT NULL ,
  `role_desc` VARCHAR(255) NULL ,
  PRIMARY KEY (`roleid`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `YouthMinistry`.`rolemembers`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`rolemembers` ;

CREATE TABLE IF NOT EXISTS `YouthMinistry`.`rolemembers` (
  `roleid` INT NOT NULL ,
  `memberid` INT NOT NULL ,
  PRIMARY KEY (`roleid`, `memberid`) ,
  INDEX `groupid_idx` (`roleid` ASC) ,
  FOREIGN KEY (`roleid` )
    REFERENCES `YouthMinistry`.`role` (`roleid` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `YouthMinistry`.`users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`users` ;

CREATE TABLE IF NOT EXISTS `YouthMinistry`.`users` (
  `memberid` INT NOT NULL AUTO_INCREMENT ,
  `username` VARCHAR(45) NOT NULL ,
  `password` VARCHAR(45) NOT NULL ,
  `email` VARCHAR(100) NULL ,
  `first_name` VARCHAR(45) NOT NULL ,
  `last_name` VARCHAR(45) NOT NULL ,
  `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated` DATETIME NOT NULL ,
  PRIMARY KEY (`memberid`, `username`, `password`) ,
  UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
  INDEX `memberid_idx` (`memberid` ASC) ,
  INDEX `memberid_idx1` (`memberid` ASC) ,
  FOREIGN KEY (`memberid` )
    REFERENCES `YouthMinistry`.`groupmembers` (`memberid` )
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  FOREIGN KEY (`memberid` )
    REFERENCES `YouthMinistry`.`rolemembers` (`memberid` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `YouthMinistry`.`eventgroup`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`eventgroup` ;

CREATE TABLE IF NOT EXISTS `YouthMinistry`.`eventgroup` (
  `eventid` INT NOT NULL ,
  `groupid` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`eventid`, `groupid`) ,
  INDEX `groupid_idx` (`groupid` ASC) ,
  FOREIGN KEY (`groupid`)
    REFERENCES `YouthMinistry`.`group` (`groupid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `YouthMinistry`.`event`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`event` ;

CREATE TABLE IF NOT EXISTS `YouthMinistry`.`event` (
  `eventid` INT NOT NULL AUTO_INCREMENT ,
  `event_name` VARCHAR(255) NOT NULL ,
  `event_desc` VARCHAR(255) NULL ,
  PRIMARY KEY (`eventid`) ,
  INDEX `eventid_idx` (`eventid` ASC) ,
  FOREIGN KEY (`eventid` )
    REFERENCES `YouthMinistry`.`eventgroup` (`eventid` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

I'm working on creating a database for a website that I'm making and when I run the create script it gives me the following error: Error Code: 1005. Can't create table 'youthministry.users' (errno: 150)

I've looked at the following sources for possible solutions to this error: http://www.webdeveloper.com/forum/showthread.php?68260-mySQL-multiple-foreign-keys http://forums.devarticles.com/mysql-development-50/mysql-foreign-key-problem-errno-150t-7704.html

I've checked the primary and foreign key declarations just to make sure everything is correct.

Any help is much appreciated. Also this is still a prototype and any comments on the initial schema is welcome. I'm still new at database design.


回答1:


You can only create a foreign key on one table that references a key on another table. This specific problem is that memberid is not a key on either groupmembers or rolemembers tables. Simply add KEY (memberid) to those tables and you'll be good to go.

Another issue us that foreign key types must match. eventgroup has groupid varchar, but is referencing the groups table, which has groupid INT. Correct this.


As for suggestions, I very strongly recommend that each primary key be only one column: your auto-increment surrogate key. You should make these unsigned integers too.



来源:https://stackoverflow.com/questions/14888593/mysql-error-105-code-150-when-i-create-my-database-schema-i-receive-an-erro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!