如何解决MySQL 5.7引用错误?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (52)

现有两个表格department和employee,我想使用MySQL引用department中的employee,以下是我的代码:

mysql> CREATE TABLE DEPARTMENT (DEPTNO int (4) NOT NULL,
    -> DNAME CHAR (15), DFLOOR int(1),
    -> PRIMARY KEY (DEPTNO) );
Query OK, 0 rows affected (0.10 sec)
mysql> CREATE TABLE EMPLOYEE (EMPNO int (4) NOT NULL,
    -> ENAME CHAR (10),
    -> MGR CHAR (9),
    -> DEPTNO int (4) NOT NULL,
    -> SAL float(7,2),
    -> JOB_STATUS float(7,2),
    -> PRIMARY KEY (EMPNO)
    -> FOREIGN KEY (DEPTNO) REFERENCES department (DEPTNO));

下面是报错信息:

”ERROR 1064 (42000): 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 (DEPTNO) REFERENCES department (DEPTNO))' at line 8”

提问于
用户回答回答于

您需要在主键和外键定义之间放置一个逗号(,):

CREATE TABLE EMPLOYEE (
  EMPNO INT(4) NOT NULL,
  ENAME CHAR(10),
  MGR CHAR(9),
  DEPTNO INT(4) NOT NULL,
  SAL FLOAT(7, 2),
  JOB_STATUS FLOAT(7, 2),
  PRIMARY KEY (EMPNO), -- here
  FOREIGN KEY (DEPTNO) REFERENCES department (DEPTNO)
);

扫码关注云+社区

领取腾讯云代金券