-- mysql alter table content_collection add column item_id bigint unsigned NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (item_id), add unique index cc_collection_id ( collection_id ), add column parent_id bigint unsigned; update content_collection t1 join content_collection t2 on t1.in_collection=t2.collection_id set t1.parent_id = t2.item_id; alter table content_resource add column item_id bigint unsigned NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (item_id), add unique index cr_resource_id ( resource_id ), add column parent_id bigint unsigned; update content_resource t1 join content_collection t2 on t1.in_collection=t2.collection_id set t1.parent_id = t2.item_id; create trigger cc_parent_id_trig before insert on content_collection for each row set new.parent_id = (select t1.item_id from content_collection as t1 where NEW.in_collection = t1.collection_id); create trigger cr_parent_id_trig before insert on content_resource for each row set new.parent_id = (select t1.item_id from content_collection as t1 where NEW.in_collection = t1.collection_id); -- select t1.item_id,t2.item_id, t1.parent_id, t1.collection_id, t2.collection_id from content_collection t1 join content_collection t2 on t1.in_collection=t2.collection_id; -- select t1.item_id,t2.item_id, t1.parent_id, t1.resource_id, t2.collection_id from content_resource t1 join content_collection t2 on t1.in_collection=t2.collection_id;