MySQL5.7引用错误

fnvucqvd  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(375)

我有两张table department 以及 employee . 我想引用 departmentemployee 使用mysql。以下是我的代码和错误信息:

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

下面是错误信息:
错误1064(42000):您的sql语法有错误;在第8行的“foreign key(deptno)references department(deptno))”附近,检查与mysql服务器版本对应的手册,以获得正确的语法

0vvn1miw

0vvn1miw1#

你需要加个逗号( , )在主键和外键定义之间:

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

根据 CREATE TABLE 语法、定义(如列、主键、外键)必须用 , :

CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]

相关问题