# Primary Key And Index

__*_tidb_rowid*__
__*CLUSTERED PRIMARY KEY*__
__*NONCLUSTERED PRIMARY KEY*__


+ Primary keys have the following constraints:
  + Uniquely identifies a row in a table
  + NULL values are not allowed in the primary key
  + Only one primary key is allowed per table
  + TiDB creates a non-clustered index on the table if the declared primary key is not of the**INTEGER** data type
    + `_tidb_rowid`: keys for non-clustered indexes
  + If no primary key is declared, TiDB creates an implicit primary key for the table
    + `_tidb_rowid`: implicit keys
+ **Primary Key Clustered Index**: The primary key of a table is a clustered index, and at least one key-value pair is required to store a row
+ **Primary key non-clustered index**: The key of the row data consists of an implicitly allocated **_tidb_rowid**. The primary key is additionally indexed on the**_tidb_rowid**, and at least two key-value pairs are required to store a row

------------------------------------------------------------

Class demo scripts:

https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/07-demo-cluster-index-01-setup.sql

https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/07-demo-cluster-index-02-show.sql


Currently, TiDB tables are divided into the following two categories:

  - CLUSTERED indicates that the primary key of the table is a clustered index. In a clustered index table, the keys of row data consist of user-specified primary key column data, so a clustered index table stores at least one key-value pair for a row, that is
    - primary key column data (key) - row data (value)

  - NONCLUSTERED, indicating that the primary key of the table is a non-clustered index. In a non-clustered index table, the key of the row data consists of the _tidb_rowid implicitly allocated inside TiDB, and the primary key is essentially a unique index, so a non-clustered index table needs at least two key-value pairs to store a row,
    - _tidb_rowid (key) - row data (value)
    - primary key column data (key) - _tidb_rowid (value)

Clustered index (clustered index) is a feature that TiDB supports since v5.0 to control how table data containing primary keys is stored. By using clustered indexes, TiDB can better organize data tables, thereby improving the performance of some queries. Some database management systems also refer to clustered indexes as "index-organized tables" (index-organized tables).
