Madmike's Blog

September 7, 2009

How to make sequences in MySql 5.1+

Filed under: Java,Mysql — Florescu Radu @ 20:10
Tags: , ,

Mysql doesn’t support sequences as Oracle and MsSQL.But we can simulate this using some implementations i found over the internet and improuved.

There are 2 options to do this thing:

1.Using an auxiliary table named sequence

  • create a table named sequence in your database
  • table sequence (id:BIGINT)
  • set the id with value equal to your start value (default)
  • every time you need a new id execute a transaction like this

select id from sequence

update sequence set id=id+1

  • every time a thread asks for an id a distinct long will be auto incremented

This procedure was tested on multiple threads concurrently(over 1000000),it depends on your settings of your MySql.

1 Comment »

  1. Really nice posts. I will be checking back here regularly.

    Comment by Kelli Garner — September 30, 2009 @ 16:12 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: