I've got a table that is supposed to track days and costs for shipping product from one vendor to another. We (brilliantly :p) stored both the shipping vendors (FedEx, UPS) with the product handling vendors (Think... Dunder Mifflin) in a "VENDOR" table. So, I have three columns in my SHIPPING_DETAILS table that all reference VENDOR.no. For some reason MySQL isn't letting me define all three as foreign keys. Any ideas?
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;
Edited to remove double primary key definition...
Yeah, unfortunately that didn't fix it though. Now I'm getting:
Can't create table './REMOVED MY DB NAME/SHIPPING_GRID.frm' (errno: 150)
Doing a phpinfo() tells me this for mysql:
Client API version 5.0.45
Yes, the VENDOR.no is type int(6).
A table may have multiple foreign keys, and each foreign key can have a different parent table. Each foreign key is enforced independently by the database system. Therefore, cascading relationships between tables can be established using foreign keys.
Rules for creating foreign keys A foreign key column cannot be a virtual computed column, but it can be a stored computed column. A single column can have multiple foreign key constraints.
A table with a foreign key reference to itself is still limited to 253 foreign key references. Greater than 253 foreign key references are not currently available for columnstore indexes, memory-optimized tables, Stretch Database, or partitioned foreign key tables. Stretch Database is deprecated in SQL Server 2022 (16.
You can use the FOREIGN KEY REFERENCES constraint to implement a foreign key relationship in SQL Server. Specify the table name. Then specify in parenthesis the column name for the foreign key to reference it.
You defined the primary key twice. Try:
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;
The VENDOR primary key must be INT(6), and both tables must be of type InnoDB.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With