如果创建表时同时设置了主键,OceanBase 数据库会默认创建一个唯一索引。以下面的 SQL 为例:

    创建索引的 SQL 语法格式如下:

    在 MySQL 租户里,新增索引还有一种方法,SQL 语法格式如下:

    • 示例:对分区表新增索引
    1. obclient> create table t3(
    2. id bigint not null primary KEY
    3. , name varchar(50)
    4. , gmt_create timestamp not null default current_timestamp
    5. ) partition by hash(id) partitions 8;
    6. Query OK, 0 rows affected (0.14 sec)
    7. obclient> alter table t3 add unique key t3_uk (name) local;
    8. obclient> alter table t3
    9. add unique key t3_uk (name, id) LOCAL
    10. , add key t3_ind3(gmt_create) global;
    11. Query OK, 0 rows affected (18.03 sec)
    12. obclient> show indexes from t3;
    13. +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
    14. | t3 | 0 | PRIMARY | 1 | id | A | NULL | NULL | NULL | | BTREE | available | | YES |
    15. | t3 | 0 | t3_uk | 1 | name | A | NULL | NULL | NULL | YES | BTREE | available | | YES |
    16. | t3 | 0 | t3_uk | 2 | id | A | NULL | NULL | NULL | | BTREE | available | | YES |
    17. | t3 | 1 | t3_ind3 | 1 | gmt_create | A | NULL | NULL | NULL | | BTREE | available | | YES |
    18. +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
    19. 4 rows in set (0.01 sec)

    删除索引

    删除索引的语法格式如下:

    • 示例:删除表的索引