Problem Solution Discussion Using AUTO_INCREMENT To Set Up a Sequence Column

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

11.2.1 Problem

You want t o include a sequence colum n in a t able.

11.2.2 Solution

Use an AUTO_INCREMENT colum n.

11.2.3 Discussion

This sect ion provides t he basic background on how AUTO_INCREMENT colum ns work, beginning wit h a short exam ple t hat dem onst rat es t he sequence- generat ion m echanism . The illust rat ion cent ers around a bug- collect ion scenario: your son eight - year-old Junior is assigned t he t ask of collect ing insect s for a class proj ect at school. For each insect , Junior is t o record it s nam e ant , bee, and so fort h , and it s dat e and locat ion of collect ion. You have long expounded t he benefit s of MySQL for record- keeping t o Junior since his early days, so upon your arrival hom e from work t hat day, he im m ediat ely announces t he necessit y of com plet ing t his proj ect and t hen, looking you st raight in t he eye, declares t hat it s clearly a t ask for which MySQL is well- suit ed. Who are you t o argue? So t he t wo of you get t o work. Junior already collect ed som e specim ens aft er school while wait ing for you t o com e hom e and has recorded t he following inform at ion in his not ebook: Name Date Origin millipede 2001-09-10 driveway housefly 2001-09-10 kitchen grasshopper 2001-09-10 front yard stink bug 2001-09-10 front yard cabbage butterfly 2001-09-10 garden ant 2001-09-10 back yard ant 2001-09-10 back yard millbug 2001-09-10 under rock Looking over Juniors not es, youre pleased t o see t hat even at his t ender age he has learned t o writ e dat es in I SO form at . However, you also not ice t hat hes collect ed a m illipede and a m illbug, neit her of which act ually are insect s. You decide t o let t his pass for t he m om ent ; Junior forgot t o bring hom e t he writ t en inst ruct ions for t he proj ect , so at t his point it s unclear whet her or not t hese specim ens are accept able. As you consider how t o creat e a t able t o st ore t his inform at ion, it s apparent t hat you need at least name , date , and origin colum ns corresponding t o t he t ypes of inform at ion Junior is required t o record: CREATE TABLE insect name VARCHAR30 NOT NULL, type of insect date DATE NOT NULL, date collected origin VARCHAR30 NOT NULL where collected ; However, t hose colum ns m ay not be enough t o m ake t he t able easy t o use. Not e t hat t he records collect ed t hus far are not unique—bot h ant s were collect ed at t he sam e t im e and place. I f you put t he inform at ion int o an insect t able t hat has t he preceding st ruct ure, neit her ant record can be referred t o individually, because t heres not hing t o dist inguish t hem from one anot her. Unique I Ds would be helpful t o m ake t he records dist inct and t o provide values t hat m ake each record easy t o refer t o. An AUTO_INCREMENT colum n is good for t his purpose, so a bet t er insect t able has a st ruct ure like t his: CREATE TABLE insect id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY id, name VARCHAR30 NOT NULL, type of insect date DATE NOT NULL, date collected origin VARCHAR30 NOT NULL where collected ; Go ahead and creat e t he insect t able using t his second definit ion. Lat er, in Recipe 11.4 , well discuss t he specifics of why t he id colum n is declared t he way it is.

11.3 Generating Sequence Values