MySQL 5.1 #1005 - Can't create table 'data

2019-08-04 16:46发布

问题:

i'm in the phpMyadmin, MySQL 5.1 version, and i'm trying to run this SQL below, but when i exe this code on phpmyadin, it's given the error Can't create table 'datacode.foto' (errno: 150), there's someone that knows what's happing with my code ?

CREATE  TABLE IF NOT EXISTS `projeto` (
`proj_id` INT NOT NULL AUTO_INCREMENT ,
  `proj_nome` VARCHAR(40) NULL ,
  `proje_descr` VARCHAR(700) NULL ,
  `proj_oque_fizemos` VARCHAR(200) NULL ,
  `proj_url` VARCHAR(250) NULL ,
  `proj_descr_url` VARCHAR(250) NULL ,
  PRIMARY KEY (`proj_id`) )
ENGINE = InnoDB;



CREATE  TABLE IF NOT EXISTS `categoria` (
  `cat_id` INT NOT NULL AUTO_INCREMENT ,
  `cat_nome` VARCHAR(45) NULL ,
  PRIMARY KEY (`cat_id`) )
ENGINE = InnoDB;



CREATE  TABLE IF NOT EXISTS `foto` (
  `foto_id` INT NOT NULL AUTO_INCREMENT ,
  `foto_url` VARCHAR(270) NULL ,
  `projeto_proj_id` INT NOT NULL ,
  PRIMARY KEY (`foto_id`) ,
  INDEX `fk_foto_projeto1_idx` (`projeto_proj_id` ASC) ,
  CONSTRAINT `fk_foto_projeto1`
    FOREIGN KEY (`projeto_proj_id` )
    REFERENCES `mydb`.`projeto` (`proj_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

CREATE  TABLE IF NOT EXISTS `projeto_por_categoria` (
  `projeto_proj_id` INT NOT NULL ,
  `categoria_cat_id` INT NOT NULL ,
  PRIMARY KEY (`projeto_proj_id`, `categoria_cat_id`) ,
  INDEX `fk_projeto_has_categoria_categoria1_idx` (`categoria_cat_id` ASC) ,
  INDEX `fk_projeto_has_categoria_projeto_idx` (`projeto_proj_id` ASC) ,
  CONSTRAINT `fk_projeto_has_categoria_projeto`
    FOREIGN KEY (`projeto_proj_id` )
    REFERENCES `mydb`.`projeto` (`proj_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_projeto_has_categoria_categoria1`
    FOREIGN KEY (`categoria_cat_id` )
    REFERENCES `mydb`.`categoria` (`cat_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

回答1:

You're referencing a table in the mydb database, whereas your tables are actually in a database called datacode:

REFERENCES `mydb`.`projeto` (`proj_id` )
           ^^^^^^^--- change (or remove) this database qualifier


回答2:

you get this error because of those lines :

REFERENCES `mydb`.`projeto` (`proj_id` )

REFERENCES `mydb`.`categoria` (`cat_id` )

if you replace them by it should work

REFERENCES `projeto` (`proj_id` )

REFERENCES `categoria` (`cat_id` )

here is the build: http://sqlfiddle.com/#!8/751c2