回答

收藏

多个外键?

技术问答 技术问答 253 人阅读 | 0 人回复 | 2023-09-14

我有一张桌子,该桌子应该可以追踪从一家供应商向另一家供应商运送产品的天数和成本。我们(出色地:p)在产品供应商表中存储了运输供应商(FedEx,UPS)和产品处理供应商(Think9 n- T8 U+ {) f0 @" @4 V; H# q! r
… Dunder" x5 n1 `; b( y( u
Mifflin)。因此,我的SHIPPING_DETAILS表中有三列,均引用VENDOR.no。由于某种原因,MySQL不允许我将全部三个都定义为外键。有任何想法吗?8 S/ Q; O% ?" ~/ x* a
CREATE TABLE SHIPPING_GRID(  6 R$ G. d* ?2 l& ?+ j/ G* q- }
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique ID for each row',  
' [' E/ t' A' z  k' b( j8 b; b    shipping_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to VENDOR.no for the shipping vendor (vendors_type must be 3)',  
9 v- T& g% h/ J! A! f    start_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to VENDOR.no for the vendor being shipped from',  
& L# X% D( O  E) R1 E6 \) B    end_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to the VENDOR.no for the vendor being shipped to',  
& `  j  c; {( g# T6 h    shipment_duration INT(1) DEFAULT 1 COMMENT 'Duration in whole days shipment will take',  
4 r. y! w  y; V0 ^( j    price FLOAT(5,5) NOT NULL COMMENT 'Price in US dollars per shipment lbs (down to 5 decimal places)',  1 |/ o& B9 m1 [! n! D* J
    is_flat_rate TINYINT(1) DEFAULT 0 COMMENT '1 if is flat rate regardless of weight, 0 if price is by lbs',  7 R9 u9 N! H5 b' j9 t* h
    INDEX (shipping_vendor_no),  
# ^* s$ v1 n2 J; f    INDEX (start_vendor_no),  7 ?) \1 I) Y+ @! x
    INDEX (end_vendor_no),  
, [' C" @! t9 \+ }1 q4 O1 c! g* b    FOREIGN KEY (shipping_vendor_no) REFERENCES VENDOR (no),  
; ^# e/ ^. v! R    FOREIGN KEY (start_vendor_no) REFERENCES VENDOR (no),  
4 n& `. ]% ]8 I- X, T/ \    FOREIGN KEY (end_vendor_no) REFERENCES VENDOR (no)  % f# ~+ ~+ c! H' d! _
) TYPE = INNODB;
* Y( U" h6 Q# }编辑删除双主键定义…
) C5 m( [& X0 a* n) B6 k5 [( [% n$ A. y8 R# K. A8 T0 w, A
是的,不幸的是,这并没有解决。现在我得到:! o/ |7 @. n! B# {, \: |

7 G  t' o. J3 ?无法创建表“ ./ 删除我的数据库名称 /SHIPPING_GRID.frm”(错误号:150)
0 @) n- |& Z( W' M: T! c
% u! Q! M1 N4 W$ O9 r; V, Y
做一个phpinfo()会告诉我mysql:
2 o9 X- b5 N: @& D5 f( W1 K. P: B6 d# q+ j/ I
客户端API版本5.0.45
: L/ r8 M7 x: j0 e( M
( i. \* F* E8 N% U" g, S3 c
是的,VENDOR.no是int(6)类型。# o8 y; ]7 \0 B+ b! i
               
8 ~4 P& x/ Y$ S4 P" o( `. B! a1 ~0 ~解决方案:
4 G8 C9 ~- j: {7 M               
* X) [0 i4 e# Y; N* W4 \! a# I+ V$ g& I# W6 h# C$ H' [

4 h/ J; M8 `, N' i4 J8 w                您定义了两次主键。尝试:
3 g! T+ S; y4 x& G1 x8 [CREATE TABLE SHIPPING_GRID(  
0 j' F  v. T& }1 ]( U( F4 T. T: q    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique ID for each row',  7 O& S+ B, Z6 d3 _& N
    shipping_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to VENDOR.no for the shipping vendor (vendors_type must be 3)',  ( @* U1 M1 x/ Z; R: `4 w# i/ }
    start_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to VENDOR.no for the vendor being shipped from',  7 R/ {7 ~! y+ U
    end_vendor_no INT(6) NOT NULL COMMENT 'Foreign key to the VENDOR.no for the vendor being shipped to',  , f1 |8 Q: |) G& |% W+ M) j* s' M
    shipment_duration INT(1) DEFAULT 1 COMMENT 'Duration in whole days shipment will take',  # j# n2 l. O& B3 U
    price FLOAT(5,5) NOT NULL COMMENT 'Price in US dollars per shipment lbs (down to 5 decimal places)',  
. h. H5 g' s* g  D- u    is_flat_rate TINYINT(1) DEFAULT 0 COMMENT '1 if is flat rate regardless of weight, 0 if price is by lbs',  4 C6 k  e2 M4 i* D- T' R4 ~
    INDEX (shipping_vendor_no),  
+ O9 Y  w* t7 \3 f0 D    INDEX (start_vendor_no),  0 q3 u  M: U! m' t, Z
    INDEX (end_vendor_no),  + p, s. [" y3 t1 T  Q9 \
    FOREIGN KEY (shipping_vendor_no) REFERENCES VENDOR (no),  
2 I/ n: p9 F% c! ?- d3 m# K5 T    FOREIGN KEY (start_vendor_no) REFERENCES VENDOR (no),  ( |; A% D$ A  l3 _9 _3 g0 l
    FOREIGN KEY (end_vendor_no) REFERENCES VENDOR (no)  - H$ F3 P, y& \4 o
) TYPE = INNODB;
0 e. X+ G" U# B# J0 g! VVENDOR主键必须是INT(6),并且两个表都必须是InnoDB类型。
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则