Re: How to generate sequence in MySql
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
%%
Subject
Views
Written By
Posted
167288
March 13, 2007 08:05AM
73295
March 22, 2007 10:34PM
66826
March 26, 2007 08:49AM
53555
March 26, 2007 09:31PM
30747
April 04, 2007 03:00AM
24396
April 05, 2007 02:37AM
21428
November 10, 2008 06:40AM
26383
November 21, 2007 03:50AM
30096
January 29, 2008 08:33PM
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
8543
March 16, 2009 09:00AM
10300
March 16, 2009 09:18AM
8464
March 16, 2009 09:51AM
9356
March 16, 2009 10:23AM
8298
April 12, 2009 07:58AM
9848
April 03, 2009 03:07PM
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.