mysql - 在 SQL 中创建表时出现语法错误

标签 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`)
    )

餐 table 故事

    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
)

最佳答案

我发现必须满足这些条件才不会出现错误 150:

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

希望这有帮助。

尝试这样

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

关于mysql - 在 SQL 中创建表时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21793673/

相关文章:

mysql - 两个事务无法在同一个表上获得 IX 锁,MySql

mysql - 在 mysql 中插入行时锁定

MySql InnoDB可重复读取锁的意外行为

MYSQL 区分并显示组值

mysql - 如何计算所有记录但只检索(LIMIT)特定数量以供显示?

sql - 计算估计时间行

MySQL根据表2的INNER JOIN结果更新表1中的列

mysql - 加入没有公共(public)键的多个表

php - 更新 MySQL 和日期时间本地字段

mysql - 从 MS SQL Server 插入到 MySQL 数据库