Skip to main content
The ALTER VIEW applies a schema change to a .
The “ statement performs a schema change. For more information about how online schema changes work in CockroachDB, see .

Required privileges

  • To set or reset security_invoker, the user must own the view.
  • To change the schema of a view with ALTER VIEW ... SET SCHEMA, the user must have the DROP on the view and the CREATE privilege on the new schema.
  • To rename a view with ALTER VIEW ... RENAME TO, the user must have the DROP on the view and the CREATE privilege on the view’s database.

Syntax

alter_view syntax diagram

Parameters

ParameterDescription
MATERIALIZEDRename a .
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 existing view names, use:

SELECT * FROM information_schema.tables WHERE table_type = 'VIEW';
view_new_nameThe new name of the view. The name of the view must be unique to its database and follow these . Name changes do not propagate to the table(s) using the view.
schema_nameThe name of the new schema.
role_specThe role to set as the owner of the view.
security_invokerSets whether to check privileges on the underlying tables as the querying user (true) or the view owner (false). SET (security_invoker) is equivalent to SET (security_invoker = true). RESET (security_invoker) restores the default behavior, where a view checks privileges on the underlying tables as the view owner. This option applies only to non-materialized views.

Known limitations

ALTER VIEW does not currently support:
  • Changing the statement executed by a view. Instead, you must drop the existing view and create a new view.
  • Renaming a view that other views depend on. This feature may be added in the future.

Examples

Rename a view

Suppose you create a new view that you want to rename:
Note that RENAME TO only changes the name of the view; it cannot move the view to a different database or schema. To change a view’s schema, use the SET SCHEMA clause.

Change the schema of a view

Suppose you want to add the expensive_rides view to a schema called cockroach_labs: By default, created in the database belong to the public schema:
If the new schema does not already exist, :
Then, change the view’s schema:

Set or reset security_invoker

By default, a view checks privileges on the underlying tables as the view owner. To change the view to use the querying user’s privileges instead, include the :
To restore the default behavior:

See also