语法有什么问题?

时间:2013-04-11 17:54:22

标签: mysql create-table

CREATE TABLE Existing_medical_condition (
    condition VARCHAR(50) NOT NULL, 
    date_of_diagnosis DATE, treatment TEXT, 
    pssn INT NOT NULL, 
    CONSTRAINT emc_key PRIMARY KEY(condition, pssn), 
    FOREIGN KEY(pssn) REFERENCES Patient(ssn)
);

给出错误:

  

错误1064(42000):您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'条件VARCHAR(50)NOT NULL,date_of_diagnosis DATE,处理TEXT,pssn INT'第1行附近使用正确的语法

2 个答案:

答案 0 :(得分:7)

条件是reserved word。你必须用反引号包住它:

CREATE TABLE Existing_medical_condition (
    `condition` VARCHAR(50) NOT NULL,
    date_of_diagnosis DATE, 
    treatment TEXT, 
    pssn INT NOT NULL, 
    CONSTRAINT emc_key 
    PRIMARY KEY(condition, pssn), 
    FOREIGN KEY(pssn) REFERENCES Patient(ssn));

答案 1 :(得分:0)

尽量避免列名的保留关键字。如果你还想要,你可以写下面。

CREATE TABLE Existing_medical_condition ([condition] VARCHAR(50) NOT NULL,     date_of_diagnosis DATE, treatment TEXT, 
pssn INT NOT NULL, CONSTRAINT emc_key PRIMARY KEY(condition, pssn), FOREIGN KEY(pssn)       REFERENCES Patient(ssn));