numbers

MySQL Logo

MySQL Procedure To Get Incremental Numbers

9th March 2009 - 2 minutes read time

Run the following table definition to create a table called tests in the test database.

DROP TABLE IF EXISTS test.tests;
CREATE TABLE test.tests (
 id int(10) unsigned NOT NULL auto_increment,
 var varchar(45) NOT NULL,
 PRIMARY KEY  (id)
);

Fill it with some default values:

INSERT INTO tests(var) VALUES('one'), ('two'), ('three'), ('four');

Now lets say that you want to find the values in the table, but that you want to increment a value for each row, the following procedure will do this. If you are using the MySQL Query browser then open a script table and past the following and execute, changing any database/table definitions.