0

I have my DB structured like this with people.person_id as the foreign key for each row in people_meta.

people table

 ╔═════╦════════╦═══════╗
 ║ id ║ fname ║ lname ║
 ╠═════╬════════╬═══════╣
 ║ 0 ║ John ║ Doe ║
 ║ 1 ║ John ║ Doe ║
 ║ 2 ║ Bob ║ Smith ║
 ║ 3 ║ Dave ║ Jones ║
 ║ 4 ║ Bob ║ Smith ║
 ║ 5 ║ Jen ║ Smith ║
 ╚═════╩════════╩═══════╝

people_meta table

 ╔═════╦════════════╦════════╦═══════╗
 ║ id ║ person_id ║ key ║ value ║
 ╠═════╬════════════╬════════╬═══════╣
 ║ 0 ║ 0 ║ state ║ NY ║
 ║ 1 ║ 0 ║ age ║ 53 ║
 ║ 2 ║ 1 ║ party ║ D ║
 ║ 3 ║ 1 ║ age ║ 53 ║
 ║ 4 ║ 2 ║ state ║ CA ║
 ║ 5 ║ 4 ║ party ║ R ║
 ╚═════╩════════════╩════════╩═══════╝

What I want to do is combine the duplicate people rows and have it cascade to the people_meta table somehow. Please note that some data in people_meta would be duplicate if they were combined.

The first thing I tried was setting ON DELETE NO ACTION and ON UPDATE CASCADE, then in phpMyAdmin I manually attempted to delete the duplicate parent with the lower ID and update the second duplicate's ID to what the first one was, thinking it would then cascade and give me the desired affect. -FAIL- Apparently using InnoDB's ON UPDATE / ON DELETE features won't be helpful for such a task, but at least I've learned more about their purpose.

I'm sort of a newbie for SQL so I'd appreciate any recommendations.

asked Oct 13, 2014 at 15:11

1 Answer 1

0

You'll need to change the people_meta.person_id values before deleting the row from the people table, because the engine wouldn't know how to replace the values with the duplicates.

update people_meta set person_id = 1 where person_id = 0;
delete from people where id = 0;
answered Oct 13, 2014 at 18:02
1
  • That makes sense. I just made a PHP script that runs a similar update/delete sql combo from my input to assist in completing my task. Commented Oct 14, 2014 at 15:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.