create table语句中出现1064 sql错误

时间:2014-02-06 17:47:00

标签: mysql sql

我得到了错误

 #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 ''ID' int, 'MGR_ID' int, 'DEPT_ID' int, 'NAME' VARCHAR2(30), 'SAL' int, ' at line 3 while creating a table. I have already a table department with PK as 'dept_id'. Help lease

CREATE TABLE emp
(
'ID' int, 
'MGR_ID' int, 
'DEPT_ID' int, 
'NAME' VARCHAR2(30), 
'SAL' int, 
'DOJ' DATE, 
 PRIMARY KEY ('ID') , 
 FOREIGN KEY ('MGR_ID') REFERENCES emp('ID') , 
 FOREIGN KEY ('DEPT_ID') REFERENCES department('dept_id') 
) 

3 个答案:

答案 0 :(得分:1)

使用返回刻度(`)来定义对象名称,但不能使用单引号(')。

您的表格定义应为:

CREATE TABLE emp
(
    `ID` int, 
    `MGR_ID` int, 
    `DEPT_ID` int, 
    `NAME` VARCHAR2(30), 
    `SAL` int, 
    `DOJ` DATE, 
     PRIMARY KEY (`ID`) , 
     FOREIGN KEY (`MGR_ID`) REFERENCES emp(`ID`) , 
     FOREIGN KEY (`DEPT_ID`) REFERENCES department(`dept_id`) 
) 

答案 1 :(得分:0)

请勿使用'引用您的字段名称。这会把它们变成字符串,而不是字段名称。

CREATE TABLE emp (
   id int,
   mgr_id int, 
   etc...
);

如果您 DO 需要引用字段名称,因为它是一个保留字,您可以使用反引号:

CREATE TABLE emp (
   `desc` int <---"desc" is a reserved word
   etc...
);

答案 2 :(得分:0)

试试这个

   CREATE TABLE emp
 (
  `ID` int (20), 
  `MGR_ID` int (20), 
  `DEPT_ID` int (20), 
  `NAME` VARCHAR (30), 
  `SAL` int (20), 
  `DOJ` DATE, 
   PRIMARY KEY (ID) , 
   FOREIGN KEY (MGR_ID) REFERENCES emp(ID) , 
   FOREIGN KEY (DEPT_ID) REFERENCES department(dept_id) 
   )