I need to copy data within a tab that has hierarchical relationships and maintain the relation within the new data.
I have a table “site_content”, the table has thousands of rows and the data in the table is quite extent. Here are the reduced table structure and column data for an example.
CREATE TABLE `site_content` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`site_id` int(11) DEFAULT NULL,
`parent` int(11) unsigned DEFAULT NULL,
`parent_orig_id` int(11) unsigned DEFAULT NULL,
`content_text` text,
PRIMARY KEY (`id`));
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(1, 76, 0, NULL, '');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(2, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(3, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(4, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(5, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(6, 76, 1, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(7, 76, 2, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(8, 76, 3, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(9, 76, 3, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(11, 76, 9, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(12, 76, 9, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(13, 76, 9, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(14, 76, 4, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(15, 76, 4, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(28, 76, 4, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(29, 76, 4, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(30, 76, 5, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(32, 76, 5, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(36, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(41, 76, 32, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(42, 76, 32, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(43, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(44, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(45, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(46, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(47, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(48, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(49, 76, 41, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(50, 76, 42, NULL, 'some content');
INSERT INTO `site_content` (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`) VALUES(51, 76, 42, NULL, 'some content');
I need to copy the “site 76” rows within the table, so I use this query to copy the table and also to give me the original parent child relationship information in the ‘parent_orig_id’ column.
INSERT INTO site_content (`id`, `site_id`, `parent`, `parent_orig_id`, `content_text`)
SELECT NULL, "200", `parent`, `id`, `content_text` FROM site_content
WHERE `site_id` = '76';
I now have the data for site “76” copied in the table as site “200”
How can I update the new site 200 rows to have the same relationship as the site 76 rows? For example row “1” is the parent of rows “2, 3, 4, 5, 6”.
Row “2” for site 200 is now row id “53” and thinks that its parent is still row “1” based off of the “parent” column from the copy. How can I update the column “parent” in the copied rows to now reflect the new relationship as it is site 200. The “parent_orig_id” column hold the row id for the original hierarchical relationship.
I can see that if I do the query what I need to update. I would like to do this in SQL if possible.
select a.id current_row, a.parent as current_parent, b.id AS new_parent
from site_content a, site_content b
where b.parent_orig_id = a.parent AND a.site_id = '200' AND b.site_id = '200'
You need to maintain not only the
parent_orig_id, but also theorig_id. Since you’ve gone half the distance using the strategy of having columns in your table for this, I’ll continue in that direction. Like this: