我收到mysql的错误,我不明白为什么:
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 `feedback` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `feedback` ;
-- -----------------------------------------------------
-- Table `feedback`.`application`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`application` (
`application_id` INT NOT NULL AUTO_INCREMENT,
`app_name` VARCHAR(45) NULL,
PRIMARY KEY (`application_id`),
UNIQUE INDEX `app_name_UNIQUE` (`app_name` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`user` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(45) NULL,
`email` VARCHAR(45) NOT NULL,
`customer_length` VARCHAR(45) NULL,
PRIMARY KEY (`user_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`users_has_application`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`users_has_application` (
`user_id` INT NOT NULL,
`application_id` INT NOT NULL,
PRIMARY KEY (`user_id`, `application_id`),
INDEX `fk_users_has_application_application1_idx` (`application_id` ASC),
INDEX `fk_users_has_application_users_idx` (`user_id` ASC),
CONSTRAINT `fk_users_has_application_users`
FOREIGN KEY (`user_id`)
REFERENCES `feedback`.`user` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_has_application_application1`
FOREIGN KEY (`application_id`)
REFERENCES `feedback`.`application` (`application_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`survey`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`survey` (
`survey_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`description` VARCHAR(255) NULL,
`is_active` TINYINT(1) NULL,
PRIMARY KEY (`survey_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`question` (
`question_id` INT NOT NULL,
`question_text` VARCHAR(255) NULL,
PRIMARY KEY (`question_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`option` (
`option_id` INT NOT NULL AUTO_INCREMENT,
`question_id` INT NOT NULL,
`option_number` INT NOT NULL,
`option_text` TEXT NULL,
INDEX `fk_option_question1_idx` (`question_id` ASC),
PRIMARY KEY (`option_id`),
UNIQUE INDEX `uk_question_option_number_key` (`question_id` ASC, `option_number` ASC),
CONSTRAINT `fk_option_question1`
FOREIGN KEY (`question_id`)
REFERENCES `feedback`.`question` (`question_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`answer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`answer` (
`answer_id` INT NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL,
`option_id` INT NOT NULL,
`date_submitted` DATETIME NOT NULL,
PRIMARY KEY (`answer_id`),
INDEX `fk_answer_user1_idx` (`user_id` ASC),
INDEX `fk_answer_option1_idx` (`option_id` ASC),
CONSTRAINT `fk_answer_user1`
FOREIGN KEY (`user_id`)
REFERENCES `feedback`.`user` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_answer_option1`
FOREIGN KEY (`option_id`)
REFERENCES `feedback`.`option` (`option_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`survey_has_question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`survey_has_question` (
`survey_id` INT NOT NULL,
`question_id` INT NOT NULL,
`question_number` INT NULL,
PRIMARY KEY (`survey_id`, `question_id`),
INDEX `fk_survey_has_question_question1_idx` (`question_id` ASC),
INDEX `fk_survey_has_question_survey1_idx` (`survey_id` ASC),
UNIQUE INDEX `unique_order_key` (`survey_id` ASC, `question_number` ASC),
CONSTRAINT `fk_survey_has_question_survey1`
FOREIGN KEY (`survey_id`)
REFERENCES `feedback`.`survey` (`survey_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_survey_has_question_question1`
FOREIGN KEY (`question_id`)
REFERENCES `feedback`.`question` (`question_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;
错误:
#1005 - Can't create table 'feedback.answer' (errno: 150)
我正在创建我的表作为模板:
我将answer_id添加到答案表的思维过程是我希望用户能够多次填写同一个调查。
为什么答案表会抛出错误?
修改 服务器版本:5.5.29-0ubuntu0.12.04.2 我使用phpmyadmin
导入此内容答案 0 :(得分:1)
您的代码在MYSQL服务器5.1上运行且没有错误。
errno的常见原因:150是您创建引用尚不存在的PK的FK约束。在创建“答案”表之前,请确保先创建“用户”和“选项”表。
为了帮助调试,您可以一次删除一个FK约束,以查看哪个约束正在触发问题。
如果按所示顺序执行代码,我看不到任何可能发生的FK问题。
答案 1 :(得分:0)
试试这个
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';
-- -----------------------------------------------------
-- Table `feedback`.`application`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `application` (
`application_id` INT NOT NULL AUTO_INCREMENT,
`app_name` VARCHAR(45) NULL,
PRIMARY KEY (`application_id`),
UNIQUE INDEX `app_name_UNIQUE` (`app_name` ASC))
;