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

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

    • 示例:为普通表创建普通索引
    • 示例:为分区表创建唯一的本地索引、普通的全局索引

    1. obclient> create table t3(id number not null primary key, name varchar2(50), gmt_create date not null default sysdate) partition by hash(id) partitions 8;
    2. Query OK, 0 rows affected (0.09 sec)
    3. obclient> create unique index t3_uk on t3(name) local;
    4. obclient> create unique index t3_uk on t3(name, id) local;
    5. obclient> create index t3_ind2 on t3(gmt_create) global ;
    6. Query OK, 0 rows affected (12.48 sec)
    7. obclient> SELECT index_name,index_type,table_owner,table_name,uniqueness,partitioned FROM user_indexes where table_name='T3';
    8. | INDEX_NAME | INDEX_TYPE | TABLE_OWNER | TABLE_NAME | UNIQUENESS | PARTITIONED |
    9. | T3_OBPK_1585822748793678 | NORMAL | TPCC | T3 | UNIQUE | NO |
    10. | T3_UK | NORMAL | TPCC | T3 | UNIQUE | YES |
    11. | T3_IND2 | NORMAL | TPCC | T3 | NONUNIQUE | NO |
    12. +--------------------------+------------+-------------+------------+------------+-------------+
    13. 3 rows in set (0.03 sec)

    删除索引

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

    • 示例:删除表的索引
    1. Query OK, 0 rows affected (0.02 sec)