<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>CUBRID Serial Tutorial</title>
        <link>http://www.cubrid.org/?mid=cubrid_serial</link>
        <description>CUBRID Serial Tutorial</description>
        <language>en</language>
        <pubDate>Wed, 15 Jun 2011 20:13:46 -0800</pubDate>
        <lastBuildDate>Thu, 07 Jul 2011 17:10:32 -0800</lastBuildDate>
        <generator>XpressEngine 1.4.4.1</generator>
                        										        <item>
            <title>CUBRID Serial Tutorial</title>
            <dc:creator>admin</dc:creator>
            <link>http://www.cubrid.org/cubrid_serial</link>
            <guid isPermaLink="true">http://www.cubrid.org/cubrid_serial</guid>
                                    <description><![CDATA[<h1>CUBRID Serials</h1> 

<div class="contents-table"></div>

<p>The goal of this tutorial is to introduce some of the CUBRID features regarding <strong>Serial</strong> objects.&nbsp;This article will also show some "tricks" users can use in combination with <strong>Serials</strong>.</p> 

<h2>Overview</h2> 

<p><a href="/manual/840/en/SERIAL" target="_self">CUBRID <strong>Serials</strong></a> are a special type of database objects, which are used for creating and accessing a unique sequence number.</p> 

<p><strong>Serials</strong> have specific properties:</p> 

<ul>
	<li><b>Explicit</b>&nbsp;serials&nbsp;are serials created via <b>CREATE SERIAL</b> statement. They&nbsp;are not related with a particular table so can be used in multiple tables.</li><li><b>Implicit</b>&nbsp;serials are automatically created by CUBRID whenever an <b>AUTO_INCREMENT</b> attribute has been assigned to a column.</li> 
	<li>Once created, all users can get the Serials values by using <b>CURRENT_VALUE</b> and <b>NEXT_VALUE</b> methods/pseudo-columns.</li></ul> 

<p>Database serials can be very useful for:</p> 
<ul>
	<li>Creating a unique sequence number in multi-user environment.</li> 
	<li>Auto-generate a unique id for a field as a default value for that field.</li> 
	<li>Generate artificial primary keys.</li> 
	<li>Enforce unique id's across client applications.</li> 
</ul>

<p><span style="color:black">Many database systems have nowadays support for similar type of objects, for example:</span></p> 
<ul>
	<li>ORACLE&nbsp;<a href="http://orafaq.com/wiki/Sequence" target="_self">SEQUENCE</a></li> 
	<li>MS SQL Server <a href="http://msdn.microsoft.com/en-us/library/aa933196%28v=sql.80%29.aspx" target="_self">IDENTITY</a></li>
	<li>PostgreSQL <a href="http://www.postgresql.org/docs/8.1/static/sql-createsequence.html" target="_self">SEQUENCE</a></li> 
</ul>

<table width="99%" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;border:none">
	<tbody><tr>
		<td valign="top" style="border:solid windowtext 1.0pt; background:#EEECE1;padding:0in 5.4pt 0in 5.4pt">
			<p><strong>Warning</strong>: Even if they look very similar in concepts, there are some differences between various databases implementations of this concept.&nbsp;For instance, when comparing CUBRID and ORACLE implementations, there are different behaviors when using parallel user sessions in combination with retrieving the current value of the sequence.</p>
		</td>
	</tr>
</tbody></table> 


<h2>AUTO_INCREMENT and Serial</h2> 
<p><i>So what is the connection between <strong>AUTO_INCREMENT</strong> and <strong>Serials</strong> in CUBRID?</i></p> 
<p>Actually, even if they don’t seem to be related at a first sight, there actually are!&nbsp;When we create a new table in CUBRID, we have an option to specify an&nbsp;<strong>AUTO_INCREMENT</strong> attribute&nbsp;for a column just like in other RDBMS:</p> 
<center><img border="0" width="601" height="473" id="Picture 3" src="/files/docs/tutorials/serials/image001.jpg" editor_component="image_link"/></center> 
<p>However, if you look&nbsp;at the <strong>Serials</strong> list shown below, there are no changes. Still two serials which were created for the <b>demodb </b>database which is the demo database created automatically during CUBRID installation.</p> 
<p align="center" style="text-align:center"><img border="0" width="277" height="109" id="Picture 6" src="/files/docs/tutorials/serials/image002.png" editor_component="image_link"/></p> 
<p>No additional serial has been created, because&nbsp;we have not executed any <b>CREATE SERIAL</b> statement.&nbsp;However, if we look at the content of the system table <strong>db_serial</strong>, you will notice that an extra record has been created:</p> 
<center><img border="0" width="601" height="179" id="Picture 8" src="/files/docs/tutorials/serials/image003.jpg" editor_component="image_link"/></center> 
<p>The reason for this is very simple:</p> 

<table width="99%" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;border:none">
	<tbody><tr>
		<td valign="top" style="border:solid windowtext 1.0pt; background:#EEECE1;padding:0in 5.4pt 0in 5.4pt">
			<p>Whenever an <strong>AUTO_INCREMENT	</strong> attribute is defined, CUBRID automatically creates an "<b>implicit</b>" Serial object.</p>
		</td>
	</tr>
</tbody></table> 

<p>This "implicit" AUTO_INCREMENT serial will be used by the system to handle the values in the associated column.</p> 
<p>Summarizing, we have two types of Serials in CUBRID:</p> 
<ul>
	<li><strong>"Explicit"</strong> – created via CREATE SERIAL statements</li> 
	<li><strong>"Implicit"</strong> – create automatically by the system for AUTO_INCREMENT columns</li> 
</ul>


<h2>Modifying the system "implicit" serials</h2> 
<p>The system <b>implicit </b>serials (those automatically created for the AUTO_INCREMENT columns) behave in many ways the same as the "explicit" serials (the ones created using the CREATE SERIAL statement). </p> 
<p>For example, you can use the <i>current_value</i> and <i>next_values</i> as usual:</p> 

<p align="center" style="text-align:center"><img border="0" width="256" height="207" id="Picture 9" src="/files/docs/tutorials/serials/image004.png" editor_component="image_link"/></p> 
<p>We can even modify the <i>current_value/next_value</i> sequence:</p> 
<p align="center" style="text-align:center"><img border="0" width="270" height="259" id="Picture 10" src="/files/docs/tutorials/serials/image005.png" editor_component="image_link"/></p> 
<p>However, if you try to update directly the <strong>db_serial</strong> table, you will get an error:</p> 
<p align="center" style="text-align:center"><img border="0" width="564" height="353" id="Picture 14" src="/files/docs/tutorials/serials/image006.png" editor_component="image_link"/></p> 
<p>And this is the default behavior stating with CUBRID version 8.1.0.</p> 

<table width="99%" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;border:none">
	<tbody><tr>
		<td valign="top" style="border:solid windowtext 1.0pt; background:#EEECE1;padding:0in 5.4pt 0in 5.4pt">
			<p><strong>Warning</strong>: Unless you have good reasons or you know exactly what you are doing, it is not advisable to alter the default behavior of the system "implicit" AUTO_INCREMENT serials.</p>
		</td>
	</tr>
</tbody></table> 

<h2>An AUTO_DECREMENT column</h2> 

<p>Theoretically, we can define only <strong>AUTO_INCREMENT</strong> columns, right…?</p> 
<p>But what if we need an AUTO_DECREMENT column…? …Is there any way we can get one in CUBRID?</p> 
<p>The first thing a user would try to do would probably be specifying a negative increment value in the column definition. However, CUBRID Manager will warn you about this and will not allow to proceed.</p> 
<center><img border="0" width="588" height="457" id="Picture 11" src="/files/docs/tutorials/serials/image007.png" editor_component="image_link"/></center>
<h3>Syntax</h3><p>Here is the syntax how AUTO_INCREMENT should be used.</p>

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
<p><strong><i>AUTO_INCREMENT</i></strong><i> [(seed, increment)]</i></p>
</div>

<ul>
	<li><strong>seed</strong>: The initial value from which the number starts. Only positive integers are allowed. The default is 1. </li> 
	<li><strong>increment</strong>: The increment value of each row. <strong><span style="color:red">Only positive integers are allowed</span></strong>. The default value is 1.</li> 
</ul>

<p>But, <strong>we can do a little hack </strong>- manually update the <strong>db_serial</strong> table, via an <b>ALTER SERIAL</b> statement.</p> 

<p align="center" style="text-align:center"><img border="0" width="578" height="330" id="Picture 12" src="/files/docs/tutorials/serials/image008.png" editor_component="image_link"/></p> 

<p>And voilà - the results are exactly what we wanted – now we have an <strong>AUTO_DECREMENT</strong> up &amp; running!</p> 

<p align="center" style="text-align:center"><img border="0" width="394" height="306" id="Picture 13" src="/files/docs/tutorials/serials/image009.png" editor_component="image_link"/></p> 

<h2>Serials via Triggers</h2> 

<p>There is another way to get a more complex "default" value for a table column, besides using an AUTO_INCREMENT (or an AUTO_DECREMENT as we showed above), and that is <b>using triggers</b>.</p> 

<p>Using triggers, not only we can implement the AUTO_INCREMENT functionality, but we can implement more complex rules regarding the values automatically used for the column.</p> 

<p>Let’s start by creating a table – <strong>t_my_ai</strong> (note that we have not defined an AUTO_INCREMENT):</p> 

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
<p>CREATE TABLE "t_my_ai"(</p>
<p>"id" integer,</p>
<p>"txt" character varying(16)</p>
<p>);</p>
</div>

<p>Next step - let’s create a basic default serial:</p> 

<p align="center" style="text-align:center"><img border="0" width="457" height="319" id="Picture 16" src="/files/docs/tutorials/serials/image010.png" editor_component="image_link"/></p> 

<p>And now we will create a trigger which will update automatically the <i>id</i> column in the table <strong>t_my_ai</strong>, the same way an AUTO_INCREMENT attribute would do, <span style="color: rgb(0, 0, 0); ">but we will make slight modifications. We want every next&nbsp;</span><i style="color: red; "><span style="color: rgb(0, 0, 0); ">id</span></i>&nbsp;be a square of the&nbsp;consecutive number.&nbsp;Obviously, this functionality can’t be achieved using AUTO_INCREMENT or Serials definitions… …but a trigger can do the job!</p> 

<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
<p>CREATE TRIGGER "trg_update_ai"</p>
<p>AFTER INSERT ON "t_my_ai"</p>
<p>IF [obj].[id] is null</p> 
<p>EXECUTE AFTER</p> 
<p>update [t_my_ai] set [id]=(select s_my_incr.next_value * s_my_incr.current_value) where ([id] is null );</p>
</div>

<p>Now, let’s test it:</p> 
<p align="center" style="text-align:center"><img border="0" width="601" height="371" id="Picture 25" src="/files/docs/tutorials/serials/image011.png" editor_component="image_link"/></p> 
<p>Quite interesting, right…?</p> 

<h2>Summary</h2> 

<p>We hope we showed you in this tutorial some interesting ways to use CUBRID <strong>Serials</strong> and how to create your own customized serials-alike functionalities! </p> 
<p>You can even extend this functionality to deal with non-numeric data types – it is up to you to figure out, whenever you need and auto-value functionality if maybe a serial is a best fit, eventually combined with a trigger </p> 
<p>We definitely encourage you to explore all these possibilities in CUBRID!</p> 
<h2>Links &amp; Resources</h2> 

<table width="99%" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;border:none">
	<tbody><tr>
		<td valign="top" style="border:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt" class="">
			<p style="margin-bottom:0in;margin-bottom:.0001pt;line-height: normal">CUBRID Online Manual</p>
		</td>
		<td valign="top" style="border:solid windowtext 1.0pt; border-left:none;padding:0in 5.4pt 0in 5.4pt" class="">
			<p style="margin-bottom:0in;margin-bottom:.0001pt;line-height: normal"><a href="/webmanual/3.1/">http://www.cubrid.org/webmanual/3.1/</a> </p>
		</td>
	</tr>
	<tr>
		<td valign="top" style="border:solid windowtext 1.0pt; border-top:none;padding:0in 5.4pt 0in 5.4pt" class="">
			<p style="margin-bottom:0in;margin-bottom:.0001pt;line-height: normal">CUBRID Serials Manual</p>
		</td>
		<td valign="top" style="border-top:none;border-left: none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt" class="">
			<p style="margin-bottom:0in;margin-bottom:.0001pt;line-height: normal"><a href="/manual/830/en/SERIAL">http://www.cubrid.org/manual/830/en/SERIAL</a> </p>
		</td>
	</tr>
	<tr>
		<td valign="top" style="border:solid windowtext 1.0pt; border-top:none;padding:0in 5.4pt 0in 5.4pt" class="">
			<p style="margin-bottom:0in;margin-bottom:.0001pt;line-height: normal">CUBRID Developer's Course</p>
		</td>
		<td valign="top" style="border-top:none;border-left: none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt" class="">
			<p style="margin-bottom:0in;margin-bottom:.0001pt;line-height: normal"><a href="http://www.slideshare.net/cubrid/cubrid-developers-course">http://www.slideshare.net/cubrid/cubrid-developers-course</a> </p>
		</td>
	</tr>
	<tr>
		<td valign="top" style="border:solid windowtext 1.0pt; border-top:none;padding:0in 5.4pt 0in 5.4pt" class="">
			<p style="margin-bottom:0in;margin-bottom:.0001pt;line-height: normal">Auto Increment</p>
		</td>
		<td valign="top" style="border-top:none;border-left: none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt; padding:0in 5.4pt 0in 5.4pt" class="">
			<p style="margin-bottom:0in;margin-bottom:.0001pt;line-height: normal"><a href="http://www.w3schools.com/sql/sql_autoincrement.asp">http://www.w3schools.com/sql/sql_autoincrement.asp</a></p>
		</td>
	</tr>
</tbody></table> 

<p>This concludes the <strong>CUBRID Serials tutorial</strong>. Please let us know your feedback and remember to periodically check the CUBRID web site – <a href="/tutorials">www.cubrid.org/tutorials</a> - for more tutorials and resources.</p>]]></description>
                        <pubDate>Wed, 15 Jun 2011 19:14:03 -0800</pubDate>
                        <category>serials</category>
                        <category>AUTO_INCREMENT</category>
                        <category>AUTO_DECREMENT</category>
                        <category>triggers</category>
                                </item>
            </channel>
</rss>
