语法在SQL中创建表时出错

时间:2014-02-15 05:10:34

标签: mysql sql innodb

我正在尝试此查询,尽管进行了多次尝试,但在创建表调用时仍然出现语法错误。其他表格创建得很好..我不明白为什么

学生代码

  CREATE TABLE Student (
      `student_id` int NOT NULL AUTO_INCREMENT,
      `phone_number` varchar(50) NOT NULL,
      `name` varchar(50) NOT NULL,
      `school` varchar(50) NOT NULL,
      `class` varchar(50) NOT NULL,

      PRIMARY KEY (`student_id`)
    )

表故事

    CREATE TABLE Stories (
      `story_id` int NOT NULL AUTO_INCREMENT,
      `story_name` varchar(50) NOT NULL,
      `number_questions` int NOT NULL,
      `file_name` varchar(100) NOT NULL,

      PRIMARY KEY (`story_id`)
    )

表格问题

    CREATE TABLE Questions (
      `question_id` int NOT NULL,
      `story_id` int NOT NULL,
      `file_name` varchar(100) NOT NULL,
      `concept_tested` varchar(100) NOT NULL,
      `difficuly` varchar(50) NOT NULL,
      `number_options` int NOT NULL,
      `correct_answer` int NOT NULL,
      `call_number` int NOT NULL,

      PRIMARY KEY (`question_id`,`story_id`),

      FOREIGN KEY (`story_id`)
      REFERENCES `Stories`(`story_id`)
      ON DELETE CASCADE
    )

表格调用

    CREATE TABLE Call (
      `call_id` int NOT NULL AUTO_INCREMENT,
      `student_id` int NOT NULL,
      `story_id` int NOT NULL,
      `question_id` int NOT NULL,
      `call_number` int NOT NULL,
      `total_number` int NOT NULL,

      PRIMARY KEY (`call_id`),

      FOREIGN KEY (`student_id`)
      REFERENCES `Student`(`story_id`)
      ON DELETE CASCADE,

      FOREIGN KEY (`story_id`)
      REFERENCES `Stories`(`student_id`)
      ON DELETE CASCADE,

      FOREIGN KEY (`question_id`)
      REFERENCES `Stories`(`question_id`)
      ON DELETE CASCADE
    )

这是我的错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Call ( `call_id` int NOT NULL AUTO_INCREMENT, `student_id` int NOT NULL, ' at line 1

我已尝试编辑我的代码并多次检查但问题仍未解决

我解决了这个问题,

是:

CREATE TABLE `Call` (
  `call_id` int NOT NULL AUTO_INCREMENT,
  `student_id` int NOT NULL,
  `story_id` int NOT NULL,
  `question_id` int NOT NULL,
  `call_number` int NOT NULL,
  `total_number` int NOT NULL,

  PRIMARY KEY (`call_id`),

  FOREIGN KEY (`student_id`)
  REFERENCES `Student`(`student_id`)
  ON DELETE CASCADE,

  FOREIGN KEY (`story_id`)
  REFERENCES `Stories`(`story_id`)
  ON DELETE CASCADE,

  FOREIGN KEY (`question_id`)
  REFERENCES `Questions`(`question_id`)
  ON DELETE CASCADE
)

1 个答案:

答案 0 :(得分:0)

我发现必须满足这些条件才能得到错误150:

  1. 这两个表必须是ENGINE = InnoDB。 (可以是其他人:ENGINE = MyISAM也可以)
  2. 这两个表必须具有相同的字符集。
  3. 父表和FK列中的PK列必须是相同的数据类型。
  4. 父表和FK列中的PK列(如果它们具有定义排序规则类型)必须具有相同的排序规则类型;
  5. 如果外键表中已有数据,则FK列值必须与父表PK列中的值匹配。
  6. 子表不能是临时表。
  7. 希望这有帮助。

    试试这个

    CREATE TABLE Questions (
      `call_id` int NOT NULL AUTO_INCREMENT,
      `student_id` int NOT NULL,
      `story_id` int NOT NULL,
      `question_id` int NOT NULL,
      `call_number` int NOT NULL,
      `total_number` int NOT NULL,
    
      PRIMARY KEY (`call_id`),
    
      FOREIGN KEY (`student_id`)
      REFERENCES `Student`(`story_id`)
      ON DELETE CASCADE,
    
      FOREIGN KEY (`story_id`)
      REFERENCES `Stories`(`student_id`)
      ON DELETE CASCADE,
    
      FOREIGN KEY (`question_id`)
      REFERENCES `Stories`(`question_id`)
      ON DELETE CASCADE
    )ENGINE=MYISAM CHARACTER SET UTF8;
    

    FIDDLE DEMO

    查看here