简体   繁体   中英

Unable to create MySQL table: Errorcode 1005

Could you please help me figure this out

When I'm trying to create the table dvdTitle I'm getting below error

Error Code: 1005

Can't create table 'netflixclone.dvdtitle' (errno: 150)

Here is the code. Not sure what's going wrong

CREATE  TABLE IF NOT EXISTS `netflixclone`.`person` (
  `personID` INT NOT NULL ,
  `personFirstName` VARCHAR(50) NULL ,
  `personLastName` VARCHAR(50) NULL ,
  `actor` TINYINT(1)  NULL ,
  `producer` TINYINT(1)  NULL ,
  `director` TINYINT(1)  NULL ,
  PRIMARY KEY (`personID`) );

CREATE  TABLE IF NOT EXISTS `netflixclone`.`dvdTitle` (
  `dvdID` INT NOT NULL AUTO_INCREMENT ,
  `dvdMPPARating` VARCHAR(45) NULL ,
  `dvdProducer` INT NOT NULL,
  `dvdDirector` INT NOT NULL,
  PRIMARY KEY (`dvdID`) ,
   CONSTRAINT `personID`
    FOREIGN KEY (`dvdProducer` , `dvdDirector` )
    REFERENCES `netflixclone`.`person` (`personID` , `personID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

Try to create the 'netflixclone.dvdtitle' table with default charset as utf8.

CREATE  TABLE IF NOT EXISTS `netflixclone`.`dvdTitle` (
  `dvdID` INT NOT NULL AUTO_INCREMENT ,
  `dvdMPPARating` VARCHAR(45) NULL ,
  `dvdProducer` INT NOT NULL,
  `dvdDirector` INT NOT NULL,
  PRIMARY KEY (`dvdID`) ,
   CONSTRAINT `personID`
    FOREIGN KEY (`dvdProducer` , `dvdDirector` )
    REFERENCES `netflixclone`.`person` (`personID` , `personID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION) CHARSET=utf8;

Your constraint is weird. I think you want two constraints here.

CREATE TABLE IF NOT EXISTS `netflixclone`.`dvdTitle` (
  `dvdID` INT NOT NULL AUTO_INCREMENT ,
  `dvdMPPARating` VARCHAR(45) NULL ,
  `dvdProducer` INT NOT NULL,
  `dvdDirector` INT NOT NULL,
   PRIMARY KEY (`dvdID`) ,
   CONSTRAINT FOREIGN KEY (`dvdProducer`) REFERENCES `netflixclone`.`person`
       ON DELETE NO ACTION ON UPDATE NO ACTION),
   CONSTRAINT FOREIGN KEY (`dvdDirector`) REFERENCES `netflixclone`.`person`
       ON DELETE NO ACTION ON UPDATE NO ACTION)
);

FOREIGN KEY ( dvdProducer ) REFERENCES netflixclone . person ( personID ) FOREIGN KEY ( dvdProducer ) REFERENCES netflixclone . person ( personID ) you can try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM