Indexes are classified into unique and non-unique indexes. Unique indexes ensure that no two rows of a table have duplicate values in an indexed column. Non-unique indexes allow duplicate values in an indexed column. In OceanBase Database, NULL values are also stored in indexes.
Storage keys
Non-unique indexes
Storage keys of a non-unique index table are the user-specified indexed column and the primary key column in the primary table.
Unique indexes
Storage keys of a unique index table are the user-specified indexed column and a variable primary key column in the primary table. "Variable" means that values of the primary key column vary with the values of the indexed column.
- When the indexed column is not
NULL, the variable primary key column does not play a role and the value of the indexed column is used for storage. - When the indexed column is
NULL, the variable primary key column is set to the same value as the primary key column in the primary table. This is used to distinguish between multipleNULLvalues in a unique index table.
- When the indexed column is not
Here is an example:
For the
idx1_tbl1table with a non-unique index, the storage keys arecol2andcol1.For the
idx2_tbl1table with a unique index:- When the indexed column
col3isNULL, the value of the variablecol1column is the same as that of thecol1column in the primary table. The storage key iscol1. - When the indexed column
col3is notNULL, the value of the variablecol1column isNULLand does not play a role. The storage key iscol3.
- When the indexed column
Create the
tbl1table.CREATE TABLE tbl1(col1 INT, col2 INT, col3 INT, col4 INT, PRIMARY KEY(col1));Create the non-unique index
idx1_tbl1.CREATE INDEX idx1_tbl1 ON tbl1(col2);Create the unique index
idx2_tbl1.CREATE UNIQUE INDEX idx2_tbl1 ON tbl1(col3);