Working with SequencesMany applications need to generate unique numbers for identification purposes. The requirement for unique values occurs in a number of contexts: membership numbers, sample or lot numbering, customer IDs, bug report or trouble ticket tags, and so forth. MySQL's mechanism for providing unique numbers is through the AUTO_INCREMENT column attribute, which enables you to generate sequential numbers automatically. However, AUTO_INCREMENT columns are handled somewhat differently by the various storage engines that MySQL supports, so it's important to understand not only the general concepts underlying the AUTO_INCREMENT mechanism, but also the differences between storage engines. This section describes how AUTO_INCREMENT columns work in general and for specific storage engines so that you can use them effectively without running into the traps that sometimes surprise people. It also describes how you can generate a sequence without using an AUTO_INCREMENT column. General AUTO_INCREMENT ConceptsAUTO_INCREMENT columns must be defined according to the following conditions:
Once created, an AUTO_INCREMENT column behaves like this:
AUTO_INCREMENT Handling Per Storage EngineThe general AUTO_INCREMENT characteristics just described form the basis for understanding sequence behavior specific to other storage engines. Most engines implement behavior that for the most part is similar to that just described, so keep the preceding discussion in mind as you read on. AUTO_INCREMENT for MyISAM TablesMyISAM tables offer the most flexibility for sequence handling. The MyISAM storage engine has the following AUTO_INCREMENT characteristics:
The MyISAM storage engine supports the use of composite (multiple-column) indexes for creating multiple independent sequences within the same table. To use this feature, create a multiple-column PRIMARY KEY or UNIQUE index that includes an AUTO_INCREMENT column as its final column. For each distinct key in the leftmost column or columns of the index, the AUTO_INCREMENT column will generate a separate sequence of values. For example, you might use a table named bugs for tracking bug reports of several software projects, where the table is defined as follows: CREATE TABLE bugs ( proj_name VARCHAR(20) NOT NULL, bug_id INT UNSIGNED NOT NULL AUTO_INCREMENT, description VARCHAR(100), PRIMARY KEY (proj_name, bug_id) ) ENGINE = MYISAM; Here, the proj_name column identifies the project name and the description column contains the bug description. The bug_id column is an AUTO_INCREMENT column; by creating an index that ties it to the proj_name column, you can generate an independent series of sequence numbers for each project. Suppose that you enter the following records into the table to register three bugs for SuperBrowser and two for SpamSquisher: mysql> INSERT INTO bugs (proj_name,description) -> VALUES('SuperBrowser','crashes when displaying complex tables'); mysql> INSERT INTO bugs (proj_name,description) -> VALUES('SuperBrowser','image scaling does not work'); mysql> INSERT INTO bugs (proj_name,description) -> VALUES('SpamSquisher','fails to block known blacklisted domains'); mysql> INSERT INTO bugs (proj_name,description) -> VALUES('SpamSquisher','fails to respect whitelist addresses'); mysql> INSERT INTO bugs (proj_name,description) -> VALUES('SuperBrowser','background patterns not displayed'); The resulting table contents are as follows: mysql> SELECT * FROM bugs ORDER BY proj_name, bug_id; +--------------+--------+------------------------------------------+ | proj_name | bug_id | description | +--------------+--------+------------------------------------------+ | SpamSquisher | 1 | fails to block known blacklisted domains | | SpamSquisher | 2 | fails to respect whitelist addresses | | SuperBrowser | 1 | crashes when displaying complex tables | | SuperBrowser | 2 | image scaling does not work | | SuperBrowser | 3 | background patterns not displayed | +--------------+--------+------------------------------------------+ The table numbers the bug_id values for each project separately, regardless of the order in which records are entered for projects. You need not enter all records for one project before you enter records for another. If you use a composite index to create multiple sequences, values deleted from the top of each individual sequence are reused. This contrasts with the usual MyISAM behavior of not reusing values. AUTO_INCREMENT for MEMORY TablesThe MEMORY storage engine has the following AUTO_INCREMENT characteristics:
AUTO_INCREMENT for InnoDB TablesThe InnoDB storage engine has the following AUTO_INCREMENT characteristics:
AUTO_INCREMENT for BDB TablesThe BDB storage engine has the following AUTO_INCREMENT characteristics:
Issues to Consider with AUTO_INCREMENT ColumnsYou should keep the following points in mind to avoid being surprised when you use AUTO_INCREMENT columns:
Tips for Working with AUTO_INCREMENT ColumnsThis section describes some techniques that are useful when working with AUTO_INCREMENT columns. Adding a Sequence Number Column to a TableSuppose that you create a table and put some information into it: mysql> CREATE TABLE t (c CHAR(10)); mysql> INSERT INTO t VALUES('a'),('b'),('c'); mysql> SELECT * FROM t; +------+ | c | +------+ | a | | b | | c | +------+ Then you decide that you want to include a sequence number column in the table. To do this, issue an ALTER TABLE statement to add an AUTO_INCREMENT column, using the same kind of type definition that you'd use with CREATE TABLE: mysql> ALTER TABLE t ADD i INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY; mysql> SELECT * FROM t; +------+---+ | c | i | +------+---+ | a | 1 | | b | 2 | | c | 3 | +------+---+ Note how MySQL assigns sequence values to the AUTO_INCREMENT column automatically. You need not do so yourself. Resequencing an Existing ColumnIf a table already has an AUTO_INCREMENT column, but you want to renumber it to eliminate gaps in the sequence that may have resulted from row deletions, the easiest way to do it is to drop the column and then add it again. When MySQL adds the column, it assigns new sequence numbers automatically. Suppose that a table t looks like this, where i is the AUTO_INCREMENT column: mysql> CREATE TABLE t (c CHAR(10), i INT UNSIGNED AUTO_INCREMENT -> NOT NULL PRIMARY KEY); mysql> INSERT INTO t (c) -> VALUES('a'),('b'),('c'),('d'),('e'),('f'),('g'),('h'),('i'),('j'),('k'); mysql> DELETE FROM t WHERE c IN('a','d','f','g','j'); mysql> SELECT * FROM t; +------+----+ | c | i | +------+----+ | b | 2 | | c | 3 | | e | 5 | | h | 8 | | i | 9 | | k | 11 | +------+----+ The following ALTER TABLE statement drops the column and then adds it again, renumbering the column in the process: mysql> ALTER TABLE t -> DROP i, -> ADD i INT UNSIGNED NOT NULL AUTO_INCREMENT, -> AUTO_INCREMENT = 1; mysql> SELECT * FROM t; +------+---+ | c | i | +------+---+ | b | 1 | | c | 2 | | e | 3 | | h | 4 | | i | 5 | | k | 6 | +------+---+ The AUTO_INCREMENT = 1 clause resets the sequence to begin again at 1. For a MyISAM or MEMORY table (or InnoDB table, as of MySQL 5.0.3), you can use a value other than 1 to begin the sequence at a different value. For other storage engines, just omit the AUTO_INCREMENT clause, because they do not allow the initial value to be specified this way. The sequence will begin at 1. Note that although it's easy to resequence a column, and the question, "How do you do it?" is a common one, there is usually very little need to do so. MySQL doesn't care whether a sequence has holes in it, nor do you gain any performance efficiencies by resequencing. In addition, if you have records in another table that refer to the values in the AUTO_INCREMENT column, resequencing the column destroys the correspondence between tables. Generating Sequences Without AUTO_INCREMENTMySQL supports a method for generating sequence numbers that doesn't use an AUTO_INCREMENT column at all. Instead, it uses an alternative form of the LAST_INSERT_ID() function that takes an argument. If you insert or update a column using LAST_INSERT_ID(expr), the next call to LAST_INSERT_ID() with no argument returns the value of expr. In other words, MySQL treats expr as though it had been generated as an AUTO_INCREMENT value. This allows you to create a sequence number and then retrieve it later in your session, confident that the value will not have been affected by the activity of other clients. One way to use this strategy is to create a single-row table containing a value that you update each time you want the next value in the sequence. For example, you can create and initialize the table like this: CREATE TABLE seq_table (seq INT UNSIGNED NOT NULL); INSERT INTO seq_table VALUES(0); Those statements set up seq_table with a single row containing a seq value of 0. To use the table, generate the next sequence number and retrieve it like this: UPDATE seq_table SET seq = LAST_INSERT_ID(seq+1); SELECT LAST_INSERT_ID(); The UPDATE statement retrieves the current value of the seq column and increments it by 1 to produce the next value in the sequence. Generating the new value using LAST_INSERT_ID(seq+1) causes it to be treated like an AUTO_INCREMENT value, which allows it to be retrieved by calling LAST_INSERT_ID() without an argument. LAST_INSERT_ID() is client-specific, so you get the correct value even if other clients have generated other sequence numbers in the interval between the UPDATE and the SELECT. Other uses for this method are to generate sequence values that increment by a value other than 1, or that are negative. For example, this statement can be executed repeatedly to generate a sequence of numbers that increase by 100 each time: UPDATE seq_table SET seq = LAST_INSERT_ID(seq+100); Repeating the following statement generates a sequence of decreasing numbers: UPDATE seq_table SET seq = LAST_INSERT_ID(seq-1); You also can use this technique to generate a sequence that begins at an arbitrary value, by setting the seq column to an appropriate initial value. The preceding discussion describes how to set up a counter using a table with a single row. That's okay for a single counter. If you want several counters, add another column to the table to server as a counter identifier, and insert a row into the table for each counter. Suppose that you have a Web site and you want to put some "this page has been accessed n times" counters in several pages. Create a table with two columns. One column holds a name that uniquely identifies each counter. The other holds the current counter value. You can still use the LAST_INSERT_ID() function, but you determine which row it applies to by using the counter name. For example, you can create such a table with the following statement: CREATE TABLE counter ( name VARCHAR(255) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL, value INT UNSIGNED, PRIMARY KEY (name) ); The name column is a string so that you can name a counter whatever you want, and it's defined as a PRIMARY KEY to prevent duplicate names. This assumes that applications using the table agree on the names they'll be using. For Web counters, uniqueness of counter names is ensured simply by using the pathname of each page within the document tree as its counter name. The name column has a binary collation to cause pathname values to be treated as case sensitive. (If your system has pathnames that are not case sensitive, use a collation that is not case sensitive.) To use the counter table, insert a row corresponding to each page for which you need a counter. For example, to set up a new counter for the site's home page, do this: INSERT INTO counter (name,value) VALUES('index.html',0); That initializes a counter named 'index.html' with a value of zero. To generate the next sequence value for the page, use its pathname to look up the correct counter value and increment it with LAST_INSERT_ID(expr), and then retrieve the value with LAST_INSERT_ID(): UPDATE counter SET value = LAST_INSERT_ID(value+1) WHERE name = 'index.html'; SELECT LAST_INSERT_ID(); An alternative approach is to increment the counter without using LAST_INSERT_ID(), like this: UPDATE counter SET value = value+1 WHERE name = 'index.html'; SELECT value FROM counter WHERE name = 'index.html'; However, that doesn't work correctly if another client increments the counter after you issue the UPDATE and before you issue the SELECT. You could solve that problem by putting LOCK TABLES and UNLOCK TABLES around the two statements. Or you could create the table as an InnoDB or BDB table and update the table within a transaction. Either method blocks other clients while you're using the counter, but the LAST_INSERT_ID() method accomplishes the same thing more easily. Because its value is client-specific, you always get the value you inserted, not the one from some other client, and you don't have to complicate the code with transactions or locks to keep other clients out. |