ALTER VIEW

The ALTER VIEW statement changes the name of a view.

Note:

This statement performs a schema change. For more information about how online schema changes work in CockroachDB, see Online Schema Changes.

Note:

It is not currently possible to change the SELECT statement executed by a view. Instead, you must drop the existing view and create a new view. Also, it is not currently possible to rename a view that other views depend on, but this ability may be added in the future (see this issue).

Required privileges

The user must have the DROP privilege on the view and the CREATE privilege on the parent database.

Synopsis

ALTERVIEWIFEXISTSview_nameRENAMETOname

Parameters

ParameterDescription
IF EXISTSRename the view only if a view of view_name exists; if one does not exist, do not return an error.
view_nameThe name of the view to rename. To find view names, use:SELECT * FROM information_schema.tables WHERE table_type = 'VIEW';
nameThe new name for the view, which must be unique to its database and follow these identifier rules.

Example

  1. > SELECT * FROM information_schema.tables WHERE table_type = 'VIEW';
  1. +---------------+-------------------+--------------------+------------+---------+
  2. | TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | VERSION |
  3. +---------------+-------------------+--------------------+------------+---------+
  4. | def | bank | user_accounts | VIEW | 2 |
  5. | def | bank | user_emails | VIEW | 1 |
  6. +---------------+-------------------+--------------------+------------+---------+
  7. (2 rows)
  1. > ALTER VIEW bank.user_emails RENAME TO bank.user_email_addresses;
  1. > RENAME VIEW
  1. > SELECT * FROM information_schema.tables WHERE table_type = 'VIEW';
  1. +---------------+-------------------+----------------------+------------+---------+
  2. | TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | VERSION |
  3. +---------------+-------------------+----------------------+------------+---------+
  4. | def | bank | user_accounts | VIEW | 2 |
  5. | def | bank | user_email_addresses | VIEW | 3 |
  6. +---------------+-------------------+----------------------+------------+---------+
  7. (2 rows)

See also

Was this page helpful?
YesNo