MySQL Forums
Forum List  »  Oracle

Re: How to generate sequence in MySql
Posted by: Andy Salnikov
Date: January 23, 2009 05:38PM

I had just to implement "sequence" myself, here is my example (I did check few other examples but could not find one that has concurrency support, so I chose to invent one more). I did not try to emulate exactly Oracle or Postgres syntax for sequences, I only need one sequence per database. Anyway the result is pretty close to what Postgres does.

And it uses auto-increment column for implementation, of course, as this is the only way in mysql to satisfy concurrency requirements. Here it is:


delimiter %%

drop table if exists R_ObjectId_seq_tbl %%
create table R_ObjectId_seq_tbl ( nextval bigint not null primary key auto_increment ) engine = MyISAM %%

drop function if exists R_ObjectId_nextval %%
create function R_ObjectId_nextval()
returns bigint
begin
insert into R_ObjectId_seq_tbl values (NULL) ;
set @R_ObjectId_val=LAST_INSERT_ID() ;
delete from R_ObjectId_seq_tbl ;
return @R_ObjectId_val ;
end
%%

drop function if exists R_ObjectId_currval %%
create function R_ObjectId_currval()
returns bigint
begin
return @R_ObjectId_val ;
end
%%

Options: ReplyQuote


Subject
Views
Written By
Posted
167288
March 13, 2007 08:05AM
53555
March 26, 2007 09:31PM
30747
April 04, 2007 03:00AM
26383
November 21, 2007 03:50AM
17577
February 06, 2008 12:16PM
18368
October 15, 2008 01:03PM
12284
November 05, 2008 09:50AM
14100
November 10, 2008 06:32AM
15525
December 14, 2008 07:08AM
Re: How to generate sequence in MySql
18302
January 23, 2009 05:38PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.