MySQL 错误 #1064

标签 mysql sql mysql-error-1064

大家好,我在这个表创建位中找不到错误,看起来真的很简单,这就是它给我的内容:

ERROR 1064 at line 3: 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 'FOREIGN KEY(courses_courseDepartmentAbbv))' at line 8

DROP TABLE IF EXISTS courses;

CREATE TABLE courses(
   courses_courseNumber INT NOT NULL AUTO_INCREMENT,
   courses_courseTitle VARCHAR(25) NOT NULL,
   courses_courseTeacher VARCHAR(30) NOT NULL,
   courses_courseCostOfBooks DECIMAL(5,2) NOT NULL,
   courses_courseDepartmentAbbv CHAR(4) NOT NULL,
   PRIMARY KEY (courses_courseNumber),
   FOREIGN KEY (courses_courseTeacher),
   FOREIGN KEY (courses_courseDepartmentAbbv)
);

DROP TABLE IF EXISTS departments;

CREATE TABLE departments(
    departments_departmentAbbv CHAR(4) NOT NULL,
    departments_departmentFullName VARCHAR(15) NOT NULL,
    PRIMARY KEY (departments_departmentAbbv),
    FOREIGN KEY (departments_departmentAbbv) REFERENCES (courses_courseDepartmentAbbv)
);

DROP TABLE IF EXISTS teachers;

CREATE TABLE teachers(
          teachers_teacherName VARCHAR(20) NOT NULL,
    teachers_teacherHomeroom SMALLINT(3) NOT NULL,
    teachers_teacherHomeroomGrade SMALLINT(1) NOT NULL,
    teachers_teacherFullTime BOOL NOT NULL,
    PRIMARY KEY (teachers_teacherName),
    FOREIGN KEY (teachers_teacherName) REFERENCES (courses_courseTeacher)
);

最佳答案

每个外键后面都需要有一个引用。您在类(class)的开始设置中错过了这一点。这是documentation

我认为这更符合您的要求。您的创建顺序不正确。因此,您的外键位于错误的位置。您只需在与 PK 相关的表上设置外键映射。您只需要在其他 table 上设置PK即可。只要您以正确的顺序创建表,就可以执行此操作,如下所示。

因此,教师部门 的主键是courses 表中的外键。 教师部门不需要担心外键。您将其留给实际具有引用的表(类(class))

DROP TABLE IF EXISTS teachers;

CREATE TABLE teachers(
          teachers_teacherName VARCHAR(20) NOT NULL,
    teachers_teacherHomeroom SMALLINT(3) NOT NULL,
    teachers_teacherHomeroomGrade SMALLINT(1) NOT NULL,
    teachers_teacherFullTime BOOL NOT NULL,
    PRIMARY KEY (teachers_teacherName)
    --FOREIGN KEY (teachers_teacherName) REFERENCES courses (courses_courseTeacher)
    --This is not where you set up the FK for courses
);

DROP TABLE IF EXISTS departments;

CREATE TABLE departments(
    departments_departmentAbbv CHAR(4) NOT NULL,
    departments_departmentFullName VARCHAR(15) NOT NULL,
    PRIMARY KEY (departments_departmentAbbv)
    --FOREIGN KEY (departments_departmentAbbv)  REFERENCES courses (courses_courseDepartmentAbbv)
    --This is not where you set up the FK for courses
);

CREATE TABLE courses(
   courses_courseNumber INT NOT NULL AUTO_INCREMENT,
   courses_courseTitle VARCHAR(25) NOT NULL,
   courses_courseTeacher VARCHAR(30) NOT NULL,
   courses_courseCostOfBooks DECIMAL(5,2) NOT NULL,
   courses_courseDepartmentAbbv CHAR(4) NOT NULL,
   PRIMARY KEY (courses_courseNumber),
   FOREIGN KEY (courses_courseTeacher) 
       REFERENCES teachers (teachers_teacherName)
   FOREIGN KEY (courses_courseDepartmentAbbv) 
       REFERENCES departments(departments_departmentAbbv)
);

关于MySQL 错误 #1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9848524/

相关文章:

mysql - sql权限分组

java - .hbm hibernate 文件中的复杂 order-by 子句

java - PreparedStatement.setString() 方法不带引号

php - MySQL 与 SQL AS 错误

mysql - MySQL 中 "ORDER BY order DESC"附近的语法错误

php - MySQL查询选择所有日期等于日期时间的今天

php - 在 PHP 中检索值

mysql如何对同一个表进行内部连接和第二个内部连接?

sql - 是否可以在 Symfony3 中的实体内定义自定义 SQL 查询

sql - mySQL JOIN 语句未返回我想要的内容