多个外键?

时间:2008-08-08 20:07:43

标签: sql mysql foreign-keys

我有一张桌子可以跟踪从一个供应商到另一个供应商运送产品的日期和成本。我们(出色地:p)将运输供应商(FedEx,UPS)与产品处理供应商(Think ... Dunder Mifflin)存储在“供应商”表中。所以,我的SHIPPING_DETAILS表中有三列都引用了VENDOR.no。出于某种原因,MySQL不允许我将所有三个定义为外键。有什么想法吗?

CREATE TABLE SHIPPING_GRID(  
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique ID for each row',  
    shipping_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to VENDOR.no for the shipping vendor (vendors_type must be 3)',  
    start_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to VENDOR.no for the vendor being shipped from',  
    end_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to the VENDOR.no for the vendor being shipped to',  
    shipment_duration INT(1) DEFAULT 1 COMMENT 'Duration in whole days shipment will take',  
    price FLOAT(5,5) NOT NULL COMMENT 'Price in US dollars per shipment lbs (down to 5 decimal places)',  
    is_flat_rate TINYINT(1) DEFAULT 0 COMMENT '1 if is flat rate regardless of weight, 0 if price is by lbs',  
    INDEX (shipping_vendor_no),  
    INDEX (start_vendor_no),  
    INDEX (end_vendor_no),  
    FOREIGN KEY (shipping_vendor_no) REFERENCES VENDOR (no),  
    FOREIGN KEY (start_vendor_no) REFERENCES VENDOR (no),  
    FOREIGN KEY (end_vendor_no) REFERENCES VENDOR (no)  
) TYPE = INNODB;

已编辑删除双主键定义...


是的,不幸的是,虽然没有修复它。现在我得到了:

  

无法创建表格   './ 删除我的数据库名称 /SHIPPING_GRID.frm'   (错误:150)

做一个phpinfo()告诉我这个mysql:

  

客户端API版本5.0.45

是的,VENDOR.no是int(6)类型。

3 个答案:

答案 0 :(得分:9)

您定义了主键两次。尝试:

CREATE TABLE SHIPPING_GRID(  
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique ID for each row',  
    shipping_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to VENDOR.no for the shipping vendor (vendors_type must be 3)',  
    start_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to VENDOR.no for the vendor being shipped from',  
    end_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to the VENDOR.no for the vendor being shipped to',  
    shipment_duration INT(1) DEFAULT 1 COMMENT 'Duration in whole days shipment will take',  
    price FLOAT(5,5) NOT NULL COMMENT 'Price in US dollars per shipment lbs (down to 5 decimal places)',  
    is_flat_rate TINYINT(1) DEFAULT 0 COMMENT '1 if is flat rate regardless of weight, 0 if price is by lbs',  
    INDEX (shipping_vendor_no),  
    INDEX (start_vendor_no),  
    INDEX (end_vendor_no),  
    FOREIGN KEY (shipping_vendor_no) REFERENCES VENDOR (no),  
    FOREIGN KEY (start_vendor_no) REFERENCES VENDOR (no),  
    FOREIGN KEY (end_vendor_no) REFERENCES VENDOR (no)  
) TYPE = INNODB;

VENDOR主键必须是INT(6),并且两个表必须是InnoDB类型。

答案 1 :(得分:0)

我在这里运行了代码,错误消息显示(并且它是正确的!)您将 id 字段设置为主键两次。

答案 2 :(得分:0)

  

你能提供的定义   供应商表

我明白了。 VENDOR表是MyISAM ...(编辑你的答案,告诉我让他们都是INNODB;)

(任何原因只是将VENDOR类型切换到INNODB?)