Chapter 11. Generating and Using Sequences
Introduction
A sequence is a set of integers 1, 2, 3, ... that are generated in order on demand. Sequences are frequently used in databases because many applications require each row in a table to contain a unique value, and sequences provide an easy way to generate them. This chapter describes how to use sequences in MySQL. It covers the following topics:
Using AUTO_INCREMENT columns to create sequences.
The
AUTO_INCREMENTcolumn is MySQL’s mechanism for generating a sequence over a set of rows. Each time you create a row in a table that contains anAUTO_INCREMENTcolumn, MySQL automatically generates the next value in the sequence as the column’s value. This value serves as a unique identifier, making sequences an easy way to create items such as customer ID numbers, shipping package waybill numbers, invoice or purchase order numbers, bug report IDs, ticket numbers, or product serial numbers.Retrieving sequence values.
For many applications, it’s not enough just to create sequence values. It’s also necessary to determine the sequence value for a just-inserted record. A web application may need to redisplay to a user the contents of a record created from the contents of a form just submitted by the user. Or the value may need to be retrieved so it can be stored as part of other records in a related table.
Resequencing techniques.
This section describes how to renumber a sequence that has holes in it due to record deletions—and also discusses ...