Moving fields from one table to another
Posted by mholmes on 28 Feb 2011 in Activity log
Two of the outstanding requests involve moving records from one table to another:
- In the ‘Dissent types’ table, could item #14 (“Communication vocab: Bruits publics, murmures, etc.”) be moved to the ‘News/info’ table?
- Also in the ‘News/info’ table: can two fields be moved to the “Concerns of authorities” table? They seem misplaced. The fields are: “Sedition: how to counteract seditious words”, and “Importance of communicating to peasants”
I'm just going to map out this process before undertaking it, working with the first one above. There are 19 records in the documents table which link to this record in dissentTypes (as part of a one-to-many relationship). I think these would be the steps we need to take:
- Record which records have a link to this field.
SELECT `dtd_doc_id_fk` FROM `docs_to_disTypes` WHERE `dtd_disType_id_fk` = "14" ORDER BY `dtd_doc_id_fk`;
- Create the parallel field in the News table, and get its id.
INSERT INTO `news` (`nw_desc`) VALUES ((SELECT `dt_name` FROM `disTypes` WHERE `dt_id` = "14")); SELECT `nw_id` FROM `news` WHERE `nw_desc` = (SELECT `dt_name` FROM `disTypes` WHERE `dt_id` = "14");
(New id was 20.) - For each of the entries in the docs_to_disTypes table, create a parallel entry in the docs_to_news table.
INSERT INTO `docs_to_news` (`dtn_doc_id_fk`, `dtn_news_id_fk`) (SELECT `dtd_doc_id_fk`, "20" FROM `docs_to_disTypes` WHERE `docs_to_disTypes`.`dtd_disType_id_fk` = "14");
- Delete the original field from the disTypes table. This should cascade to remove the entries in docs_to_disTypes automatically.
DELETE FROM `disTypes` WHERE `dt_id` = "14";
- Check that those 19 document records have lost the old entries and acquired the new.
Tested on dev, implemented on live site.