mysql> CREATE TABLE `T_IDENT` (
-> `id` int(11) NOT NULL AUTO_INCREMENT,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.04 sec)

mysql> desc T_IDENT;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.01 sec)

mysql> INSERT INTO T_IDENT (id) VALUES (NULL);
Query OK, 1 row affected (0.01 sec)

mysql> select * from T_IDENT;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)

mysql> select LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 1 |
+------------------+
1 row in set (0.00 sec)

mysql>

auto_increment_0

他のセッションでデータをINSERTしても、現在のセッションでの最後にINSERTした
値が、LAST_INSERT_ID()には表示される。

auto_increment_1

Comments are closed.

Post Navigation