Recipe 11.2. Choosing the Data Type for a Sequence Column


Problem

You want to know more about how to define a sequence column.

Solution

Use the guidelines given here.

Discussion

You should follow certain guidelines when creating an AUTO_INCREMENT column. As an illustration, consider how the id column in the insect table was declared:

id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) 

The AUTO_INCREMENT keyword informs MySQL that it should generate successive sequence numbers for the column's values, but the other information is important, too:

  • INT is the column's base data type. You need not necessarily use INT, but the column must be one of the integer types: TINYINT, SMALLINT, MEDIUMINT, INT, or BIGINT. It's important to remember that AUTO_INCREMENT is a column attribute that should be applied only to integer types.

  • The column is declared as UNSIGNED to disallow negative values. This is not a requirement for an AUTO_INCREMENT column. However, there is no reason to allow negative values because sequences consist only of positive integers (normally beginning at 1). Furthermore, not declaring the column to be UNSIGNED cuts the range of your sequence in half. For example, TINYINT has a range of 128 to 127. Sequences include only positive values, so the range of a TINYINT sequence would be 1 to 127. The range of a TINYINT UNSIGNED column is 0 to 255, which increases the upper end of the sequence to 255. The maximum sequence value is determined by the specific integer type used, so you should choose a type that is big enough to hold the largest value you'll need. The maximum unsigned value of each integer type is shown in the following table, which you can use to select an appropriate type.

    Data typeMaximum unsigned value
    TINYINT 255
    SMALLINT 65,535
    MEDIUMINT 16,777,215
    INT 4,294,967,295
    BIGINT 18,446,744,073,709,551,615


    Sometimes people omit UNSIGNED so that they can create rows that contain negative numbers in the sequence column. (Using 1 to signify "has no ID" is an instance of this.) This is a bad idea. MySQL makes no guarantees about how negative numbers will be treated in an AUTO_INCREMENT column, so you're playing with fire if you try to use them. For example, if you resequence the column, you'll find that all your negative values get turned into positive sequence numbers.

  • AUTO_INCREMENT columns cannot contain NULL values, so id is declared as NOT NULL. (It's true that you can specify NULL as the column value when you insert a new row, but for an AUTO_INCREMENT column, that really means "generate the next sequence value.") MySQL automatically defines AUTO_INCREMENT columns as NOT NULL if you forget to.

  • AUTO_INCREMENT columns must be indexed. Normally, because a sequence column exists to provide unique identifiers, you use a PRIMARY KEY or UNIQUE index to enforce uniqueness. Tables can have only one PRIMARY KEY, so if the table already has some other PRIMARY KEY column, you can declare an AUTO_INCREMENT column to have a UNIQUE index instead:

    id INT UNSIGNED NOT NULL AUTO_INCREMENT, UNIQUE (id) 

    If the AUTO_INCREMENT column is the only column in the PRIMARY KEY or UNIQUE index, you can declare it as such in the column definition rather than in a separate clause. For example, these definitions are equivalent:

    id INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) 

    id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY 

    As are these:

    id INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE 

    id INT UNSIGNED NOT NULL AUTO_INCREMENT, UNIQUE (id) 

    Using a separate clause to specify the index helps to emphasize that it's not, strictly speaking, part of the column definition.

When you create a table that contains an AUTO_INCREMENT column, it's also important to consider which storage engine to use (MyISAM, InnoDB, and so forth). The engine affects behaviors such as reuse of values that are deleted from the top of the sequence and whether you can set the initial sequence value. In general, MyISAM is the best storage engine for tables that contain AUTO_INCREMENT columns because it offers the greatest flexibility for sequence management. This will become apparent in the rest of the chapter.




MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2004
Pages: 375
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net