Problem Solution Discussion Choosing the Type for a Sequence Column

This happens because when you creat e an AUTO_INCREMENT colum n, you m ust declare it t o be eit her a PRIMARY KEY or a UNIQUE index. As a result , it cannot cont ain duplicat e values. • I f t he value is not present in t he t able, MySQL insert s t he record using t hat value. I n addit ion, if it s larger t han t he current sequence count er, t he t ables count er is reset t o t he new value plus one. The insect t able at t his point has sequence values 1 t hrough 8. I f you insert a new row wit h t he id colum n set t o 20, t hat becom es t he new m axim um value. Subsequent insert s t hat aut om at ically generat e id values will begin at 21. The values 9 t hrough 19 becom e unused, result ing in a gap in t he sequence. Now let s look in m ore det ail at how t o define AUTO_INCREMENT colum ns and how t hey behave.

11.4 Choosing the Type for a Sequence Column

11.4.1 Problem

You want t o know m ore about how t o define a sequence colum n.

11.4.2 Solution

Use t he guidelines given here.

11.4.3 Discussion

You should follow cert ain guidelines when creat ing an AUTO_INCREMENT colum n. As an illust rat ion, consider how t he id colum n in t he insect t able w as declared: id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY id The AUTO_INCREMENT keyword inform s MySQL t hat it should generat e successive sequence num bers for t he colum ns values, but t he ot her inform at ion is im port ant , t oo: • INT is t he colum ns basic t ype. You need not necessarily use INT , but t he colum n m ust be one of t he int eger t ypes: TINYINT , SMALLINT , MEDIUMINT , INT , or BIGINT . I t s im port ant t o rem em ber t hat AUTO_INCREMENT is a colum n at t ribut e t hat should be applied only t o int eger t ypes. Older versions of MySQL will allow you t o creat e an AUTO_INCREMENT colum n using non- int eger t ypes such as CHAR , but bad t hings will happen if you do t hat . Even if t he init ial sequence num bers appear t o be generat ed norm ally, sooner or lat er t he colum n will fail. A t ypical error is duplicat e key aft er insert ing a few records, even when you know t he colum n should be able t o hold m ore num bers. Save yourself som e t rouble—always use an int eger t ype for AUTO_INCREMENT colum ns. • The colum n is declared as UNSIGNED . Theres no need t o allow negat ive values, because AUTO_INCREMENT sequences consist only of posit ive int egers norm ally beginning at 1 . Furt herm ore, not declaring t he colum n t o be UNSIGNED cut s t he range of your sequence in half. For exam ple, TINYINT has a range of - 128 t o 127. Sequences include only posit ive values, so t he range of a TINYINT sequence would be 1 t o 127. The range of an unsigned TINYINT colum n is 0 t o 255, which increases t he upper end of t he sequence t o 255. The m axim um sequence value is det erm ined by t he specific int eger t ype used, so you should choose a t ype t hat is big enough t o hold t he largest value youll need. The m axim um unsigned value of each int eger t ype is shown in t he following t able, which you can use t o select an appropriat e t ype. Colu m n t ype M a x im u m u n sign e d va lu e TINYINT 255 SMALLINT 65,535 MEDIUMINT 16,777,215 INT 4,294,967,295 BIGINT 18,446,744,073,709,551,615 • Som et im es people om it UNSIGNED so t hat t hey can creat e records t hat cont ain negat ive num bers in t he sequence colum n. Using -1 t o signify has no I D w ould be an inst ance of t his. MySQL m akes no guarant ees about how negat ive num bers will be t reat ed, so youre playing wit h fire if you t ry t o use t hem in an AUTO_INCREMENT colum n. For exam ple, if you resequence t he colum n, youll find t hat all your negat ive values get t urned int o regular posit ive sequence num bers. • AUTO_INCREMENT colum ns cannot cont ain NULL values, so id is declared as NOT NULL . I t s t rue t hat you can specify NULL as t he colum n value w hen you insert a new record, but for an AUTO_INCREMENT colum n t hat really m eans generat e t he next sequence value. Current versions of MySQL aut om at ically define AUTO_INCREMENT colum ns as NOT NULL if you forget t o. However, it s best t o indicat e NOT NULL in t he CREATE TABLE st at em ent explicit ly if t here is a possibilit y t hat you m ight use it wit h an older version of MySQL som et im e. • The colum n is declared as a PRIMARY KEY t o ensure t hat it s values are unique. Tables can have only one PRIMARY KEY , so if t he t able already has som e ot her PRIMARY KEY colum n, you can declare an AUTO_INCREMENT colum n t o have a UNIQUE index inst ead: • id INT UNSIGNED NOT NULL AUTO_INCREMENT, UNIQUE id I f t he AUTO_INCREMENT colum n is t he only colum n in t he PRIMARY KEY or UNIQUE index, you can declare it as such in t he colum n definit ion rat her t han in a separat e clause. For exam ple, t hese definit ions are equivalent : id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY id id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY As are t hese: id INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE id INT UNSIGNED NOT NULL AUTO_INCREMENT, UNIQUE id Using a separat e clause t o specify t he index helps t o em phasize t hat it s not , st rict ly speaking, part of t he colum n definit ion. I f you read t hrough Chapt er 8 , youll not ice t hat m odifying a colum ns indexes is discussed separat ely from changing t he definit ion of t he colum n it self. When creat ing a t able t hat cont ains an AUTO_INCREMENT colum n, it s also im port ant t o consider t he t able t ype MyI SAM, I nnoDB, and so fort h . The t ype affect s behaviors such as reuse of values t hat are delet ed from t he t op of t he sequence, and w het her or not you can set t he init ial sequence value. I n general, MyI SAM is t he best t ype for t ables t hat cont ain AUTO_INCREMENT colum ns, because it offers t he m ost feat ures for sequence m anagem ent . This will becom e apparent as you cont inue t hrough t he chapt er.

11.5 The Effect of Record Deletions on Sequence Generation