Share via


ALTER VIEW

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Alters metadata associated with the view. It can change the definition of the view, change the name of a view to a different name, set and unset the metadata of the view by setting TBLPROPERTIES.

To add or alter a comment on a view or its columns, use COMMENT ON.

If the view is cached, the command clears cached data of the view and all its dependents that refer to it. The view's cache will be lazily filled when the view is accessed the next time. The command leaves view's dependents as uncached.

Syntax

ALTER VIEW view_name
  { rename |
    SET TBLPROPERTIES clause |
    UNSET TBLPROPERTIES clause |
    alter_body |
    schema_binding |
    owner_to |
    SET TAGS clause |
    UNSET TAGS clause }

rename
  RENAME TO to_view_name

alter_body
AS { query | yaml_definition }

yaml_definition
  $$
    yaml_string
  $$

schema_binding
  WITH SCHEMA { BINDING | [ TYPE ] EVOLUTION | COMPENSATION }

property_key
  { idenitifier [. ...] | string_literal }

owner_to
  [ SET ] OWNER TO principal

Parameters

  • view_name

    Identifies the view to be altered. If the view cannot be found Azure Databricks raises a TABLE_OR_VIEW_NOT_FOUND error.

  • RENAME TO to_view_name

    Renames the existing view to to_view_name.

    For Unity Catalog views, the to_view_name must be within the same catalog as view_name. For other views, the to_view_name must be within the same schema as view_name.

    If to_view_name is unqualified it is implicitly qualified with the current schema.

    Materialized views cannot be renamed.

    Sets or resets one or more user defined properties.

  • UNSET TBLPROPERTIES

    Removes one or more user defined properties.

  • AS query

    A query that constructs the view from base tables or other views.

    AS query is not supported for metric views.

    This clause is equivalent to a CREATE OR REPLACE VIEW statement on an existing view, except that privileges granted on the view are preserved.

  • AS yaml_definition

    Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 16.4 and above check marked yes Unity Catalog only

    A yaml_definition for a metric view.

    This clause is equivalent to a CREATE OR REPLACE VIEW statement on an existing view, except that privileges granted on the view are preserved.

  • schema_binding

    Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 15.3 and above

    Specifies how subsequent querying of the view adapts to changes to the view's schema due to changes in the underlying object definitions. See CREATE VIEW… WITH SCHEMA for details on schema binding modes.

    This clause is not supported for metric views.

  • [ SET ] OWNER TO principal

    Transfers ownership of the view to principal. Unless the view is defined in the hive_metastore you may only transfer ownership to a group you belong to.

    Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 11.3 LTS and above

    SET is allowed as an optional keyword.

  • SET TAGS ( { tag_name = tag_value } [, …] )

    Apply tags to the view. You need to have APPLY TAG permission to add tags to the view.

    Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 13.3 LTS and above

  • UNSET TAGS ( tag_name [, …] )

    Remove tags from the table. You need to have APPLY TAG permission to remove tags from the view.

    Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 13.3 LTS and above

  • tag_name

    A literal STRING. The tag_name must be unique within the view.

  • tag_value

    A literal STRING.

Examples

-- Rename only changes the view name.
-- The source and target schemas of the view have to be the same.
-- Use qualified or unqualified name for the source and target view.
> ALTER VIEW tempsc1.v1 RENAME TO tempsc1.v2;

-- Verify that the new view is created.
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1       int   NULL
                            c2    string   NULL

  # Detailed Table Information
                      Database   tempsc1
                         Table        v2

-- Before ALTER VIEW SET TBLPROPERTIES
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1       int   null
                            c2    string   null

  # Detailed Table Information
                      Database   tempsc1
                         Table        v2
              Table Properties    [....]

-- Set properties in TBLPROPERTIES
> ALTER VIEW tempsc1.v2 SET TBLPROPERTIES ('created.by.user' = "John", 'created.date' = '01-01-2001' );

-- Use `DESCRIBE TABLE EXTENDED tempsc1.v2` to verify
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1                                                   int   NULL
                            c2                                                string   NULL

  # Detailed Table Information
                      Database                                               tempsc1
                         Table                                                    v2
              Table Properties [created.by.user=John, created.date=01-01-2001, ....]

-- Remove the key created.by.user and created.date from `TBLPROPERTIES`
> ALTER VIEW tempsc1.v2 UNSET TBLPROPERTIES (`created`.`by`.`user`, created.date);

-- Use `DESCRIBE TABLE EXTENDED tempsc1.v2` to verify the changes
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1       int   NULL
                            c2    string   NULL

  # Detailed Table Information
                      Database   tempsc1
                         Table        v2
              Table Properties    [....]

-- Change the view definition
> ALTER VIEW tempsc1.v2 AS SELECT * FROM tempsc1.v1;

-- Use `DESCRIBE TABLE EXTENDED` to verify
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1                        int   NULL
                            c2                     string   NULL

  # Detailed Table Information
                      Database                    tempsc1
                         Table                         v2
                          Type                       VIEW
                     View Text   select * from tempsc1.v1
            View Original Text   select * from tempsc1.v1

-- Transfer ownership of a view to another user
> ALTER VIEW v1 OWNER TO `alf@melmak.et`

-- Change the view schema binding to adopt type evolution
> ALTER VIEW v1 WITH SCHEMA TYPE EVOLUTION;

-- Applies three tags to the view named `test`.
> ALTER VIEW test SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');

-- Removes three tags from the view named `test`.
> ALTER VIEW test UNSET TAGS ('tag1', 'tag2', 'tag3');

-- Alter a the metric view `region_sales_metrics` defined in CREATE VIEW to drop the `total_revenue_for_open_orders` measure.
> ALTER VIEW region_sales_metrics
  AS $$
   version: 0.1
   source: samples.tpch.orders
   filter: o_orderdate > '1990-01-01'
   dimensions:
   - name: month
     expr: date_trunc('MONTH', o_orderdate)
   - name: status
     expr: case
       when o_orderstatus = 'O' then 'Open'
       when o_orderstatus = 'P' then 'Processing'
       when o_orderstatus = 'F' then 'Fulfilled'
       end
   - name: order_priority
     expr: split(o_orderpriority, '-')[1]
   measures:
   - name: count_orders
     expr: count(1)
   - name: total_revenue
     expr: SUM(o_totalprice)
   - name: total_revenue_per_customer
     expr: SUM(o_totalprice) / count(distinct o_custkey)
  $$;

> DESCRIBE EXTENDED region_sales_metrics;
 col_name                    data_type
 month	                     timestamp
 status	                     string
 prder_priority              string
 count_orders                bigint measure
 total_revenue               decimal(28,2) measure
 total_revenue_per_customer  decimal(38,12) measure

 # Detailed Table Information
 Catalog                     main
 Database                    default
 Table                       region_sales_metrics
 Owner                       alf@melmak.et
 Created Time                Sun May 18 23:45:25 UTC 2025
 Last Access                 UNKNOWN
 Created By                  Spark
 Type                        METRIC_VIEW
 Comment                     A metric view for regional sales metrics.
 View Text                   "
    version: 0.1
    source: samples.tpch.orders
    filter: o_orderdate > '1990-01-01'
    dimensions:
    - name: month
      expr: date_trunc('MONTH', o_orderdate)
    - name: status
      expr: case
        when o_orderstatus = 'O' then 'Open'
        when o_orderstatus = 'P' then 'Processing'
        when o_orderstatus = 'F' then 'Fulfilled'
        end
    - name: prder_priority
      expr: split(o_orderpriority, '-')[1]
    measures:
    - name: count_orders
      expr: count(1)
    - name: total_revenue
      expr: SUM(o_totalprice)
    - name: total_revenue_per_customer
      expr: SUM(o_totalprice) / count(distinct o_custkey)
   "
 Language                    YAML
 Table Properties            [metric_view.from.name=samples.tpch.orders, metric_view.from.type=ASSET, metric_view.where=o_orderdate > '1990-01-01']