2013-08-28 61 views
0

我想創建一個具有多個外鍵的表,並且出現錯誤。我不確定我做錯了什麼,我一直在修補它。我敢肯定,它的東西小而笨,但幫助表示讚賞。MYSQL與多個外鍵創建表錯誤

Create table Order(
order_number INT AUTO_INCREMENT, 
product_id INT(11) NOT NULL, 
address_id INT(11) NOT NULL, 
user_id INT(11) NOT NULL, 
quantity INT(3) NOT NULL, 
size VARCHAR(20) NOT NULL, 
status VARCHAR(20) NOT NULL, 
order_date DATE NOT NULL, 
PRIMARY KEY (order_number), 
FOREIGN KEY (user_id) 
REFERENCES User (user_id), 
FOREIGN KEY (product_id) 
REFERENCES Product (product_id), 
FOREIGN KEY (address_id) 
REFERENCES Address (address_id) 
)ENGINE = INNODB 

特定的錯誤我得到的是:

#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 'Order(

ORDER_NUMBER INT AUTO_INCREMENT, 的product_id INT(11)NOT NULL, 地址_」在行1

+2

'ORDER'這是你指定了你的表名是保留關鍵字。這是一個保留關鍵字的完整列表:[MySQL保留關鍵字列表](http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html) –

回答

0

你不能只用order,因爲它也是一個關鍵字;放在引號:

Create table `Order`(
order_number INT AUTO_INCREMENT, 
product_id INT(11) NOT NULL, 
address_id INT(11) NOT NULL, 
user_id INT(11) NOT NULL, 
quantity INT(3) NOT NULL, 
size VARCHAR(20) NOT NULL, 
status VARCHAR(20) NOT NULL, 
order_date DATE NOT NULL, 
PRIMARY KEY (order_number), 
FOREIGN KEY (user_id) 
REFERENCES User (user_id), 
FOREIGN KEY (product_id) 
REFERENCES Product (product_id), 
FOREIGN KEY (address_id) 
REFERENCES Address (address_id) 
)ENGINE = INNODB 
+0

這是如此簡單和愚蠢,謝謝。我可能會改變它的名字,以防止我不得不在整個地方使用引號。 –

+0

這不會是一個壞主意;儘管「全部使用引用」本身也不是一個壞主意。 – vollie

+0

刻度標記讓我瘋狂編碼MYSQL –