I have a table called Order_List that contains a list of orders. Each column contains a foreign key that references the primary key of an order in the order table. My question is, what is the best way to declare these foreign keys. Using MySQL Workbench I found two ways…
Method #1
CREATE TABLE IF NOT EXISTS 'mydb'.'Order_List' (
'idOrder_List' INT UNSIGNED NOT NULL AUTO_INCREMENT ,
'orderID01' INT UNSIGNED NULL ,
'orderID02' INT UNSIGNED NULL ,
'orderID03' INT UNSIGNED NULL ,
'orderID04' INT UNSIGNED NULL ,
PRIMARY KEY ('idOrder_List') ,
INDEX 'fk_Order_List_1' ('orderID01' ASC, 'orderID02' ASC, 'orderID03' ASC, 'orderID04' ASC) ,
CONSTRAINT 'fk_Order_List_1'
FOREIGN KEY ('orderID01' , 'orderID02' , 'orderID03' , 'orderID04' )
REFERENCES 'mydb'.'Order' ('idOrder' , 'idOrder' , 'idOrder' , 'idOrder' )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
Method #2
CREATE TABLE IF NOT EXISTS 'mydb'.'Order_List' (
'idOrder_List' INT UNSIGNED NOT NULL AUTO_INCREMENT ,
'orderID01' INT UNSIGNED NULL ,
'orderID02' INT UNSIGNED NULL ,
'orderID03' INT UNSIGNED NULL ,
'orderID04' INT UNSIGNED NULL ,
PRIMARY KEY ('idOrder_List') ,
INDEX 'fk_Order_List_1' ('orderID01' ASC) ,
INDEX 'fk_Order_List_2' ('orderID02' ASC) ,
INDEX 'fk_Order_List_3' ('orderID03' ASC) ,
INDEX 'fk_Order_List_4' ('orderID04' ASC) ,
CONSTRAINT 'fk_Order_List_1'
FOREIGN KEY ('orderID01' )
REFERENCES 'mydb'.'Order' ('idOrder' )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT 'fk_Order_List_2'
FOREIGN KEY ('orderID02' )
REFERENCES 'mydb'.'Order' ('idOrder' )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT 'fk_Order_List_3'
FOREIGN KEY ('orderID03' )
REFERENCES 'mydb'.'Order' ('idOrder' )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT 'fk_Order_List_4'
FOREIGN KEY ('orderID04' )
REFERENCES 'mydb'.'Order' ('idOrder' )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
The first method combines the declaration. The second method splits it up. Which one is better? Are there any inherent weaknesses to one over the other? Thanks!
This isn’t the same FK.
'mydb'.'Order''mydb'.'Order'row