This creates a table with a column called 'id', which has an integer datatype (AUTO_INCREMENT needs that), and since the column is defined as auto_increment, this means that the sequence happens automatically. Every time you insert records into the table, if you insert nothing in the 'id' column, it will automatically place a number that is 1 value higher than the number in the previous record, starting with the number 1, if no number is specified for the first record. If you insert a number in that column, such as 1000, then the next record will automatically insert 1001, unless otherwise specified.
This is not a true SEQUENCE tool, such as that in PostgreSQL. It only handles numbers, and it only increments by 1, but without too much trouble, you can create other sequences based on auto_increment values, if needed.
The most common usage for auto_increment is to define a unique primary key for your table:
CREATE TABLE users (
id int(11) NOT NULL auto_increment,
username tinytext NOT NULL,
password tinytext NOT NULL,
PRIMARY KEY (id),
UNIQUE id (id),
KEY id_2 (id)
);
This table creation statement uses the 'id' column as a primary key, constrains it to unique values, and indexes the column for speed, so that SELECTs based on the user's ID happen quite fast.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.