{"id":181378,"date":"2013-05-30T11:24:03","date_gmt":"2013-05-30T07:24:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=181378"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=181378","title":{"rendered":"<span class=\"post_title\">\u0421\u043b\u0438\u044f\u043d\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432 \u0432 Oracle<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0412\u0441\u0435\u0433\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043d\u0435\u0439 \u043d\u0430\u0437\u0430\u0434 \u044f <a href=\"http:\/\/habrahabr.ru\/post\/179789\/\">\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b<\/a> \u043d\u0430\u0431\u043e\u0440 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440, \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0449\u0438\u0445 \u0431\u043e\u0440\u043e\u0442\u044c\u0441\u044f \u0441 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u043c\u0438 \u0432 \u0411\u0414 PostgreSQL. \u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u043f\u043e\u0434 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u043c\u0438 \u044f \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0437\u0430\u043f\u0438\u0441\u0438 \u0432\u043d\u0435\u0441\u0435\u043d\u043d\u044b\u0435 \u0432 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043e\u0448\u0438\u0431\u043a\u0435. \u041a\u0430\u043a \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u0434\u043b\u044f Oracle \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u0435\u043d.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u00ab\u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u044b\u0435\u00bb \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u043e\u0442\u043b\u0430\u0434\u043a\u0438 \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/2.bp.blogspot.com\/-8i21G-_rfaY\/Uabqf2qE99I\/AAAAAAAAAVQ\/vF6PH-gxvNw\/s1600\/0002.PNG\" alt=\"image\"\/><\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0422\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">create table city (   id          number                            not null,   name        varchar2(30)                      not null );  create unique index city_pk on city(id);  alter table city   add constraint city_pk primary key(id);   create table street (   id          number                            not null,   city_id     number                            not null,   name        varchar2(30)                      not null );  create unique index street_pk on street(id);  create index street_fk on street(city_id);  alter table street   add constraint street_pk primary key(id);   alter table street add   constraint fk_street foreign key (city_id)      references city(id);  create table address (   id          number                            not null,   street_id   number                            not null,   house       varchar2(10)                      not null,   apartment  varchar2(10) );  create unique index address_pk on address(id);  create index address_fk on address(street_id);  alter table address   add constraint address_pk primary key(id);   alter table address add   constraint fk_address foreign key (street_id)      references street(id); <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u2026 \u0438 \u043d\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u043c\u0438:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0422\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">insert into city(id, name) values (1, '\u041a\u0430\u0437\u0430\u043d\u044c');  insert into street(id, city_id, name) values (1, 1, '\u041f\u043e\u0431\u0435\u0434\u044b'); insert into street(id, city_id, name) values (2, 1, '\u041f\u043e\u0431\u0435\u0434\u044b \u043f\u0440\u043e\u0441\u043f\u0435\u043a\u0442');  insert into address(id, street_id, house, apartment) values (1, 1, '10', '1'); insert into address(id, street_id, house, apartment) values (2, 2, '10', '2'); <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c, \u043a\u0430\u043a\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u00ab\u0441\u043b\u0438\u0442\u044c\u00bb \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b? \u0423 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u00ab\u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u043c\u0438\u00bb. \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u0438\u0437 \u043d\u0438\u0445, \u0430 \u0432\u0441\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043d\u0435\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u0432\u0442\u043e\u0440\u0443\u044e. \u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0432\u0441\u0435 \u044d\u0442\u043e \u043d\u0430\u0434\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e, \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043e\u0442\u043a\u0430\u0442\u0438\u0442\u044c. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0443\u044e\u0441\u044f \u0434\u043b\u044f \u043e\u0442\u043a\u0430\u0442\u0430, \u0432 \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445:<\/p>\n<p>  <img decoding=\"async\" src=\"http:\/\/3.bp.blogspot.com\/-NBLv--7RuBM\/UaXejlzgjWI\/AAAAAAAAAU0\/rIvjqZK5XDE\/s1600\/0001.PNG\" alt=\"image\"\/><\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043b\u0443\u0436\u0435\u0431\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">create table mg_table (   name        varchar2(30)                      not null );  create unique index mg_table_pk on mg_table(name);  alter table mg_table   add constraint mg_table_pk primary key(name);   create sequence mg_merge_seq;  create table mg_merge (   id          number                            not null,   table_name  varchar2(30)                      not null,   old_id      number                            not null,   new_id      number                            not null );  create unique index mg_merge_pk on mg_merge(id);  create unique index mg_merge_uk on mg_merge(table_name, old_id);  alter table mg_merge   add constraint mg_merge_pk primary key(id);   alter table mg_merge add   constraint fk_mg_merge foreign key (table_name)      references mg_table(name);  create sequence mg_ref_seq;  create table mg_ref (   id          number                            not null,   merge_id    number                            not null,   table_name  varchar2(30)                      not null,   pk_name     varchar2(30)                      not null,   column_name varchar2(30)                      not null,   object_id   number                            not null );  create unique index mg_ref_pk on mg_ref(id);  create index mg_ref_fk on mg_ref(merge_id);  alter table mg_ref   add constraint mg_ref_pk primary key(id);   alter table mg_ref add   constraint fk_mg_ref foreign key (merge_id)      references mg_merge(id); <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0414\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438, \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439. \u042d\u0442\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u043b\u0438\u044f\u043d\u0438\u044f \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0430. \u0418\u043c\u044f \u0431\u0443\u0434\u0435\u043c \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0443\u0442\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043a \u0438\u043c\u0435\u043d\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0430 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u0430 \u00abmg_\u00bb (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u0432\u043f\u0435\u0440\u0432\u044b\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043b\u0438\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b \u0432 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0435 street, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 mg_street \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439). <\/p>\n<p>  \u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 mg_table \u0431\u0443\u0434\u0443\u0442 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043c\u0435\u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0445 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u043e\u0432. \u0412 mg_merge \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043b\u044e\u0447\u0438 \u0441\u043b\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0430 \u0432 mg_ref \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0441\u0441\u044b\u043b\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043d\u0430 \u0441\u0442\u0430\u0440\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041b\u0435\u0433\u043a\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0442\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043e\u0434\u043d\u043e\u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u043c\u0438 \u043a\u043b\u044e\u0447\u0430\u043c\u0438. \u041f\u043e \u044d\u0442\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435, \u043f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0442\u043e\u0433\u043e, \u043c\u043e\u0436\u0435\u043c \u043b\u0438 \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u043c \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u043e\u043c:<\/p>\n<pre><code class=\"sql\">select max(cn) from ( select b.constraint_name, count(*) cn        from   user_constraints a        inner  join user_constraints b on (b.r_constraint_name = a.constraint_name)        inner  join user_cons_columns c on (c.constraint_name = b.constraint_name)        where  a.table_name = upper(p_name)        group  by b.constraint_name ); <\/code><\/pre>\n<p>  \u042d\u0442\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432\u043e \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u043a\u043b\u044e\u0447\u0430\u0445, \u0441\u0441\u044b\u043b\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0437\u0430\u0434\u0430\u043d\u043d\u0443\u044e \u0438\u043c\u0435\u043d\u0435\u043c p_name. \u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 Oracle \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e. \u041d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043d\u0430\u0438\u0437\u0443\u0441\u0442\u044c \u0438\u043c\u0435\u043d\u0430 \u0432\u0441\u0435\u0445 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439 Oracle. \u041c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u043c \u043e\u0441\u0432\u0435\u0436\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">select * from dictionary <\/code><\/pre>\n<p>  \u0414\u0430\u043b\u0435\u0435, \u0432\u0441\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f, \u043f\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e \u0441\u043b\u0438\u044f\u043d\u0438\u044f, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b, \u0445\u043e\u0442\u044f \u0438 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <a href=\"http:\/\/docs.oracle.com\/cd\/B19306_01\/appdev.102\/b14261\/executeimmediate_statement.htm\">execute immediate<\/a> (\u043f\u043e\u043b\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442 \u043f\u0430\u043a\u0435\u0442\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0442\u0430\u0442\u044c\u0438). <\/p>\n<p>  \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u043f\u043e \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0435 \u043e\u0442\u043a\u0430\u0442\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439. \u041f\u043e\u043c\u0438\u043c\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0442\u043a\u0430\u0442\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0442\u043a\u0430\u0442\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435, \u0443\u043a\u0430\u0437\u0430\u0432 \u0435\u0433\u043e ID. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043e\u0442\u043a\u0430\u0442\u0438\u0442\u044c \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043d\u0435 \u043b\u044e\u0431\u043e\u0435 \u0441\u043b\u0438\u044f\u043d\u0438\u0435. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u043a\u0430\u0442\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"sql\">select count(*) from   mg_merge where old_id = l_new; <\/code><\/pre>\n<p>  \u0421\u043c\u044b\u0441\u043b \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u0440\u043e\u0441\u0442 \u2014 \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u043a\u0430\u0442\u0438\u0442\u044c \u0441\u043b\u0438\u044f\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u0435\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0430\u044f\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0435\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0431\u044b\u043b\u0430 \u0432\u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u043c \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c.<\/p>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u043a\u0435\u0442\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">mg_merge_pkg.sql<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">create or replace package mg_merge_pkg as     procedure merge(p_name in varchar2, p_old in number, p_new in number);     procedure undo(p_id in number);     procedure undo; end mg_merge_pkg; \/  show errors;  create or replace package body mg_merge_pkg as      e_unsupported_error EXCEPTION;     pragma EXCEPTION_INIT(e_unsupported_error, -20001);      cursor c_col(p_name varchar2, p_pk varchar2) is     select column_name     from   user_tab_columns     where  table_name = upper(p_name)     and    column_name &lt;&gt; p_pk;      procedure merge(p_name in varchar2, p_old in number, p_new in number) as     cursor c_fk is     select b.table_name, c.column_name, e.column_name pk_name     from   user_constraints a     inner  join user_constraints b on (b.r_constraint_name = a.constraint_name)     inner  join user_cons_columns c on (c.constraint_name = b.constraint_name)     inner  join user_constraints d on (d.table_name = b.table_name                                          and d.constraint_type = 'P')     inner  join user_cons_columns e on (e.constraint_name = d.constraint_name)     where  a.table_name = upper(p_name);     r_fk   c_fk%rowtype;     r_col  c_col%rowtype;     l_id   number default null;     l_cn   number default null;     l_pk   varchar2(30) default null;     l_sql  varchar2(500) default null;     begin       select max(cn) into l_cn       from ( select b.constraint_name, count(*) cn              from   user_constraints a              inner  join user_constraints b on (b.r_constraint_name = a.constraint_name)              inner  join user_cons_columns c on (c.constraint_name = b.constraint_name)              where  a.table_name = upper(p_name)              group  by b.constraint_name );       if l_cn &gt; 1 then          RAISE_APPLICATION_ERROR(-20001, 'Can''t support multicolumn FK');       end if;              select c.column_name into l_pk       from   user_constraints a       inner  join user_cons_columns c on (c.constraint_name = a.constraint_name)       where  a.table_name = upper(p_name)       and    a.constraint_type = 'P';              select count(*) into l_cn       from   mg_table where name = upper(p_name);       if l_cn = 0 then          insert into mg_table(name) values (upper(p_name));          execute immediate         'create table mg_' || p_name || ' as select * from ' || upper(p_name) || ' ' ||         'where rownum = 0';          execute immediate         'create unique index mg_' || p_name || '_pk on mg_' || p_name || '(' || l_pk || ')';       end if;              insert into mg_merge(id, table_name, old_id, new_id)       values (mg_merge_seq.nextval, upper(p_name), p_old, p_new)       returning id into l_id;               open c_fk;       loop         fetch c_fk into r_fk;         exit when c_fk%notfound;                  execute immediate        'insert into mg_ref(id, merge_id, table_name, pk_name, column_name, object_id) ' ||        'select mg_ref_seq.nextval, :merge_id, :tab_name, :pk_name, :col_name, ' || r_fk.pk_name || ' ' ||        'from   ' || r_fk.table_name || ' where ' || r_fk.column_name || ' = :old_id'          using l_id, r_fk.table_name, r_fk.pk_name, r_fk.column_name, p_old;                  execute immediate        'update ' || r_fk.table_name || ' set ' || r_fk.column_name || ' = :new_id ' ||        'where ' || r_fk.column_name || ' = :old_id' using p_new, p_old;       end loop;       close c_fk;              l_sql := 'insert into mg_' || p_name || '(' || l_pk;       open c_col(p_name, l_pk);       loop         fetch c_col into r_col;         exit when c_col%notfound;         l_sql := l_sql || ',' || r_col.column_name;       end loop;       close c_col;       l_sql := l_sql || ') select '|| l_pk;       open c_col(p_name, l_pk);       loop         fetch c_col into r_col;         exit when c_col%notfound;         l_sql := l_sql || ',' || r_col.column_name;       end loop;       close c_col;       l_sql := l_sql || ' from ' || p_name || ' where ' || l_pk || ' = :old_id';       execute immediate l_sql using p_old;              execute immediate      'delete from ' || p_name || ' where ' || l_pk || ' = :id' using p_old;       commit;     exception       when others then         if c_fk%isopen then close c_fk; end if;         if c_col%isopen then close c_col; end if;         rollback;         raise;     end;      procedure undo(p_id in number) as     cursor c_fk is     select table_name, pk_name, column_name     from   mg_ref     where  merge_id = p_id     group  by table_name, pk_name, column_name;     r_fk   c_fk%rowtype;     r_col  c_col%rowtype;     l_name varchar2(30) default null;     l_old  number default null;     l_new  number default null;     l_cn   number default null;     l_pk   varchar2(30) default null;     l_sql  varchar2(500) default null;     begin       select table_name, old_id, new_id into l_name, l_old, l_new       from   mg_merge where id = p_id;              select count(*) into l_cn       from   mg_merge where old_id = l_new;       if l_cn &gt; 0 then          RAISE_APPLICATION_ERROR(-20001, 'Can''t undo');       end if;              select c.column_name into l_pk       from   user_constraints a       inner  join user_cons_columns c on (c.constraint_name = a.constraint_name)       where  a.table_name = upper(l_name)       and    a.constraint_type = 'P';        l_sql := 'insert into ' || l_name || '(' || l_pk;       open c_col(l_name, l_pk);       loop         fetch c_col into r_col;         exit when c_col%notfound;         l_sql := l_sql || ',' || r_col.column_name;       end loop;       close c_col;       l_sql := l_sql || ') select '|| l_pk;       open c_col(l_name, l_pk);       loop         fetch c_col into r_col;         exit when c_col%notfound;         l_sql := l_sql || ',' || r_col.column_name;       end loop;       close c_col;       l_sql := l_sql || ' from mg_' || l_name || ' where ' || l_pk || ' = :old_id';       execute immediate l_sql using l_old;              open c_fk;       loop         fetch c_fk into r_fk;         exit when c_fk%notfound;                  execute immediate        'merge into ' || r_fk.table_name || ' d using mg_ref s '||        'on (s.object_id = d.' || r_fk.pk_name || ' and s.merge_id = :id and s.table_name = :tab_name and s.column_name = :col_name) ' ||        'when matched then ' ||        'update set d.' || r_fk.column_name || ' = :old_id' using p_id, r_fk.table_name, r_fk.column_name, l_old;        end loop;       close c_fk;              execute immediate      'delete from mg_' || l_name || ' where ' || l_pk || ' = :id' using l_old;               delete from mg_ref where merge_id = p_id;       delete from mg_merge where id = p_id;        commit;     exception       when others then         if c_fk%isopen then close c_fk; end if;         if c_col%isopen then close c_col; end if;         rollback;         raise;     end;      procedure undo as     l_id number default null;     begin       select max(id) into l_id from mg_merge;       undo(l_id);     end; end mg_merge_pkg; \/  show errors; <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043b\u0438\u0442\u044c \u0441\u043b\u043e\u0432\u0430\u0440\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">begin   mg_merge_pkg.merge('street', 2, 1); end; <\/code><\/pre>\n<p>  \u0418 \u043e\u0442\u043a\u0430\u0442\u0438\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c:<\/p>\n<pre><code class=\"sql\">begin   mg_merge_pkg.undo; end; <\/code><\/pre>\n<p>      \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/181378\/\"> http:\/\/habrahabr.ru\/post\/181378\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0412\u0441\u0435\u0433\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043d\u0435\u0439 \u043d\u0430\u0437\u0430\u0434 \u044f <a href=\"http:\/\/habrahabr.ru\/post\/179789\/\">\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b<\/a> \u043d\u0430\u0431\u043e\u0440 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440, \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0449\u0438\u0445 \u0431\u043e\u0440\u043e\u0442\u044c\u0441\u044f \u0441 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u043c\u0438 \u0432 \u0411\u0414 PostgreSQL. \u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u043f\u043e\u0434 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u043c\u0438 \u044f \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0437\u0430\u043f\u0438\u0441\u0438 \u0432\u043d\u0435\u0441\u0435\u043d\u043d\u044b\u0435 \u0432 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043e\u0448\u0438\u0431\u043a\u0435. \u041a\u0430\u043a \u0432\u044b\u044f\u0441\u043d\u0438\u043b\u043e\u0441\u044c, \u0434\u043b\u044f Oracle \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u0435\u043d.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-181378","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/181378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=181378"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/181378\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=181378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=181378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=181378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}