{"id":3130,"date":"2019-05-21T22:12:24","date_gmt":"2019-05-21T20:12:24","guid":{"rendered":"http:\/\/van-maanen.com\/?p=3130"},"modified":"2019-05-21T22:12:24","modified_gmt":"2019-05-21T20:12:24","slug":"giving-values-to-primary-key","status":"publish","type":"post","link":"http:\/\/archief.van-maanen.com\/?p=3130","title":{"rendered":"Giving values to primary key"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Let us see how we might provide a value to the primary key in Oracle. There are many ways to provide a value for the primary key. One is to create a trigger (see  <a href=\"http:\/\/van-maanen.com\/index.php\/2011\/02\/02\/create-a-surrogate-key-in-oracle\/\">http:\/\/van-maanen.com\/index.php\/2011\/02\/02\/create-a-surrogate-key-in-oracle\/<\/a>  ). Another is to declare a field as  an identity field (id NUMBER GENERATED ALWAYS AS IDENTITY,) . A third way is to create a sequence that is used to load unique values into that primary key field.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let me provide an example to load such field.  The field is loaded with the NEXTVAL field of a sequence. Then the value is created first. Then the value is loaded into the field that contains the primary key.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DECLARE\n  NewUserID   employee.empid%TYPE;\nBEGIN\n  NewUserID := EmpIDSequence.NEXTVAL;\n  INSERT INTO employee (dno, empid)  VALUES( 5, NewUserID);\nEND;\n\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Another example is direct usage in an insert statement of a sequence object. Recently this possibility to use sequence objects in inserts is introduced in Oracle.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INSERT INTO USERS    VALUES ( 'John',    UsrIDSeq.NEXTVAL); <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let us see how we might provide a value to the primary key in Oracle. There are many ways to provide a value for the primary key. One is to create a trigger (see http:\/\/van-maanen.com\/index.php\/2011\/02\/02\/create-a-surrogate-key-in-oracle\/ ). Another is to declare a field as an identity field (id NUMBER GENERATED ALWAYS AS IDENTITY,) . A third [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3130","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3130","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=3130"}],"version-history":[{"count":0,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=\/wp\/v2\/posts\/3130\/revisions"}],"wp:attachment":[{"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3130"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/archief.van-maanen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}