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

    为表增加索引可以通过 CREATE INDEX 语句。OceanBase 能在普通表和分区表上创建索引,索引可以是本地索引或者全局索引。同时索引可以是唯一索引或者普通索引,如果是分区表的唯一索引,唯一索引必须包含表分区的拆分键。

    MySQL 租户里,索引名称在表范围内不能重复,查看索引可以通过命令 SHOW INDEXES 。

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

    • 示例:对分区表新增索引
    1. obclient> create table t1(id bigint not null primary key ,name varchar(50) not null);
    2. Query OK, 0 rows affected (0.06 sec)
    3. obclient> show indexes from t1;
    4. *************************** 1. row ***************************
    5. Table: t1
    6. Non_unique: 0
    7. Key_name: PRIMARY
    8. Seq_in_index: 1
    9. Column_name: id
    10. Cardinality: NULL
    11. Packed: NULL
    12. Null:
    13. Index_type: BTREE
    14. Comment: available
    15. Index_comment:
    16. Visible: YES
    17. 1 row in set (0.00 sec)
    18. obclient> create table t3(
    19. id bigint not null primary KEY
    20. , name varchar(50)
    21. , gmt_create timestamp not null default current_timestamp
    22. obclient> alter table t3 add unique key t3_uk (name) local;
    23. ERROR 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function
    24. obclient> alter table t3
    25. add unique key t3_uk (name, id) LOCAL
    26. , add key t3_ind3(gmt_create) global;
    27. Query OK, 0 rows affected (18.03 sec)
    28. obclient> show indexes from t3;

    删除索引

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

    1. obclient> alter table t3 drop key t3_uk, drop key t3_ind3;
    2. Query OK, 0 rows affected (0.07 sec)