Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Whilst this doesn't help you on InnoDB, it's worth pointing out that MyISAM natively supports this functionality. As documented under <a href="http://dev.mysql.com/doc/en/example-auto-increment.html" rel="nofollow">Using <code>AUTO_INCREMENT</code></a>:</p> <blockquote> <h3>MyISAM Notes</h3> <p>For <code>MyISAM</code> tables, you can specify <code>AUTO_INCREMENT</code> on a secondary column in a multiple-column index. In this case, the generated value for the <code>AUTO_INCREMENT</code> column is calculated as <a href="http://dev.mysql.com/doc/en/group-by-functions.html#function_max" rel="nofollow"><code>MAX(<em>auto_increment_column</em>) + 1 WHERE prefix=<em>given-prefix</em></code></a>. This is useful when you want to put data into ordered groups.</p> <pre> CREATE TABLE animals ( grp ENUM('fish','mammal','bird') NOT NULL, id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (grp,id) ) ENGINE=MyISAM; INSERT INTO animals (grp,name) VALUES ('mammal','dog'),('mammal','cat'), ('bird','penguin'),('fish','lax'),('mammal','whale'), ('bird','ostrich'); SELECT * FROM animals ORDER BY grp,id; </pre> <p>Which returns:</p> <pre> +--------+----+---------+ | grp | id | name | +--------+----+---------+ | fish | 1 | lax | | mammal | 1 | dog | | mammal | 2 | cat | | mammal | 3 | whale | | bird | 1 | penguin | | bird | 2 | ostrich | +--------+----+---------+ </pre> <p>In this case (when the <code>AUTO_INCREMENT</code> column is part of a multiple-column index), <code>AUTO_INCREMENT</code> values are reused if you delete the row with the biggest <code>AUTO_INCREMENT</code> value in any group. This happens even for <code>MyISAM</code> tables, for which <code>AUTO_INCREMENT</code> values normally are not reused.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload