2013-04-11 123 views
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) 
); 

給出錯誤:語法有什麼問題?

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 'condition VARCHAR(50) NOT NULL, date_of_diagnosis DATE, treatment TEXT, pssn INT' at line 1

+0

如果以下任何答案有幫助,請考慮接受他們作爲答案。 – Kermit 2013-04-23 00:45:10

回答

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

只爲處理名稱投票。 – Jordan 2013-04-11 17:56:25

+1

我以爲是[條件],但我會遠離保留字。 – JBrooks 2013-04-11 17:57:50

+0

@JBrooks方括號用於MSSQL。 – Kermit 2013-04-11 17:58:11

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