ERROR: Error 1005: Can't create table 'db.pics' (errno: 150)
SQL Code:
CREATE TABLE IF NOT EXISTS `db`.`pics` (
`pic_id` INT NOT NULL COMMENT '',
PRIMARY KEY (`pic_id`) COMMENT '',
CONSTRAINT `fk_pics_houses1`
FOREIGN KEY (`pic_id`)
REFERENCES `db`.`houses` (`pic_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
我已确保值与外键(pic_id)和我的house表中的主键相同。我也创建了索引所以我真的不确定这个错误会在哪里发生。如果任何人都可以提供任何其他解释为什么错误1005发生,这将是非常有帮助的。谢谢。
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';
CREATE SCHEMA IF NOT EXISTS `db` DEFAULT CHARACTER SET latin1 ;
USE `db` ;
DROP TABLE IF EXISTS `db`.`users` ;
CREATE TABLE IF NOT EXISTS `db`.`users` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '',
PRIMARY KEY (`user_id`) COMMENT '',
ENGINE = InnoDB
AUTO_INCREMENT = 24
DEFAULT CHARACTER SET = latin1;
DROP TABLE IF EXISTS `db`.`houses` ;
CREATE TABLE IF NOT EXISTS `db`.`houses` (
`house_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '',
`user_id` INT(11) NOT NULL COMMENT '',
`pic_id` INT(11) NOT NULL COMMENT '',
PRIMARY KEY (`house_id`) COMMENT '',
CONSTRAINT `fk_houses_users1`
FOREIGN KEY (`user_id`)
REFERENCES `db`.`users` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 25
DEFAULT CHARACTER SET = latin1;
CREATE INDEX `fk_houses_users1_idx` ON `db`.`houses` (`user_id` ASC) COMMENT '';
DROP TABLE IF EXISTS `db`.`pics` ;
CREATE TABLE IF NOT EXISTS `db`.`pics` (
`pic_id` INT NOT NULL COMMENT '',
PRIMARY KEY (`pic_id`) COMMENT '',
CONSTRAINT `fk_pics_houses1`
FOREIGN KEY (`pic_id`)
REFERENCES `db`.`houses` (`pic_id`)
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;
答案 0 :(得分:1)
我刚刚创建了一个索引fk_houses_pics_idx
,但它确实有效。这是更新的代码:
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';
CREATE SCHEMA IF NOT EXISTS `db` DEFAULT CHARACTER SET latin1 ;
USE `db` ;
DROP TABLE IF EXISTS `db`.`users` ;
CREATE TABLE IF NOT EXISTS `db`.`users` (
`user_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '',
PRIMARY KEY (`user_id`) COMMENT ''
)
ENGINE = InnoDB
AUTO_INCREMENT = 24
DEFAULT CHARACTER SET = latin1;
DROP TABLE IF EXISTS `db`.`houses` ;
CREATE TABLE IF NOT EXISTS `db`.`houses` (
`house_id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '',
`user_id` INT(11) NOT NULL COMMENT '',
`pic_id` INT(11) NOT NULL COMMENT '',
PRIMARY KEY (`house_id`) COMMENT '',
CONSTRAINT `fk_houses_users1`
FOREIGN KEY (`user_id`)
REFERENCES `db`.`users` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 25
DEFAULT CHARACTER SET = latin1;
CREATE INDEX `fk_houses_users1_idx` ON `db`.`houses` (`user_id` ASC) COMMENT '';
CREATE INDEX `fk_houses_pics_idx` ON `db`.`houses` (`pic_id` ASC) COMMENT ''; /*This is what I just added */
DROP TABLE IF EXISTS `db`.`pics` ;
CREATE TABLE IF NOT EXISTS `db`.`pics` (
`pic_id` INT NOT NULL COMMENT '',
PRIMARY KEY (`pic_id`) COMMENT '',
CONSTRAINT `fk_pics_houses1`
FOREIGN KEY (`pic_id`)
REFERENCES `db`.`houses` (`pic_id`)
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;
那为什么会这样呢?创建外键约束时,MySQL需要在引用表和引用表上都有可用的索引。如果不存在,则会自动创建引用表上的索引,但需要手动创建引用表上的索引(Source)。你的似乎不见了。
所以,您似乎缺少pic_id
表上houses
的索引。添加它解决了这个问题。