UPDATE color_library lib
   JOIN template_color_library tpl ON
   lib.id=tpl.color_library_id
SET lib.shared_code = (SELECT IFNULL(tpl.code, (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 60))))
WHERE lib.shared_code IS NULL;

UPDATE font_library lib
   JOIN template_font_library tpl ON
   lib.id=tpl.font_library_id
SET lib.shared_code = (SELECT IFNULL(tpl.code, (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 60))))
WHERE lib.shared_code IS NULL;

UPDATE image_library lib
   JOIN template_image_library tpl ON
   lib.id=tpl.image_library_id
SET lib.shared_code = (SELECT IFNULL(tpl.code, (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 60))))
WHERE lib.shared_code IS NULL;

UPDATE text_library lib
   JOIN template_text_library tpl ON
   lib.id=tpl.text_library_id
SET lib.shared_code = (SELECT IFNULL(tpl.code, (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 60))))
WHERE lib.shared_code IS NULL;

UPDATE item_library lib
   JOIN template_item_library tpl ON
   lib.id=tpl.item_library_id
SET lib.shared_code = (SELECT IFNULL(tpl.code, (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 60))))
WHERE lib.shared_code IS NULL;

UPDATE external_item_library lib
   JOIN template_external_item_library tpl ON
   lib.id=tpl.external_item_library_id
SET lib.shared_code = (SELECT IFNULL(tpl.code, (SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 60))))
WHERE lib.shared_code IS NULL;