# AUTO_INCREMENT Attribute
+ AUTO_INCREMENT is an attribute used to automatically populate the default column value
  + For performance reasons, AUTO_INCREMENT numbers are assigned to each TiDB server instance in batches
  + Cache batch size can be controlled by the AUTO_ID_CACHE table option
  + To get the value used in the last insert operation from function LAST_INSERT_ID()
  + To get the next value from the auto_increment column of table information_schema.tables
  + AUTO_INCREMENT may lead to hot spot issue

![`smart`](./diagram/2013E041_en_001_20230407.png)

------------------------------------------------------------------------------
Class demo scripts:

https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/07-demo-auto-increment-01-show.sql

https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/07-demo-auto-increment-02-show.sh

https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/07-demo-auto-increment-03-show.sh

Try the following statements and obverse the details.

tidb:4000> DROP TABLE IF EXISTS test.ar;
Query OK, 0 rows affected (1.08 sec)

tidb:4000> 
tidb:4000> CREATE TABLE test.ar (id BIGINT PRIMARY KEY AUTO_INCREMENT, name CHAR(30));
Query OK, 0 rows affected (0.58 sec)

tidb:4000> 
tidb:4000> INSERT INTO test.ar (NAME) VALUES ('AAA');
Query OK, 1 row affected (0.01 sec)

tidb:4000> 
tidb:4000> INSERT INTO test.ar (NAME) VALUES ('BBB');
Query OK, 1 row affected (0.00 sec)

tidb:4000> 
tidb:4000> INSERT INTO test.ar (NAME) VALUES ('CCC');
Query OK, 1 row affected (0.00 sec)

tidb:4000> 
tidb:4000> SELECT * FROM test.ar;
+----+------+
| id | name |
+----+------+
| 1 | AAA |
| 2 | BBB |
| 3 | CCC |
+----+------+
3 rows in set (0.00 sec)

tidb:4000> 
tidb:4000> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 3 |
+------------------+
1 row in set (0.00 sec)

tidb:4000> 
tidb:4000> SELECT auto_increment FROM information_schema.tables WHERE table_name='ar' AND table_schema='test'\G
*************************** 1. row ***************************
auto_increment: 4
1 row in set (0.01 sec)

