Update foreign key from csv file
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'sparent_id
- find
parent_id'sid_numusing parent table - replace
child_id's foreign key (referred to asparent_id_num, set currently to NULL) with the found parent id'sid_num
mysql csv
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.
add a comment |
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'sparent_id
- find
parent_id'sid_numusing parent table - replace
child_id's foreign key (referred to asparent_id_num, set currently to NULL) with the found parent id'sid_num
mysql csv
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.
add a comment |
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'sparent_id
- find
parent_id'sid_numusing parent table - replace
child_id's foreign key (referred to asparent_id_num, set currently to NULL) with the found parent id'sid_num
mysql csv
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'sparent_id
- find
parent_id'sid_numusing parent table - replace
child_id's foreign key (referred to asparent_id_num, set currently to NULL) with the found parent id'sid_num
mysql csv
mysql csv
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Dec 14 '15 at 8:55
user79772user79772
111
111
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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