<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Oracle Auto Increment Columns &#8211; Part 2</title>
	<atom:link href="http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/</link>
	<description>because I don't believe in life before coffee...</description>
	<lastBuildDate>Wed, 28 Oct 2009 06:44:36 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Jon Emmons</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-373116</link>
		<dc:creator>Jon Emmons</dc:creator>
		<pubDate>Sun, 17 May 2009 19:39:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-373116</guid>
		<description>Quick,

Actually, Oracle is clever enough to maintain currval for your existing session so you will get the correct value even if someone else is accessing or updating the sequence.</description>
		<content:encoded><![CDATA[<p>Quick,</p>
<p>Actually, Oracle is clever enough to maintain currval for your existing session so you will get the correct value even if someone else is accessing or updating the sequence.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: quick</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-372885</link>
		<dc:creator>quick</dc:creator>
		<pubDate>Tue, 07 Apr 2009 21:22:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-372885</guid>
		<description>This code is not safe, if you have a database that is being updated by multiple processes you will receive the wrong value...</description>
		<content:encoded><![CDATA[<p>This code is not safe, if you have a database that is being updated by multiple processes you will receive the wrong value&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Krishantha</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-370314</link>
		<dc:creator>Krishantha</dc:creator>
		<pubDate>Fri, 19 Sep 2008 12:30:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-370314</guid>
		<description>Sorry little typo error. In the above post. &quot;AFTER INSERT&quot; on Trigger definition should be be changed as &quot;BEFORE INSERT&quot;

Thanks.
Krishantha.</description>
		<content:encoded><![CDATA[<p>Sorry little typo error. In the above post. &#8220;AFTER INSERT&#8221; on Trigger definition should be be changed as &#8220;BEFORE INSERT&#8221;</p>
<p>Thanks.<br />
Krishantha.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Krishantha</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-370313</link>
		<dc:creator>Krishantha</dc:creator>
		<pubDate>Fri, 19 Sep 2008 12:28:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-370313</guid>
		<description>Hi Jon,

Here is my trigger.

CREATE TABLE test1 (id NUMBER PRIMARY KEY,name VARCHAR2(30) NOT NULL)
/
CREATE SEQUENCE test1_sequence START WITH 1 INCREMENT BY 1
/
CREATE OR REPLACE TRIGGER test1_trigger
AFTER INSERT
ON test1
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT test1_sequence.nextval INTO :NEW.ID FROM dual;
END;
/
commit;
/

After trying to insert data to the table with NULL name value. ID sequence got changed.

SQL&gt; select * from test1;

        ID NAME
---------- ------------------------------
         1 kkTEST
         2 kkTEST11
         3 kkTEST12
         5 kkTEST14555


I update the &quot;before insert&quot; as &quot;after insert&quot; and executed the trigger. This time I got the following error.

ORA-04084: cannot change NEW values for this trigger type

Any help will be greatly appreciated.

Thanks,
Krishantha.</description>
		<content:encoded><![CDATA[<p>Hi Jon,</p>
<p>Here is my trigger.</p>
<p>CREATE TABLE test1 (id NUMBER PRIMARY KEY,name VARCHAR2(30) NOT NULL)<br />
/<br />
CREATE SEQUENCE test1_sequence START WITH 1 INCREMENT BY 1<br />
/<br />
CREATE OR REPLACE TRIGGER test1_trigger<br />
AFTER INSERT<br />
ON test1<br />
REFERENCING NEW AS NEW<br />
FOR EACH ROW<br />
BEGIN<br />
SELECT test1_sequence.nextval INTO :NEW.ID FROM dual;<br />
END;<br />
/<br />
commit;<br />
/</p>
<p>After trying to insert data to the table with NULL name value. ID sequence got changed.</p>
<p>SQL&gt; select * from test1;</p>
<p>        ID NAME<br />
&#8212;&#8212;&#8212;- &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
         1 kkTEST<br />
         2 kkTEST11<br />
         3 kkTEST12<br />
         5 kkTEST14555</p>
<p>I update the &#8220;before insert&#8221; as &#8220;after insert&#8221; and executed the trigger. This time I got the following error.</p>
<p>ORA-04084: cannot change NEW values for this trigger type</p>
<p>Any help will be greatly appreciated.</p>
<p>Thanks,<br />
Krishantha.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Emmons</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-370304</link>
		<dc:creator>Jon Emmons</dc:creator>
		<pubDate>Thu, 18 Sep 2008 02:28:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-370304</guid>
		<description>Krishantha,

How are you activating the trigger?  You might be able to prevent false incrementing by changing to an &#039;after insert&#039; trigger.  More likely you&#039;ll want to make the trigger check the name column and raise an error if it&#039;s null.

Hope this helps.</description>
		<content:encoded><![CDATA[<p>Krishantha,</p>
<p>How are you activating the trigger?  You might be able to prevent false incrementing by changing to an &#8216;after insert&#8217; trigger.  More likely you&#8217;ll want to make the trigger check the name column and raise an error if it&#8217;s null.</p>
<p>Hope this helps.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: krishantha</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-370279</link>
		<dc:creator>krishantha</dc:creator>
		<pubDate>Mon, 15 Sep 2008 09:47:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-370279</guid>
		<description>Hi Jon,

Thanks for your valuable tips. My table A has two columns which are ID and Name. I used sequence to increment the ID number sequentially. &quot;Name&quot; column is with not null constraint. My problem in here is that if a user insert data into table &quot;A&quot; without giving a value for &quot;Name&quot; column then the trigger will fire and sequence value will be increment by 1. in the next time if I insert name value,then ID numbers are not sequential. How I can handle this problem from DB level.

Thanks,
Krishantha.</description>
		<content:encoded><![CDATA[<p>Hi Jon,</p>
<p>Thanks for your valuable tips. My table A has two columns which are ID and Name. I used sequence to increment the ID number sequentially. &#8220;Name&#8221; column is with not null constraint. My problem in here is that if a user insert data into table &#8220;A&#8221; without giving a value for &#8220;Name&#8221; column then the trigger will fire and sequence value will be increment by 1. in the next time if I insert name value,then ID numbers are not sequential. How I can handle this problem from DB level.</p>
<p>Thanks,<br />
Krishantha.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rajneesh</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-338722</link>
		<dc:creator>rajneesh</dc:creator>
		<pubDate>Mon, 03 Dec 2007 09:35:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-338722</guid>
		<description>i create a table. in this table one coloum id is primary key . i want to increment this value automaticaly. user use this application without insert these value. in database this value automaticaly increment.</description>
		<content:encoded><![CDATA[<p>i create a table. in this table one coloum id is primary key . i want to increment this value automaticaly. user use this application without insert these value. in database this value automaticaly increment.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Emmons</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-311273</link>
		<dc:creator>Jon Emmons</dc:creator>
		<pubDate>Wed, 24 Oct 2007 13:42:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-311273</guid>
		<description>Saravanan,

The only way I know to do this is to create a sequence for each table.  There are few instances where you would want to use one sequence for two different tables.  Sequences take up very little resources so there is no reason not to have one per table.</description>
		<content:encoded><![CDATA[<p>Saravanan,</p>
<p>The only way I know to do this is to create a sequence for each table.  There are few instances where you would want to use one sequence for two different tables.  Sequences take up very little resources so there is no reason not to have one per table.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Saravanan</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-309390</link>
		<dc:creator>Saravanan</dc:creator>
		<pubDate>Mon, 22 Oct 2007 06:24:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-309390</guid>
		<description>Is there a way to have the seqeuncer to start afresh(from 0 or from the previous value of the table in question) for each table, for instance I have 2 tables with auto_increment fields.I insert into table 1 and then into table 2 using seq_name.nextval.

Now the id for T1 will be 1 and id for T2 will start from 2

ofcourse I can avoid this situation by having 2 sequencer one each for T1 and T2, but just wanted to check if there is a alternative</description>
		<content:encoded><![CDATA[<p>Is there a way to have the seqeuncer to start afresh(from 0 or from the previous value of the table in question) for each table, for instance I have 2 tables with auto_increment fields.I insert into table 1 and then into table 2 using seq_name.nextval.</p>
<p>Now the id for T1 will be 1 and id for T2 will start from 2</p>
<p>ofcourse I can avoid this situation by having 2 sequencer one each for T1 and T2, but just wanted to check if there is a alternative</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madhu</title>
		<link>http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/comment-page-1/#comment-4479</link>
		<dc:creator>Madhu</dc:creator>
		<pubDate>Wed, 30 Aug 2006 09:28:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.lifeaftercoffee.com/2006/05/22/oracle-auto-increment-columns-part-2/#comment-4479</guid>
		<description>small correction:

using this way:

insert into mytable (a, b) values (1, (select NVL(max(b),0)+1 from mytable where a=1));

will ensure it works for the first time and everytime :-)
as Jon rightly said, this query will kill the performance.</description>
		<content:encoded><![CDATA[<p>small correction:</p>
<p>using this way:</p>
<p>insert into mytable (a, b) values (1, (select NVL(max(b),0)+1 from mytable where a=1));</p>
<p>will ensure it works for the first time and everytime <img src='http://www.lifeaftercoffee.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
as Jon rightly said, this query will kill the performance.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
