# `INSERT` a Row Into a Table with Non-Clustered Index for Primary Key

![`smart`](NonClusteredPrimaryKeyTableINSERT01_aa_002_20230905.png)

```7
tidb> DESC t_nonclustered;
+-------+-------------+------+------+---------+-------+
| Field | Type        | Null | Key  | Default | Extra |
+-------+-------------+------+------+---------+-------+
| id    | int(11)     | NO   | PRI  | NULL    |       |
| name  | varchar(20) | YES  |      | NULL    |       |
| tel   | varchar(16) | YES  |      | NULL    |       |
+-------+-------------+------+------+---------+-------+

tidb> SELECT TABLE_NAME, TIDB_PK_TYPE FROM information_schema.tables 
  WHERE table_schema = 'test' AND table_name in ('t_nonclustered');
+----------------+--------------+
| TABLE_NAME     | TIDB_PK_TYPE |
+----------------+--------------+
| t_nonclustered | ~!@NONCLUSTERED@!~ |
+----------------+--------------+

tidb> SELECT _tidb_rowid, id, name, tel FROM t_nonclustered
  ORDER BY _tidb_rowid;
+-------------+----+-------+------------+
| ~!@_tidb_rowid@!~ | id | name  | tel        |
+-------------+----+-------+------------+
|           1 |  1 | Tom   | 7073958726 |
|           2 |  2 | Jack  | 4782733578 |
|           3 |  3 | Frank | 147810546  |
|           4 |  4 | Tony  | 335451143  |
|           5 |  5 | Jim   | 1390811212 |
|           6 |  6 | Sam   | 1579915611 |
+-------------+----+-------+------------+

tidb> INSERT INTO t_nonclustered VALUES (7, 'Andy', 18922118921);
...
```
------------------------------------------------------------------------------
The slide shows a table `t_nonclustered` with 6 rows, which is a table with non-clustered index for primary key. The primary key is on column `id`.

```
CREATE TABLE t_nonclustered (
 id INT PRIMARY KEY NONCLUSTERED,
 name varchar(20),
 tel varchar(16)
);
INSERT INTO t_nonclustered VALUES (1, 'Tom', '7073958726');
INSERT INTO t_nonclustered VALUES (2, 'Jack', '4782733578');
INSERT INTO t_nonclustered VALUES (3, 'Frank', '147810546');
INSERT INTO t_nonclustered VALUES (4, 'Tony', '335451143');
INSERT INTO t_nonclustered VALUES (5, 'Jim', '1390811212');
INSERT INTO t_nonclustered VALUES (6, 'Sam', '1579915611');
```

Database client has issued a `INSERT` statement:
```
INSERT INTO t_nonclustered VALUES (7, 'Andy', 18922118921);
```

Then, three important steps executed by the TiDB server instance in a specific order:
`1.` Queries which TiKV region that has a key range containing the actual key. Note that on the diagram the pointer is special, normally the information is cached in TiDB server, so no actual query is executed from TiDB server to PD server during the INSERT operation.
`2.` Stores the newly created row in the TiKV Region where the table is located.
`3.` Writes the related primary key entry to the TiKV Region where the primary key index is located.
