On this page
RENAME TO
is a subcommand of ALTER INDEX
that changes the name of an index for a table.
Note:
It is not possible to rename an index referenced by a view. For more details, see View Dependencies.
Note:
The ALTER INDEX ... RENAME TO
statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.
This schema change statement is registered as a job. You can view long-running jobs with SHOW JOBS
.
Synopsis
Required privileges
The user must have the CREATE
privilege on the table.
Parameters
Parameter | Description |
---|---|
IF EXISTS |
Rename the index only if an index current_name exists; if one does not exist, do not return an error. |
table_name |
The name of the table with the index you want to use |
index_name |
The current name of the index |
name |
The name you want to use for the index, which must be unique to its table and follow these identifier rules. |
Example
Rename an index
> CREATE INDEX on users(name);
> SHOW INDEXES FROM users;
table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit | visible
-------------+------------+------------+--------------+-------------+-----------+---------+----------+----------
users | name_idx | t | 1 | name | DESC | f | f | t
users | name_idx | t | 2 | city | ASC | f | t | t
users | name_idx | t | 3 | id | ASC | f | t | t
users | users_pkey | f | 1 | city | ASC | f | f | t
users | users_pkey | f | 2 | id | ASC | f | f | t
users | users_pkey | f | 3 | name | N/A | t | f | t
users | users_pkey | f | 4 | address | N/A | t | f | t
users | users_pkey | f | 5 | credit_card | N/A | t | f | t
(8 rows)
> ALTER INDEX users@name_idx RENAME TO users_name_idx;
> SHOW INDEXES FROM users;
table_name | index_name | non_unique | seq_in_index | column_name | direction | storing | implicit | visible
-------------+----------------+------------+--------------+-------------+-----------+---------+----------+----------
users | users_name_idx | t | 1 | name | DESC | f | f | t
users | users_name_idx | t | 2 | city | ASC | f | t | t
users | users_name_idx | t | 3 | id | ASC | f | t | t
users | users_pkey | f | 1 | city | ASC | f | f | t
users | users_pkey | f | 2 | id | ASC | f | f | t
users | users_pkey | f | 3 | name | N/A | t | f | t
users | users_pkey | f | 4 | address | N/A | t | f | t
users | users_pkey | f | 5 | credit_card | N/A | t | f | t
(8 rows)