locked
INSERT to table rows with the same key value RRS feed

  • Question

  • User-2111854606 posted

    I have an Orders table and OrderLines table.

    For each order I have a couple of lines which suppose to be connected by having the same OrderID in both tables.

    When I am trying to insert the data, it's says that it's cannot be because it has the same foreign key on couple of rows.

    How I can do it ?

    Thanks in Advance.

    Monday, May 18, 2015 5:47 PM

Answers

  • User-2010311731 posted

    Any time you insert to a table, all of the "key" fields (weather primary or foreign) need to be unique.  You can't do two or more inserts with the same foreign key combination. 

    You will need to post some table definitions and code samples in order to give you any specific advice.

    Matt

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2015 5:59 PM
  • User61956409 posted

    Hi rubik951,

    Thanks for your post.

    You could refer to below sample to design your tables.

    Orders table:

    OrderLines table:

    We could add a record with the same “OrderID” into [OrderLines].

    If insert a record which  “OrderID” is not in [Orders] into [OrderLines], we will get the below error.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 19, 2015 2:35 AM

All replies

  • User-2010311731 posted

    Any time you insert to a table, all of the "key" fields (weather primary or foreign) need to be unique.  You can't do two or more inserts with the same foreign key combination. 

    You will need to post some table definitions and code samples in order to give you any specific advice.

    Matt

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2015 5:59 PM
  • User61956409 posted

    Hi rubik951,

    Thanks for your post.

    You could refer to below sample to design your tables.

    Orders table:

    OrderLines table:

    We could add a record with the same “OrderID” into [OrderLines].

    If insert a record which  “OrderID” is not in [Orders] into [OrderLines], we will get the below error.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 19, 2015 2:35 AM