2008-08-08 86 views
13

我有一张表,应该跟踪天和产品从一个供应商运送到另一个的成本。我们(出色地说:p)在“供应商”表中将产品处理供应商(Think ... Dunder Mifflin)与运输供应商(FedEx,UPS)存储在一起。所以,我的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; 

修改,以去除双主键定义...


是啊,可惜就是没有解决它虽然。现在我越来越:

无法创建表 './ 打消了我的DB名称 /SHIPPING_GRID.frm' (错误:150)

做一个phpinfo()函数告诉我这个对MySQL:

客户端API版本5.0.45

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

+0

事实上,错误? 150引用[外键约束](http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html)错误。你能提供VENDOR表的定义吗?供应商的PK是INT(6)还是INT?从上面的页面: - *整数类型的大小和符号必须相同。* – 2008-08-08 20:35:00

回答

8

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

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

0

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

0

你能提供 卖方表

我理解了它的定义。供应商表是MyISAM的...(编辑你的答案告诉我,使他们既INNODB;))

(任何原因只是开关供应商类型超过InnoDB的)