Introduction Generating and Using Sequences

11.1 Introduction

A sequence is a set of int egers 1, 2, 3, ... t hat are generat ed in order on dem and. Sequences are frequent ly used in dat abases because m any applicat ions require each row in a t able t o cont ain a unique value, and sequences provide an easy way t o generat e t hem . This chapt er describes how t o use sequences in MySQL. I t covers t he following t opics: • Usin g AUTO_ I N CREM EN T colu m n s t o cr e a t e se qu e n ce s. The AUTO_INCREMENT colum n is MySQLs m echanism for generat ing a sequence over a set of rows. Each t im e you creat e a row in a t able t hat cont ains an AUTO_INCREMENT colum n, MySQL aut om at ically generat es t he next value in t he sequence as t he colum ns value. This value serves as a unique ident ifier, m aking sequences an easy way t o creat e it em s such as cust om er I D num bers, shipping package waybill num bers, invoice or purchase order num bers, bug report I Ds, t icket num bers, or product serial num bers. • Re t r ie vin g se qu e n ce va lu e s. For m any applicat ions, it s not enough j ust t o creat e sequence values. I t s also necessary t o det erm ine t he sequence value for a j ust - insert ed record. A web applicat ion m ay need t o redisplay t o a user t he cont ent s of a record creat ed from t he cont ent s of a form j ust subm it t ed by t he user. Or t he value m ay need t o be ret rieved so it can be st ored as part of ot her records in a relat ed t able. • Re se qu e n cin g t e ch n iqu e s. This sect ion describes how t o renum ber a sequence t hat has holes in it due t o record delet ions—and also discusses reasons t o avoid resequencing. Ot her t opics include st art ing sequences at values ot her t han 1 and adding a sequence colum n t o a t able t hat doesnt have one. • Usin g a n AUTO_ I N CREM EN T colu m n t o cr e a t e m u lt iple se qu e n ce s. I n m any cases, t he AUTO_INCREMENT colum n in a t able is independent of ot her colum ns and it s values increm ent t hroughout t he t able in a single m onot onic sequence. However, if you creat e a m ult iple- colum n index t hat cont ains an AUTO_INCREMENT colum n, you can use it t o generat e m ult iple sequences. For exam ple, if you run a bullet in board t hat cat egorizes m essages int o t opics, you can num ber m essages sequent ially wit hin each t opic by t ying an AUTO_INCREMENT colum n t o a t opic indicat or colum n. • M a n a gin g m u lt iple sim u lt a n e ou s AUTO_ I N CREM EN T va lu e s. Special care is necessary when you need t o keep t rack of m ult iple sequence values. This can occur when you issue a set of st at em ent s t hat affect a single t able, or when creat ing records in m ult iple t ables t hat each have an AUTO_INCREMENT colum n. This sect ion describes what t o do in t hese cases. • Usin g sin gle - r ow se qu e n ce ge n e r a t or s. Sequences also can be used as count ers. For exam ple, if you serve banner ads on your web sit e, you m ight increm ent a count er for each im pression t hat is, for each t im e you serve an ad . The count s for a given ad form a sequence, but because t he count it self is t he only value of int erest , t here is no need t o generat e a new row t o record each im pression. MySQL provides a solut ion for t his problem , t oo, using a m echanism t hat allows a sequence t o be easily generat ed wit hin a single t able row over t im e. To st ore m ult iple count ers in t he t able, add a colum n t hat ident ifies t he count er uniquely. For exam ple, you can have an arbit rary num ber of ad im pression count ers in a t able. Each row in t he t able ident ifies a specific banner ad, and t he count er in each row increm ent s independent ly of t he ot hers. The sam e m echanism also allows creat ion of sequences t hat increase by values ot her t han one, by non- uniform values, or even by negat ive increm ent s. • N u m be r in g qu e r y ou t pu t r ow s se qu e n t ia lly. This sect ion suggest s ways t o generat e display- only sequences for t he purpose of num bering t he rows of out put from a query. Sequence Generators and Portability The engines for m ost dat abase syst em s provide sequence generat ion capabilit ies, t hough t he im plem ent at ions t end t o be engine-dependent . That s t rue for MySQL as well, so t he m at erial in t his sect ion is alm ost com plet ely MySQL-specific, even at t he SQL level. I n ot her words, t he SQL for generat ing sequences is it self non- port able, even if you use an API like DBI or JDBC t hat provides an abst ract ion layer. Abst ract int erfaces m ay help you process SQL st at em ent s port ably, but t hey dont m ake nonport able SQL port able.

11.2 Using AUTO_INCREMENT To Set Up a Sequence Column