<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hussain Fakhruddin, India Weblog &#187; mysql</title>
	<atom:link href="http://blog.hussulinux.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hussulinux.com</link>
	<description>Things should be simple. But not any simpler!</description>
	<lastBuildDate>Tue, 07 Sep 2010 19:04:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating sequences in MySQL an alternative to AUTO_INCREMENT</title>
		<link>http://blog.hussulinux.com/2009/01/creating-sequence-mysql-without-auto-increment/</link>
		<comments>http://blog.hussulinux.com/2009/01/creating-sequence-mysql-without-auto-increment/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 11:47:13 +0000</pubDate>
		<dc:creator>Hussain Fakhruddin</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.hussulinux.com/?p=85</guid>
		<description><![CDATA[MySQL doesn&#8217;t doesn&#8217;t have sequences and Auto_Increment does not suffice the need for a text based sequence generator. Or say you want to reset AUTO_INCREMENT&#8217;s value?
So here&#8217;s a workaround for a generating a sequence in MySQL which can be reset later.
Step 1: Create a Table
CREATE TABLE IF NOT EXISTS `seqgen` (
`seqno` int(4) unsigned NOT NULL,
`application_id` [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL doesn&#8217;t doesn&#8217;t have sequences and Auto_Increment does not suffice the need for a text based sequence generator. Or say you want to reset AUTO_INCREMENT&#8217;s value?</p>
<p>So here&#8217;s a workaround for a generating a sequence in MySQL which can be reset later.</p>
<p><strong>Step 1: Create a Table</strong></p>
<p>CREATE TABLE IF NOT EXISTS `seqgen` (<br />
`seqno` int(4) unsigned NOT NULL,<br />
`application_id` varchar(20) NOT NULL<br />
) ENGINE=MyISAM DEFAULT CHARSET=latin1;</p>
<p><strong>Step 2: Add your application in the sequence</strong><br />
INSERT INTO `seqgen` (`seqno`, `application_id`) VALUES (0, &#8216;myappname&#8217;);<br />
Now when inserting I have set the seqno = 0 , because I want my sequence to start with 0;</p>
<p><strong>Step 3:  You can create a Stored Proc or a Function to get the value and increment it by 1</strong><br />
&lt;Remember to add this Proc/Function through command line with delimiter, else it will not work&gt;</p>
<p>delimiter //<br />
CREATE PROCEDURE seq_gen( OUT nextval INT , IN applicationid text)<br />
BEGIN<br />
select seqno into nextval from seqgen where application_id = applicationid;<br />
update seqgen SET seqno = seqno + 1 where application_id = applicationid;<br />
commit;<br />
END;<br />
//</p>
<p>&#8212;OR Function &#8212;- If you use function note that you&#8217;ll have to commit it by your program, MySQL doesn&#8217;t allow implicit or explicit commit in a Stored Function. So better use Procedure..</p>
<p>delimiter //<br />
CREATE FUNCTION f_seq_gen(applicationid text)<br />
RETURNS INT<br />
BEGIN<br />
DECLARE<br />
nextval INT;<br />
select seqno into nextval from seqgen where application_id = applicationid;<br />
update seqgen SET seqno = seqno + 1 where application_id = applicationid;<br />
RETURN nextva;<br />
END;<br />
//</p>
<p><strong>Step 4: Access your sequence by </strong></p>
<p>select f_seq_gen(&#8216;myappname&#8221;);</p>
<p>Now you can use many such applications as Sequences in MySQL</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hussulinux.com/2009/01/creating-sequence-mysql-without-auto-increment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
