【问题标题】:When creating a foreign key I am getting the error "Syntax error: missing 'closing parenthesis'创建外键时出现错误“语法错误:缺少'右括号'
【发布时间】:2015-04-07 19:17:27
【问题描述】:

我正在使用 MySQL Workbench。
行内:

REFERENCES section_t(section_id, semester, year)

我收到了错误

"Syntax error: missing 'closing parenthesis'". 

“学期”一词带有下划线,是错误的来源。

我不明白为什么 MySQL 在这里要求右括号。我用多个字段构建了其他几个外键,这些外键工作正常,但是在这个特定的地方我遇到了错误。我想也许我的保留字有问题,但情况似乎并非如此。我也看不到括号放错地方了,或者漏掉了一个。

如果有任何帮助,将不胜感激。

这里是 SQL:

-- MySQL Workbench Forward Engineering

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';

-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema cphillips03
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema cphillips03
-- -----------------------------------------------------
USE `cphillips03` ;
DROP TABLE IF EXISTS `cphillips03`.`advisor_t` ;
DROP TABLE IF EXISTS `cphillips03`.`classroom_t` ;
DROP TABLE IF EXISTS `cphillips03`.`course_t` ;
DROP TABLE IF EXISTS `cphillips03`.`department_t` ;
DROP TABLE IF EXISTS `cphillips03`.`instructor_t` ;
DROP TABLE IF EXISTS `cphillips03`.`instructor_t` ;
DROP TABLE IF EXISTS `cphillips03`.`prereq_t` ;
DROP TABLE IF EXISTS `cphillips03`.`section_t` ;
DROP TABLE IF EXISTS `cphillips03`.`student_t` ;
DROP TABLE IF EXISTS `cphillips03`.`takes_t` ;
DROP TABLE IF EXISTS `cphillips03`.`teaches_t` ;
DROP TABLE IF EXISTS `cphillips03`.`timeslot_t` ;

-- -----------------------------------------------------
-- Table `cphillips03`.`classroom_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`classroom_t` (
  `building` VARCHAR(15) NOT NULL DEFAULT '',
  `room_number` VARCHAR(7) NOT NULL DEFAULT '',
  `capacity` DECIMAL(4,0) NULL DEFAULT NULL,
  PRIMARY KEY (`building`, `room_number`))
;
-- -----------------------------------------------------
-- Insert commands for `cphillips03`.`classroom_t`
-- -----------------------------------------------------
insert into classroom_t values('Lamberton', 134, 10);

-- -----------------------------------------------------
-- Table `cphillips03`.`timeslot_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`timeslot_t` (
  `timeslot_id` VARCHAR(4) NOT NULL DEFAULT '',
  `day` VARCHAR(1) NOT NULL DEFAULT '',
  `start_hr` TIME NOT NULL DEFAULT '00:00:00',
  `start_min` TIME NOT NULL DEFAULT '00:00:00',
  `end_hour` TIME NULL DEFAULT NULL,
  `end_min` TIME NULL DEFAULT NULL,
  PRIMARY KEY (`timeslot_id`, `day`, `start_hr`, `start_min`))
 ;

-- -----------------------------------------------------
-- Table `cphillips03`.`department_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`department_t` (
  `dept_name` VARCHAR(20) NOT NULL DEFAULT '',
  `building` VARCHAR(15) NULL DEFAULT NULL,
  `budget` DECIMAL(12,2) NULL DEFAULT NULL,
  PRIMARY KEY (`dept_name`),
  FOREIGN KEY (building)
    REFERENCES classroom_t(building)
    ON UPDATE CASCADE
    )
 ;


-- -----------------------------------------------------
-- Table `cphillips03`.`course_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`course_t` (
  `course_id` VARCHAR(8) NOT NULL DEFAULT '',
  `title` VARCHAR(50) NULL DEFAULT NULL,
  `dept_name` VARCHAR(20) NULL DEFAULT NULL,
  `credits` DECIMAL(2,0) NULL DEFAULT NULL,
  PRIMARY KEY (`course_id`),
  FOREIGN KEY (dept_name)
    REFERENCES department_t(dept_name)
    ON UPDATE CASCADE
    )
;




-- -----------------------------------------------------
-- Table `cphillips03`.`instructor_t`
-- -----------------------------------------------------

CREATE TABLE IF NOT EXISTS `cphillips03`.`instructor_t` (
  `instructor_id` VARCHAR(5) NOT NULL DEFAULT '',
  `name` VARCHAR(20) NOT NULL,
  `dept_name` VARCHAR(20) NULL DEFAULT NULL,
  `salary` DECIMAL(8,2) NULL DEFAULT NULL,
  PRIMARY KEY (`instructor_id`),
  FOREIGN KEY (dept_name)
    REFERENCES department_t(dept_name)
    ON UPDATE CASCADE
    )
 ;


-- -----------------------------------------------------
-- Table `cphillips03`.`prereq_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`prereq_t` (
  `course_id` VARCHAR(8) NOT NULL DEFAULT '',
  `prereq_id` VARCHAR(8) NOT NULL DEFAULT '',
  PRIMARY KEY (`course_id`, `prereq_id`),
  FOREIGN KEY (course_id)
    REFERENCES course_t(course_id)
    ON UPDATE CASCADE
    )
 ;


-- -----------------------------------------------------
-- Table `cphillips03`.`section_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`section_t` (
  `course_id` VARCHAR(8) NOT NULL DEFAULT '',
  `section_id` VARCHAR(8) NOT NULL DEFAULT '',
  `semester` VARCHAR(6) NOT NULL DEFAULT '',
  `year` DECIMAL(4,0) NOT NULL DEFAULT '0',
  `building` VARCHAR(15) NULL DEFAULT NULL,
  `room_number` VARCHAR(7) NULL DEFAULT NULL,
  `timeslot_id` VARCHAR(4) NULL DEFAULT NULL,
  PRIMARY KEY (`course_id`, `section_id`, `semester`, `year`),
  FOREIGN KEY (course_id)
    REFERENCES course_t(course_id)
    ON UPDATE CASCADE,
  FOREIGN KEY (building, room_number)
    REFERENCES classroom_t(building, room_number)
    ON UPDATE CASCADE,
  FOREIGN KEY (timeslot_id)
    REFERENCES timeslot_t(timeslot_id)
    ON UPDATE CASCADE
    )
 ;


-- -----------------------------------------------------
-- Table `cphillips03`.`student_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`student_t` (
  `student_id` VARCHAR(5) NOT NULL DEFAULT '',
  `name` VARCHAR(20) NOT NULL,
  `dept_name` VARCHAR(20) NULL DEFAULT NULL,
  `tot_cred` DECIMAL(3,0) NULL DEFAULT NULL,
  PRIMARY KEY (`student_id`),
  FOREIGN KEY (dept_name)
    REFERENCES department_t(dept_name)
    ON UPDATE CASCADE
    )
 ;


-- -----------------------------------------------------
-- Table `cphillips03`.`takes_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`takes_t` (
  `takes_id` VARCHAR(5) NOT NULL DEFAULT '',
  `course_id` VARCHAR(8) NOT NULL DEFAULT '',
  `sec_id` VARCHAR(8) NOT NULL DEFAULT '',
  `semester` VARCHAR(6) NOT NULL DEFAULT '',
  `year` DECIMAL(4,0) NOT NULL DEFAULT '0',
  `grade` VARCHAR(2) NULL DEFAULT NULL,
  PRIMARY KEY (`takes_id`, `course_id`, `sec_id`, `semester`, `year`),
  FOREIGN KEY (course_id)
    REFERENCES course_t(course_id)
    ON UPDATE CASCADE,
  FOREIGN KEY (sec_id, semester, year)
    REFERENCES section_t(section_id, semester, year)
    ON UPDATE CASCADE
    )
;


-- -----------------------------------------------------
-- Table `cphillips03`.`teaches_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`teaches_t` (
  `teaches_id` VARCHAR(5) NOT NULL DEFAULT '',
  `course_id` VARCHAR(8) NOT NULL DEFAULT '',
  `sec_id` VARCHAR(8) NOT NULL DEFAULT '',
  `semester` VARCHAR(6) NOT NULL DEFAULT '',
  `year` DECIMAL(4,0) NOT NULL DEFAULT '0',
  PRIMARY KEY (`teaches_id`, `course_id`, `sec_id`, `semester`, `year`),
  FOREIGN KEY (course_id)
    REFERENCES course_t(course_id)
    ON UPDATE CASCADE
    )
 ;



-- -----------------------------------------------------
-- Table `cphillips03`.`advisor_t`
-- -----------------------------------------------------


CREATE TABLE IF NOT EXISTS `cphillips03`.`advisor_t` (
  `student_id` VARCHAR(5) NOT NULL DEFAULT '',
  `instructor_id` VARCHAR(5) NOT NULL DEFAULT '',
  PRIMARY KEY (`student_id`, `instructor_id`),
  FOREIGN KEY (student_id)
    REFERENCES student_t(student_id)
    ON UPDATE CASCADE,
  FOREIGN KEY (instructor_id)
    REFERENCES instructor_t(instructor_id)
    ON UPDATE CASCADE
    )
 ;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

【问题讨论】:

  • 您确定发布的代码与您尝试过的相同吗?
  • 感谢您的回复,以赛亚。我从查询窗口复制了文本并按原样粘贴。我需要在两个地方识别这个外键,并多次从头开始重写,以确保我没有 type-o 或缺少括号。这确实是实际的文本。如果我可以提供任何其他说明,请告诉我。
  • 给我看看你的 course_t 和 section_t 表定义,这样我就可以测试了。
  • 包括主键在内的一切都很好(包括最后几行),所以它特定于外键.....
  • 以赛亚,我认为你完全正确。我想我在某个地方定义了一个错误的主键,然后在错误的表中定义了一个外键。我想弄清楚是否需要将“学期”字段定义为不同表中的主字段(可能像takes_t),然后在其他表之一中作为外键引用?再次,我真的很感谢你花时间来帮助男人。

标签: foreign-keys syntax-error mysql-workbench


【解决方案1】:

主要解决方案

编辑:经过大量讨论,我们确定添加索引是解决此问题的最佳方案:

ALTER TABLE section_t
ADD INDEX( section_id, semester, year )

CREATE TABLE section_t (
...
INDEX( section_id, semester, year )

过去的历史讨论

好吧,我没有收到任何错误;你觉得你使用的是最新版本的mysql吗?

代码跑:

CREATE TABLE IF NOT EXISTS takes_t (
takes_id VARCHAR(5) NOT NULL DEFAULT '',
course_id VARCHAR(8) NOT NULL DEFAULT '',
sec_id VARCHAR(8) NOT NULL DEFAULT '',
semester VARCHAR(6) NOT NULL DEFAULT '',
year DECIMAL(4, 0) NOT NULL DEFAULT '0',
grade VARCHAR(2) NULL DEFAULT NULL,
PRIMARY KEY( takes_id, course_id, sec_id, semester, year ),
FOREIGN KEY(course_id)
REFERENCES course_t(course_id)
ON UPDATE CASCADE,
FOREIGN KEY( sec_id, semester, year )
REFERENCES section_t( section_id, semester, year )
ON UPDATE CASCADE
)
ENGINE = INNODB
DEFAULT CHARACTER SET=utf8;

我收到以下回复:

Query OK, 0 rows affected

如果你发现了什么,请告诉我。

编辑:使用您的完整脚本,我在执行 take_t 时遇到以下错误。我已单独输入所有查询:

ERROR 1215 (HY000): Cannot add foreign key constraint

当我在以下行时,我明白了:

FOREIGN KEY(sec_id, semester, year)
REFERENCES section_t( section_id, semester, year )

可能(虽然不太可能)解决方案:将所有 sec_id 实例替换为 section_id

更可能的解决方案:从 section_t 的主键中删除 course_id

原因:外键需要索引;主键的索引可能仅在使用完整主键时才适用。

【讨论】:

  • 或者,您可以删除所有反引号
  • 感谢您的意见。我添加了反引号并得到了同样的错误。我也尝试删除所有反引号,但在“学期”这个词上我仍然遇到同样的错误。
  • 同一个地方?为什么不删除所有的反引号?除非有空格,否则您不必反引号引用列名。
  • 你很快! =) 我编辑了我的回复说我也删除了反引号并得到了相同的结果。我感谢你的努力。谢谢。
  • 您尝试过我刚刚发布/编辑的内容了吗?另外,我是直接在 mysql 命令行程序中输入的。
猜你喜欢
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 2015-03-31
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
相关资源
最近更新 更多