2011-06-04 42 views
1

我有3個表格創建連接表的問題!

客戶,菜單和訂單。

訂單表假定加入客戶和菜單表,幷包含兩者的主鍵。以下是我嘗試在phpmyadmin上創建訂單表的方法。

create table order(
customerID int not null, 
itemID int not null, 
primary key (customerID, itemID), 
foreign key(customerID) reference customer(ID), 
foreign key(itemID) reference menu(itemID) 
) 

這是行不通的。我究竟做錯了什麼?!!

+0

你可以繼續發佈所有三個表的DDL嗎? – 2011-06-04 01:28:10

回答

4

order是一個保留字,換個名稱,或引用它,就像

create table `order`( 
    customerID int not null, 
    itemID int not null, 
    primary key (customerID, itemID), 
    foreign key(customerID) reference customer(ID), 
    foreign key(itemID) reference menu(itemID)) 
+1

如果這是我的意見,我想建議使用非保留名稱。 MySQL爲此提供了一個引用解決方法,但並非每個SQL方言都有。 – SingleNegationElimination 2011-06-04 01:43:16

+0

非常感謝你 - 那就是訣竅。我用'購買'來代替。另外,參考應該是'參考'。再次,非常感謝! – Kira 2011-06-04 21:30:42

0

據抱怨爲order是保留關鍵字。用@TokenMacGuy告訴你用反引號包裝它,以解決你的問題。 Here is a list of them

此外,作爲一般規則,你可以改變你的實體,像這樣以避免出現問題,特別是與保留關鍵字: -

一)實體總是模仿(在紙上)爲單數,因爲它代表了概念/資產/人在現實世界或問題領域。例如。訂單,檢查,學生,汽車

b)它轉換成的相應數據庫表總是可以用複數命名。邏輯是該表將包含該實體的大量實例。因此,訂單,檢查,學生,汽車