简体   繁体   中英

how to get one record in a table into two records of a separate table

According to the old database, I have a table called guardiant, it has a foreign key called db_id (which is from the student table). And the table structure is like this:

CREATE TABLE IF NOT EXISTS `guardiant` (
  `db_id` varchar(100) NOT NULL,
  `fg_name` varchar(100) DEFAULT NULL,
  `fg_address` varchar(100) DEFAULT NULL,
  `fg_tel_work` varchar(100) DEFAULT NULL,
  `fg_mobile` varchar(100) DEFAULT NULL,
  `fg_email` varchar(100) DEFAULT NULL,
  `fg_prof` varchar(100) DEFAULT NULL,
  `mg_name` varchar(100) DEFAULT NULL,
  `mg_address` varchar(100) DEFAULT NULL,
  `mg_tel_work` varchar(100) DEFAULT NULL,
  `mg_mobile` varchar(100) DEFAULT NULL,
  `mg_email` varchar(100) DEFAULT NULL,
  `mg_prof` varchar(100) DEFAULT NULL,
  `by` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`db_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

This table contains both records of mother and father (father being fg_ and mother being mg_). I need to migrate the data into another table which can only contain either a mother or a father. The structure is similar to this:

CREATE TABLE IF NOT EXISTS `fsms_parent_guardian` (
  `parent_guardian_id` varchar(20) NOT NULL,
  `address_1` varchar(200) default NULL,
  `address_2` varchar(200) default NULL,
  `date_of_birth` date default NULL,
  `description` varchar(250) default NULL,
  `education` varchar(100) default NULL,
  `email` varchar(100) default NULL,
  `first_name` varchar(100) NOT NULL,
  `last_name` varchar(100) NOT NULL,
  `middle_name` varchar(100) default NULL,
  `modified_by` varchar(20) NOT NULL,
  `modified_date` date NOT NULL,
  `nationality` varchar(20) default NULL,
  `profession` varchar(50) default NULL,
 PRIMARY KEY  (`parent_guardian_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

So one record in the old table should go into two records of the new table with separate ids. Also primary keys from fsms_parent_guardian table and the student table should be mapped into a separated table. Please tell me a feasible way to solve this.

to copy the records, you will need tow querys, the first one selects fg records and inserts them in the new table, the second one selects the mg records and insert them again. Each record of fg and mg needs to be mapped to the student table with a foreign key (ID_student).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM