Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a MySQL schema, what is the meaning of "AUTO_INCREMENT=3"

Tags:

sql

mysql

This is a schema of a MySQL table creation. What is the meaning of AUTO_INCREMENT=3 on the last line?

CREATE TABLE IF NOT EXISTS `test_b` (
  `ID_b` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `stuff` varchar(1000) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`ID_b`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
like image 782
JDelage Avatar asked Aug 13 '12 21:08

JDelage


2 Answers

That sets the initial AUTO_INCREMENT value to 3. So rather than starting at 1 by default for the first inserted record, it will start at 3.

From the manual on CREATE TABLE Syntax:

The initial AUTO_INCREMENT value for the table.
For engines that support the AUTO_INCREMENT table option in CREATE TABLE statements, you can also use ALTER TABLE tbl_name AUTO_INCREMENT = N to reset the AUTO_INCREMENT value. The value cannot be set lower than the maximum value currently in the column.

I've heard cases where people set this value to something large from the beginning to make it seem like they have more content than they really do in the beginning. E.g. user ID 15,000 vs user ID 1.

like image 117
drew010 Avatar answered Oct 01 '22 21:10

drew010


Apparently it's used to set the starting autoincremented number:

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

like image 28
ApplePie Avatar answered Oct 01 '22 20:10

ApplePie