Problem Solution Discussion Ensuring That Rows Are Renumbered in a Particular Order

wit h no addit ional act ion on your part . For MyI SAM or I nnoDB t ables, issue t he following st at em ent : ALTER TABLE tbl_name AUTO_INCREMENT = 1 This causes MySQL t o reset t he sequence count er down as far as it can for creat ing new records in t he fut ure. You can use ALTER TABLE t o reset t he sequence count er if a sequence colum n cont ains gaps in t he m iddle, but doing so st ill will reuse only values delet ed from t he t op of t he sequence. I t will not elim inat e t he gaps. Suppose you have a t able wit h sequence values from 1 t o 10 and t hen delet e t he records for values 3, 4, 5, 9, and 10. The m axim um rem aining value is 8, so if you use ALTER TABLE t o reset t he sequence count er, t he next record will be given a value of 9, not 3. To resequence a t able and elim inat e t he gaps as well, see Recipe 11.9 .

11.11 Ensuring That Rows Are Renumbered in a Particular Order

11.11.1 Problem

You resequenced a colum n, but MySQL didnt num ber t he rows t he way you want .

11.11.2 Solution

Select t he rows int o anot her t able, using an ORDER BY clause t o place t hem in t he order you want , and let MySQL num ber t hem as it perform s t he operat ion. Then t he rows will be num bered according t o t he sort order.

11.11.3 Discussion

When you resequence an AUTO_INCREMENT colum n, MySQL is free t o pick t he rows from t he t able in any order, so it wont necessarily renum ber t hem in t he order t hat you expect . This doesnt m at t er at all if your only requirem ent is t hat each row have a unique ident ifier. But you m ay have an applicat ion for which it s im port ant t hat t he rows be assigned sequence num bers in a part icular order. For exam ple, you m ay want t he sequence t o correspond t o t he order in which rows were creat ed, as indicat ed by a TIMESTAMP colum n. To assign num bers in a part icular order, use t his procedure: 1. Creat e an em pt y clone of t he t able. 2. Copy rows from t he original int o t he clone using INSERT INTO ... SELECT . Copy all colum ns except t he sequence colum n, using an ORDER BY clause t o specify t he order in which rows are copied and t hus assigned sequence num bers . 3. Drop t he original t able and renam e t he clone t o have t he original t ables nam e. 4. I f t he t able is large and has m ult iple indexes, it will be m ore efficient t o creat e t he new t able init ially wit h no indexes except t he one on t he AUTO_INCREMENT colum n. Then copy t he original t able int o t he new t able and add t he rem aining indexes aft erward. An alt ernat e procedure: 1. Creat e a new t able t hat cont ains all t he colum ns of t he original t able except t he AUTO_INCREMENT colum n. 2. Use INSERT INTO ... SELECT t o copy t he non- AUTO_INCREMENT colum ns from t he original t able int o t he new t able. 3. Delet e t he rows from t he original t able, and reset t he sequence count er t o 1 if necessary. 4. Copy rows from t he new t able back t o t he original t able, using an ORDER BY clause t o sort rows int o t he order in which you want sequence num bers assigned. For inform at ion on creat ing a clone t able, see Recipe 3.26 . I f youre using one of t hese procedures from w it hin a program t hat doesnt necessarily have any prior knowledge about t he st ruct ure of t he t able, use t he t able m et adat a t o get a list of colum n nam es and t o det erm ine which colum n has t he AUTO_INCREMENT at t ribut e. See Recipe 9.6 .

11.12 Starting a Sequence at a Particular Value