sql code

4 Simple Steps to Emulating Real Sequences in MySQL

Submitted by intellicon on
If all you want to do is create a unique ID for a record, the auto_increment feature in MySQL will do the job nicely. Sometimes, however, you need more functionality. Unfortunately, unlike Oracle or PostgreSQL, MySQL doesn't have native support for the nextval() function, so creating real sequences is a little more challenging. First, we need to define what we want the nextval() function to achieve. We want to be able to begin the sequence with any number, and auto increment in multiples of any number, not just 1, including negative integers (-1 for example).