2011-06-04 51 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)它转换成的相应数据库表总是可以用复数命名。逻辑是该表将包含该实体的大量实例。因此,订单,检查,学生,汽车