¿Cuál es la diferencia entre declarar varias claves externas a la vez o declararlas individualmente?

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...

Método #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

Método #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!

preguntado el 08 de noviembre de 11 a las 14:11

Will all of the orderID columns always be used? -

I've fixed your formatting. We don't use bbcode tags here. Please read this: stackoverflow.com/editing-help -

3 Respuestas

This isn't the same FK.

  • One ensures that the combinación of the 4 columns in any OrderList row exist in a single row row in 'mydb'.'Order'
  • The other ensures that the 4 column values in any OrderList row exist independientemente of the others in some 'mydb'.'Order' fila

respondido 08 nov., 11:18

The difference is that in the first example you have uno FOREIGN KEY constraint (made of four columns), while in your second example you have Digital XNUMXk FOREIGN KEY restricciones

In your first example, the four columns refer to the same column in the Order table, so you'll be able to only store combinations of 4 idéntico order ids. So, it is no more useful than a simple one-column reference.

If you use the second example, you'll be able to store any combination of 4 orders in the OrderList mesa.

respondido 08 nov., 11:18

That is one god awful way of doing an order list. You should have an order list table, and a order list entries table. That way you don't have just 4 items in the list, and only a single index/foreign key from the order list entries table to the orders table.

respondido 08 nov., 11:18

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.