How to set initial value and auto increment in MySQL?

bbtang picture bbtang · Sep 28, 2009 · Viewed 575.5k times · Source

How do I set the initial value for an "id" column in a MySQL table that start from 1001?

I want to do an insert "INSERT INTO users (name, email) VALUES ('{$name}', '{$email}')";

Without specifying the initial value for the id column.

Answer

Anatoliy picture Anatoliy · Sep 28, 2009

Use this:

ALTER TABLE users AUTO_INCREMENT=1001;

or if you haven't already added an id column, also add it

ALTER TABLE users ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ADD INDEX (id);