如果创建表时同时设置了主键,OceanBase 数据库会默认创建一个唯一索引。以下面的 SQL 为例:
创建索引的 SQL 语法格式如下:
在 MySQL 租户里,新增索引还有一种方法,SQL 语法格式如下:
- 示例:对分区表新增索引
obclient> create table t3(
id bigint not null primary KEY
, name varchar(50)
, gmt_create timestamp not null default current_timestamp
) partition by hash(id) partitions 8;
Query OK, 0 rows affected (0.14 sec)
obclient> alter table t3 add unique key t3_uk (name) local;
obclient> alter table t3
add unique key t3_uk (name, id) LOCAL
, add key t3_ind3(gmt_create) global;
Query OK, 0 rows affected (18.03 sec)
obclient> show indexes from t3;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
| t3 | 0 | PRIMARY | 1 | id | A | NULL | NULL | NULL | | BTREE | available | | YES |
| t3 | 0 | t3_uk | 1 | name | A | NULL | NULL | NULL | YES | BTREE | available | | YES |
| t3 | 0 | t3_uk | 2 | id | A | NULL | NULL | NULL | | BTREE | available | | YES |
| t3 | 1 | t3_ind3 | 1 | gmt_create | A | NULL | NULL | NULL | | BTREE | available | | YES |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+---------+
4 rows in set (0.01 sec)
删除索引
删除索引的语法格式如下:
- 示例:删除表的索引