{"id":34,"date":"2011-02-02T21:30:01","date_gmt":"2011-02-02T21:30:01","guid":{"rendered":"http:\/\/62.131.51.129\/wordpress\/?p=34"},"modified":"2011-02-02T21:30:01","modified_gmt":"2011-02-02T21:30:01","slug":"create-a-surrogate-key-in-oracle","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=34","title":{"rendered":"Create a surrogate key in Oracle"},"content":{"rendered":"<p>To create a surrogate key in Oracle is not really trivial. The issue, we address works as follows. Assume we have a table with 3 records an four attributes. The first attribute is a name, the second is a surrogate key. Other attributes refer to a user and a datetime stamp. This surrogate key should contain a unique number that allows us to locate the record uniquely. Let us assume the table looks like:<\/p>\n<pre>name, SURKEY, CreatedDate, CreatedUser\ntom    1\nine     2\npaula \n<\/pre>\n<p>In this case, we would like to have a unique value next to &#8220;paula&#8221;.<br \/>\nThis can be accomplished by a so-called trigger that creates such a unique value every time a record is inserted. To ensure that a unique value is retrieved, we use a sequence. The trigger has next code:<\/p>\n<pre>create or replace\nTRIGGER SCOTT.DWH_SAI_ID\nBEFORE INSERT\nON SCOTT.TABLE_SURKEY \nREFERENCING NEW AS New OLD AS Old\nFOR EACH ROW\nDECLARE\ntmpVar NUMBER;\nBEGIN\n   tmpVar := 0;\n   SELECT DWH_TECH_ID.NEXTVAL INTO tmpVar FROM dual;\n   :NEW.SURKEY := tmpVar;\n   :NEW.CreatedDate := SYSDATE;\n   :NEW.CreatedUser := USER;\nEND DWH_SAI_ID; \n<\/pre>\n<p>This trigger takes a new (unique) value from a sequence DWH_TECH_ID and stores this value in attribute SURKEY. Along with the surrogate key, the user that inserted the record and the datetime is stored in the record.<\/p>\n<p>The result is:<\/p>\n<pre>name, SURKEY, CreatedDate, CreatedUser\ntom    1\nine     2\npaula  3           2010\/2\/2      tom\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To create a surrogate key in Oracle is not really trivial. The issue, we address works as follows. Assume we have a table with 3 records an four attributes. The first attribute is a name, the second is a surrogate key. Other attributes refer to a user and a datetime stamp. This surrogate key should [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1220,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-34","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-warehousing"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=34"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}