18

我有一张表格,用来记录从一个供应商向另一个供应商运送产品的天数和成本。我们(出色地: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) 类型。

4

3 回答 3

13

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

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 类型。

于 2008-08-08T20:18:25.553 回答
1

能否提供 VENDOR 表的定义

我想到了。VENDOR 表是 MyISAM ...(编辑了您的答案,告诉我让它们都成为 INNODB ;))

(有什么理由将 VENDOR 类型切换到 INNODB 吗?)

于 2008-08-08T20:44:23.597 回答
0

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

于 2008-08-08T20:18:58.173 回答