Plusieurs clés étrangères ?

J'ai un tableau censé suivre les jours et les coûts d'expédition des produits d'un fournisseur à un autre. Nous avons (brillamment :p) stocké les fournisseurs d'expédition (FedEx, UPS) avec les fournisseurs de manutention de produits (Think... Dunder Mifflin) dans une table 'VENDOR'. J'ai donc trois colonnes dans ma table SHIPPING_DETAILS qui font toutes référence à VENDOR.no. Pour une raison quelconque, MySQL ne me laisse pas définir les trois comme des clés étrangères. Des idées ?

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;

Modifié pour supprimer la double définition de clé primaire...

Ouais, malheureusement, cela n'a pas résolu le problème. Maintenant j'obtiens :

Impossible de créer le tableau './SUPPRIMÉ MON NOM BD/SHIPPING_GRID.frm' (errno : 150)

Faire un phpinfo() me dit ceci pour mysql :

API client 5.0.45

Oui, le VENDOR.no est de type int(6).

请先 登录 后评论

3 réponses

Christian Lescuyer

Vous avez défini la clé primaire deux fois. Essayez :

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;

La clé primaire VENDOR doit être INT(6) et les deux tables doivent être de type InnoDB.

请先 登录 后评论
Mario Marinato

J'ai exécuté le code ici, et le message d'erreur a montré (et c'est vrai !) que vous définissez le champ id deux fois comme clé primaire.

请先 登录 后评论
cmcculloh

Pouvez-vous fournir la définition du Tableau VENDEUR

J'ai compris. La table VENDOR était MyISAM... (modifié votre réponse pour me dire de les faire tous les deux INNODB ;) )

(une raison de ne pas simplement changer le type VENDOR en INNODB ?)

请先 登录 后评论