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:
INTis the column’s base data type. You need not necessarily useINT, but the column must be one of the integer types:TINYINT,SMALLINT,MEDIUMINT,INT, orBIGINT. It’s important to remember thatAUTO_INCREMENTis a column attribute that should be applied only to integer types.The column is declared as
UNSIGNEDto disallow negative values. This is not a requirement for anAUTO_INCREMENTcolumn. 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 beUNSIGNEDcuts the range of your sequence in half. For example,TINYINThas a range of –128 to 127. Sequences include only positive values, so the range of aTINYINTsequence would be 1 to 127. The range of aTINYINTUNSIGNEDcolumn 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access