{"id":440,"date":"2009-09-03T01:41:31","date_gmt":"2009-09-02T19:41:31","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=440"},"modified":"2016-01-07T11:31:11","modified_gmt":"2016-01-07T05:31:11","slug":"generate-foreign-keys","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=440","title":{"rendered":"Generate foreign keys"},"content":{"rendered":"<p>The other day I had to drop a parent table, which would also remove all its child relationships. I created this script to reverse engineer the foreign key relationships a table would have.<\/p>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">select &#8216;alter table &#8216;||rtrim(creator)||&#8217;.&#8217;||tbname||<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">&#8216; add constraint &#8216;||rtrim(substr(concat(concat(reftbname,&#8217;_&#8217;),tbname),1,18))||<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">&#8216; foreign key (&#8216;||strip(replace(replace(replace(fkcolnames,&#8217; &#8216;,'&lt;&gt;&#8217;),&#8217;&gt;&lt;&#8216;,&#8221;),'&lt;&gt;&#8217;,&#8217;,&#8217;),BOTH,&#8217;,&#8217;)||&#8217;) references &#8216;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">||rtrim(reftbcreator)||&#8217;.&#8217;||reftbname||<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">case<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">when deleterule = &#8216;R&#8217; then &#8216; on delete restrict&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">when deleterule = &#8216;N&#8217; then &#8216; on delete set null&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">when deleterule = &#8216;C&#8217; then &#8216; on delete cascade&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">else &#8216; on delete no action&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">end<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">||<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">case<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">when updaterule = &#8216;R&#8217; then &#8216; on update restrict&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">when updaterule = &#8216;N&#8217; then &#8216; on update set null&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">when updaterule = &#8216;C&#8217; then &#8216; on update cascade&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">else &#8216; on update no action&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">end<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">||<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">&#8216;;&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">from sysibm.sysrels<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">where reftbname = &#8216;EMPLOYEE&#8217; or tbname = &#8216;EMPLOYEE&#8217;<\/div>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">with ur;<\/div>\n<p><span style=\"color: #3366ff;\">select &#8216;alter table &#8216;||rtrim(creator)||&#8217;.&#8217;||tbname||<br \/>\n&#8216; add constraint &#8216;||rtrim(substr(concat(concat(reftbname,&#8217;_&#8217;),tbname),1,18))||<br \/>\n&#8216; foreign key (&#8216;||strip(replace(replace(replace(fkcolnames,&#8217; &#8216;,'&lt;&gt;&#8217;),&#8217;&gt;&lt;&#8216;,&#8221;),'&lt;&gt;&#8217;,&#8217;,&#8217;),BOTH,&#8217;,&#8217;)||&#8217;) references &#8216;<br \/>\n||rtrim(reftbcreator)||&#8217;.&#8217;||reftbname||<br \/>\ncase<br \/>\nwhen deleterule = &#8216;R&#8217; then &#8216; on delete restrict&#8217;<br \/>\nwhen deleterule = &#8216;N&#8217; then &#8216; on delete set null&#8217;<br \/>\nwhen deleterule = &#8216;C&#8217; then &#8216; on delete cascade&#8217;<br \/>\nelse &#8216; on delete no action&#8217;<br \/>\nend<br \/>\n||<br \/>\ncase<br \/>\nwhen updaterule = &#8216;R&#8217; then &#8216; on update restrict&#8217;<br \/>\nwhen updaterule = &#8216;N&#8217; then &#8216; on update set null&#8217;<br \/>\nwhen updaterule = &#8216;C&#8217; then &#8216; on update cascade&#8217;<br \/>\nelse &#8216; on update no action&#8217;<br \/>\nend<br \/>\n||<br \/>\n&#8216;;&#8217;<br \/>\nfrom sysibm.sysrels<br \/>\nwhere reftbname = &#8216;EMPLOYEE&#8217; or tbname = &#8216;EMPLOYEE&#8217;<br \/>\nwith ur;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The other day I had to drop a parent table, which would also remove all its child relationships. I created this script to reverse engineer the foreign key relationships a table would have. select &#8216;alter table &#8216;||rtrim(creator)||&#8217;.&#8217;||tbname|| &#8216; add constraint &#8216;||rtrim(substr(concat(concat(reftbname,&#8217;_&#8217;),tbname),1,18))|| &#8216; foreign key (&#8216;||strip(replace(replace(replace(fkcolnames,&#8217; &#8216;,'&lt;&gt;&#8217;),&#8217;&gt;&lt;&#8216;,&#8221;),'&lt;&gt;&#8217;,&#8217;,&#8217;),BOTH,&#8217;,&#8217;)||&#8217;) references &#8216; ||rtrim(reftbcreator)||&#8217;.&#8217;||reftbname|| case when deleterule = &#8216;R&#8217; then [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[3],"tags":[],"class_list":["post-440","post","type-post","status-publish","format-standard","hentry","category-db2"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=440"}],"version-history":[{"count":1,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/440\/revisions"}],"predecessor-version":[{"id":441,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/440\/revisions\/441"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}