create table survey_block_sections ( block_section_id integer primary key, section_id integer constraint survey_b_s_section_id_fk references survey_sections, answer_description varchar(400) ); insert into survey_block_sections(block_section_id, section_id, answer_description) select distinct block_section_id, section_id, '' from survey_block_questions bq where not exists (select block_section_id from survey_block_sections bs where bs.block_section_id=bq.block_section_id); alter table survey_block_questions add constraint survey_block_sec_fk foreign key (block_section_id) references survey_block_sections; alter table survey_block_questions drop(section_id);