MySQL Tutorial/Table/AUTO INCREMENT

Материал из SQL эксперт
Перейти к: навигация, поиск

For a multiple-row insert, LAST_INSERT_ID() returns the AUTO_INCREMENT key from the first of the inserted rows

   <source lang="sql">

mysql> mysql> mysql> mysql> CREATE TABLE employee (

   ->      id MEDIUMINT NOT NULL AUTO_INCREMENT,
   ->      name CHAR(30) NOT NULL,
   ->      PRIMARY KEY (id)
   ->  );

Query OK, 0 rows affected (0.03 sec) mysql> mysql> INSERT INTO employee (name) VALUES ("A"),

   ->                                    ("B"),
   ->                                    ("C"),
   ->                                    ("D"),
   ->                                    ("E"),
   ->                                    ("F");

Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> mysql> SELECT * FROM employee; +----+------+ | id | name | +----+------+ | 1 | A | | 2 | B | | 3 | C | | 4 | D | | 5 | E | | 6 | F | +----+------+ 6 rows in set (0.02 sec) mysql> mysql> select LAST_INSERT_ID(); +------------------+ | LAST_INSERT_ID() | +------------------+ | 1 | +------------------+ 1 row in set (0.00 sec) mysql> mysql> drop table employee; Query OK, 0 rows affected (0.01 sec) mysql></source>


To start with an AUTO_INCREMENT value other than 1

   <source lang="sql">

mysql> mysql> CREATE TABLE employee (

   ->      id MEDIUMINT NOT NULL AUTO_INCREMENT,
   ->      name CHAR(30) NOT NULL,
   ->      PRIMARY KEY (id)
   ->  );

Query OK, 0 rows affected (0.01 sec) mysql> mysql> desc employee; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | mediumint(9) | NO | PRI | NULL | auto_increment | | name | char(30) | NO | | | | +-------+--------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql> mysql> ALTER TABLE employee AUTO_INCREMENT = 100; Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> INSERT INTO employee (name) VALUES ("A"),

   ->                                    ("B"),
   ->                                    ("C"),
   ->                                    ("D"),
   ->                                    ("E"),
   ->                                    ("F");

Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> mysql> SELECT * FROM employee; +-----+------+ | id | name | +-----+------+ | 100 | A | | 101 | B | | 102 | C | | 103 | D | | 104 | E | | 105 | F | +-----+------+ 6 rows in set (0.00 sec) mysql> mysql> select LAST_INSERT_ID(); +------------------+ | LAST_INSERT_ID() | +------------------+ | 100 | +------------------+ 1 row in set (0.00 sec) mysql> mysql> drop table employee; Query OK, 0 rows affected (0.00 sec) mysql> mysql> mysql></source>


Use Auto_increment column as part of the key

   <source lang="sql">

mysql> mysql> CREATE TABLE employee (

   ->     grp ENUM("A","B","C") NOT NULL,
   ->     id MEDIUMINT NOT NULL AUTO_INCREMENT,
   ->     name CHAR(30) NOT NULL,
   ->     PRIMARY KEY (grp,id)
   -> );

Query OK, 0 rows affected (0.02 sec) mysql> mysql> INSERT INTO employee (grp,name) VALUES ("A","A1"),

   ->                                        ("B","B1"),
   ->                                        ("C","C1"),
   ->                                        ("A","A2"),
   ->                                        ("B","B2"),
   ->                                        ("C","C2");

Query OK, 6 rows affected (0.00 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> mysql> SELECT * FROM employee ORDER BY grp,id; +-----+----+------+ | grp | id | name | +-----+----+------+ | A | 1 | A1 | | A | 2 | A2 | | B | 1 | B1 | | B | 2 | B2 | | C | 1 | C1 | | C | 2 | C2 | +-----+----+------+ 6 rows in set (0.00 sec) mysql> mysql> mysql> drop table employee; Query OK, 0 rows affected (0.00 sec) mysql> mysql></source>


You can retrieve the most recent AUTO_INCREMENT value with the LAST_INSERT_ID().

LAST_INSERT_ID() is connection-specific.



   <source lang="sql">

mysql> mysql> mysql> CREATE TABLE employee (

   ->      id MEDIUMINT NOT NULL AUTO_INCREMENT,
   ->      name CHAR(30) NOT NULL,
   ->      PRIMARY KEY (id)
   ->  );

Query OK, 0 rows affected (0.05 sec) mysql> mysql> desc employee; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | mediumint(9) | NO | PRI | NULL | auto_increment | | name | char(30) | NO | | | | +-------+--------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec) mysql> mysql> INSERT INTO employee (name) VALUES ("A"); Query OK, 1 row affected (0.00 sec) mysql> mysql> SELECT * FROM employee; +----+------+ | id | name | +----+------+ | 1 | A | +----+------+ 1 row in set (0.00 sec) mysql> mysql> select LAST_INSERT_ID(); +------------------+ | LAST_INSERT_ID() | +------------------+ | 1 | +------------------+ 1 row in set (0.00 sec) mysql> mysql> drop table employee; Query OK, 0 rows affected (0.00 sec) mysql> mysql></source>