{"id":334,"date":"2013-09-23T06:58:55","date_gmt":"2013-09-23T06:58:55","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=334"},"modified":"2013-09-23T12:10:18","modified_gmt":"2013-09-23T12:10:18","slug":"creating-auto-increment-column-in-oracle","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/creating-auto-increment-column-in-oracle\/","title":{"rendered":"Creating auto increment column in Oracle"},"content":{"rendered":"<p>You can create autoincrement column in oracle in two ways.<\/p>\n<p><strong>Method 1 : Using sequence<\/strong><\/p>\n<p><em>Table Creation Query<\/em><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n CREATE TABLE tblusers(UserId INT, UserName VARCHAR(50));\r\n<\/pre>\n<p><em>Sequence Creation Query<\/em><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n  CREATE SEQUENCE tblusers_userid_seq \r\n         MINVALUE 1 MAXVALUE 999999999999999999999999999 \r\n         START WITH 1 INCREMENT BY 1 CACHE 20;\r\n<\/pre>\n<p>The above code creates a sequence for table tblusers with name tblusers_userid_seq with start value of 1 and limit 999999999999999999999999999.<\/p>\n<p><em>Insertion Query<\/em><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n  INSERT INTO tblusers(UserId, UserName) VALUES (tblusers_userid_seq.nextVal, 'John')\r\n<\/pre>\n<p><strong>Method 2 : Using Trigger<\/strong><br \/>\nIn Method 2 we can use triggers to carry out auto increment when rows are added to the table.<\/p>\n<p>Before creating trigger you should make the auto increment column as primary key.For that the code is as below<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n  ALTER TABLE tblusers ADD (\r\n  CONSTRAINT UserId_pk PRIMARY KEY (UserId));\r\n<\/pre>\n<p>The Creation of sequence and trigger is as follows<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE SEQUENCE tblusers_userid_seq;  \r\nCREATE OR REPLACE TRIGGER tblusers_userid_trigg \r\nBEFORE INSERT ON tblusers \r\nFOR EACH ROW\r\nBEGIN\r\n  SELECT tblusers_userid_seq.NEXTVAL\r\n  INTO   :new.UserId\r\n  FROM   dual;\r\nEND;\r\n<\/pre>\n<p>Now during insertion there is no need to worry about the auto increment column and the insert query no need to contain the Id column as below<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n  INSERT INTO tblusers(UserName) VALUES ('John')\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can create autoincrement column in oracle in two ways. Method 1 : Using sequence Table Creation Query CREATE TABLE tblusers(UserId INT, UserName VARCHAR(50)); Sequence Creation Query CREATE SEQUENCE tblusers_userid_seq MINVALUE 1 MAXVALUE 999999999999999999999999999 START WITH 1 INCREMENT BY 1 CACHE 20; The above code creates a sequence for table tblusers with name tblusers_userid_seq with&hellip; <a href=\"https:\/\/codethataint.com\/blog\/creating-auto-increment-column-in-oracle\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry","category-oracle-database-2"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"predecessor-version":[{"id":341,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions\/341"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}