Update foreign key from csv file












4















I am trying to set up a simple database for the first time using MySQL Workbench. I'd like to update the foreign keys of the child table based on the values of parent_child.csv. Here are the current MySQL tables I have right now.



parent table



id_num  parent_id
------------------
33 parent_1
29 parent_2
46 parent_3
17 parent_4
... ...


child table



id_num  child_id  parent_id_num
--------------------------------
22 child_1 NULL
13 child_2 NULL
52 child_3 NULL
76 child_4 NULL
... ... ...


And here is what parent_child.csv looks like:



parent_child.csv



parent_id  child_id
--------------------
parent_1 child_3
parent_1 child_4
parent_2 child_1
... ...


Here is what I'd like the updated child table to be:



child table



id_num  child_id  parent_id_num
--------------------------------
22 child_1 29
13 child_2 NULL
52 child_3 33
76 child_4 33
... ... ...


Assume that not all parent_id and child_id's are in parent_child.csv. So not all child_id's get updated.



How would I go about doing this? Would it require me to create a parent_child table in MySQL?



Here's what I want to do in a nutshell:



for every child_id in parent_child.csv:




  • get child_id's parent_id

  • find parent_id's id_num using parent table

  • replace child_id's foreign key (referred to as parent_id_num, set currently to NULL) with the found parent id's id_num










share|improve this question
















bumped to the homepage by Community 15 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    4















    I am trying to set up a simple database for the first time using MySQL Workbench. I'd like to update the foreign keys of the child table based on the values of parent_child.csv. Here are the current MySQL tables I have right now.



    parent table



    id_num  parent_id
    ------------------
    33 parent_1
    29 parent_2
    46 parent_3
    17 parent_4
    ... ...


    child table



    id_num  child_id  parent_id_num
    --------------------------------
    22 child_1 NULL
    13 child_2 NULL
    52 child_3 NULL
    76 child_4 NULL
    ... ... ...


    And here is what parent_child.csv looks like:



    parent_child.csv



    parent_id  child_id
    --------------------
    parent_1 child_3
    parent_1 child_4
    parent_2 child_1
    ... ...


    Here is what I'd like the updated child table to be:



    child table



    id_num  child_id  parent_id_num
    --------------------------------
    22 child_1 29
    13 child_2 NULL
    52 child_3 33
    76 child_4 33
    ... ... ...


    Assume that not all parent_id and child_id's are in parent_child.csv. So not all child_id's get updated.



    How would I go about doing this? Would it require me to create a parent_child table in MySQL?



    Here's what I want to do in a nutshell:



    for every child_id in parent_child.csv:




    • get child_id's parent_id

    • find parent_id's id_num using parent table

    • replace child_id's foreign key (referred to as parent_id_num, set currently to NULL) with the found parent id's id_num










    share|improve this question
















    bumped to the homepage by Community 15 mins ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      4












      4








      4








      I am trying to set up a simple database for the first time using MySQL Workbench. I'd like to update the foreign keys of the child table based on the values of parent_child.csv. Here are the current MySQL tables I have right now.



      parent table



      id_num  parent_id
      ------------------
      33 parent_1
      29 parent_2
      46 parent_3
      17 parent_4
      ... ...


      child table



      id_num  child_id  parent_id_num
      --------------------------------
      22 child_1 NULL
      13 child_2 NULL
      52 child_3 NULL
      76 child_4 NULL
      ... ... ...


      And here is what parent_child.csv looks like:



      parent_child.csv



      parent_id  child_id
      --------------------
      parent_1 child_3
      parent_1 child_4
      parent_2 child_1
      ... ...


      Here is what I'd like the updated child table to be:



      child table



      id_num  child_id  parent_id_num
      --------------------------------
      22 child_1 29
      13 child_2 NULL
      52 child_3 33
      76 child_4 33
      ... ... ...


      Assume that not all parent_id and child_id's are in parent_child.csv. So not all child_id's get updated.



      How would I go about doing this? Would it require me to create a parent_child table in MySQL?



      Here's what I want to do in a nutshell:



      for every child_id in parent_child.csv:




      • get child_id's parent_id

      • find parent_id's id_num using parent table

      • replace child_id's foreign key (referred to as parent_id_num, set currently to NULL) with the found parent id's id_num










      share|improve this question
















      I am trying to set up a simple database for the first time using MySQL Workbench. I'd like to update the foreign keys of the child table based on the values of parent_child.csv. Here are the current MySQL tables I have right now.



      parent table



      id_num  parent_id
      ------------------
      33 parent_1
      29 parent_2
      46 parent_3
      17 parent_4
      ... ...


      child table



      id_num  child_id  parent_id_num
      --------------------------------
      22 child_1 NULL
      13 child_2 NULL
      52 child_3 NULL
      76 child_4 NULL
      ... ... ...


      And here is what parent_child.csv looks like:



      parent_child.csv



      parent_id  child_id
      --------------------
      parent_1 child_3
      parent_1 child_4
      parent_2 child_1
      ... ...


      Here is what I'd like the updated child table to be:



      child table



      id_num  child_id  parent_id_num
      --------------------------------
      22 child_1 29
      13 child_2 NULL
      52 child_3 33
      76 child_4 33
      ... ... ...


      Assume that not all parent_id and child_id's are in parent_child.csv. So not all child_id's get updated.



      How would I go about doing this? Would it require me to create a parent_child table in MySQL?



      Here's what I want to do in a nutshell:



      for every child_id in parent_child.csv:




      • get child_id's parent_id

      • find parent_id's id_num using parent table

      • replace child_id's foreign key (referred to as parent_id_num, set currently to NULL) with the found parent id's id_num







      mysql csv






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 12 '15 at 18:02









      H. Pauwelyn

      50531129




      50531129










      asked Dec 11 '15 at 23:53









      koreebaykoreebay

      212




      212





      bumped to the homepage by Community 15 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 15 mins ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You need to bring the CSV table into MySQL, then you can:



          update child
          left join parent_child on child.child_id = parent_child.child_id
          left join parent on parent_child.parent_id = parent_id
          set child.parent_id_num = parent.id_num





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "182"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f123496%2fupdate-foreign-key-from-csv-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You need to bring the CSV table into MySQL, then you can:



            update child
            left join parent_child on child.child_id = parent_child.child_id
            left join parent on parent_child.parent_id = parent_id
            set child.parent_id_num = parent.id_num





            share|improve this answer




























              0














              You need to bring the CSV table into MySQL, then you can:



              update child
              left join parent_child on child.child_id = parent_child.child_id
              left join parent on parent_child.parent_id = parent_id
              set child.parent_id_num = parent.id_num





              share|improve this answer


























                0












                0








                0







                You need to bring the CSV table into MySQL, then you can:



                update child
                left join parent_child on child.child_id = parent_child.child_id
                left join parent on parent_child.parent_id = parent_id
                set child.parent_id_num = parent.id_num





                share|improve this answer













                You need to bring the CSV table into MySQL, then you can:



                update child
                left join parent_child on child.child_id = parent_child.child_id
                left join parent on parent_child.parent_id = parent_id
                set child.parent_id_num = parent.id_num






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 14 '15 at 8:55









                user79772user79772

                111




                111






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Database Administrators Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f123496%2fupdate-foreign-key-from-csv-file%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Ronny Ackermann

                    Köttigit

                    MySQL 8.0.15 starts normally but any connection hangs