ALTER VIEW

Description

The ALTER VIEW statement can alter 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.

RENAME View

Renames the existing view. If the new view name already exists in the source database, a TableAlreadyExistsException is thrown. This operation does not support moving the views across databases.

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

Syntax

  1. ALTER VIEW view_identifier RENAME TO view_identifier

Parameters

  • view_identifier

    Specifies a view name, which may be optionally qualified with a database name.

    Syntax: [ database_name. ] view_name

SET View Properties

Set one or more properties of an existing view. The properties are the key value pairs. If the properties’ keys exist, the values are replaced with the new values. If the properties’ keys do not exist, the key value pairs are added into the properties.

Syntax

  1. ALTER VIEW view_identifier SET TBLPROPERTIES ( property_key = property_val [ , ... ] )

Parameters

  • view_identifier

    Specifies a view name, which may be optionally qualified with a database name.

    Syntax: [ database_name. ] view_name

  • property_key

    Specifies the property key. The key may consists of multiple parts separated by dot.

    Syntax: [ key_part1 ] [ .key_part2 ] [ ... ]

UNSET View Properties

Drop one or more properties of an existing view. If the specified keys do not exist, an exception is thrown. Use IF EXISTS to avoid the exception.

Syntax

  1. ALTER VIEW view_identifier UNSET TBLPROPERTIES [ IF EXISTS ] ( property_key [ , ... ] )

Parameters

  • view_identifier

    Specifies a view name, which may be optionally qualified with a database name.

    Syntax: [ database_name. ] view_name

  • property_key

    Specifies the property key. The key may consists of multiple parts separated by dot.

    Syntax: [ key_part1 ] [ .key_part2 ] [ ... ]

ALTER View AS SELECT

ALTER VIEW view_identifier AS SELECT statement changes the definition of a view. The SELECT statement must be valid, and the view_identifier must exist.

Syntax

  1. ALTER VIEW view_identifier AS select_statement

Note that ALTER VIEW statement does not support SET SERDE or SET SERDEPROPERTIES properties.

Parameters

  • view_identifier

    Specifies a view name, which may be optionally qualified with a database name.

    Syntax: [ database_name. ] view_name

  • select_statement

    Specifies the definition of the view. Check select_statement for details.

Examples

  1. -- Rename only changes the view name.
  2. -- The source and target databases of the view have to be the same.
  3. -- Use qualified or unqualified name for the source and target view.
  4. ALTER VIEW tempdb1.v1 RENAME TO tempdb1.v2;
  5. -- Verify that the new view is created.
  6. DESCRIBE TABLE EXTENDED tempdb1.v2;
  7. +----------------------------+----------+-------+
  8. | col_name|data_type |comment|
  9. +----------------------------+----------+-------+
  10. | c1| int| null|
  11. | c2| string| null|
  12. | | | |
  13. |# Detailed Table Information| | |
  14. | Database| tempdb1| |
  15. | Table| v2| |
  16. +----------------------------+----------+-------+
  17. -- Before ALTER VIEW SET TBLPROPERTIES
  18. DESC TABLE EXTENDED tempdb1.v2;
  19. +----------------------------+----------+-------+
  20. | col_name| data_type|comment|
  21. +----------------------------+----------+-------+
  22. | c1| int| null|
  23. | c2| string| null|
  24. | | | |
  25. |# Detailed Table Information| | |
  26. | Database| tempdb1| |
  27. | Table| v2| |
  28. | Table Properties| [....]| |
  29. +----------------------------+----------+-------+
  30. -- Set properties in TBLPROPERTIES
  31. ALTER VIEW tempdb1.v2 SET TBLPROPERTIES ('created.by.user' = "John", 'created.date' = '01-01-2001' );
  32. -- Use `DESCRIBE TABLE EXTENDED tempdb1.v2` to verify
  33. DESC TABLE EXTENDED tempdb1.v2;
  34. +----------------------------+-----------------------------------------------------+-------+
  35. | col_name| data_type|comment|
  36. +----------------------------+-----------------------------------------------------+-------+
  37. | c1| int| null|
  38. | c2| string| null|
  39. | | | |
  40. |# Detailed Table Information| | |
  41. | Database| tempdb1| |
  42. | Table| v2| |
  43. | Table Properties|[created.by.user=John, created.date=01-01-2001, ....]| |
  44. +----------------------------+-----------------------------------------------------+-------+
  45. -- Remove the key `created.by.user` and `created.date` from `TBLPROPERTIES`
  46. ALTER VIEW tempdb1.v2 UNSET TBLPROPERTIES ('created.by.user', 'created.date');
  47. --Use `DESC TABLE EXTENDED tempdb1.v2` to verify the changes
  48. DESC TABLE EXTENDED tempdb1.v2;
  49. +----------------------------+----------+-------+
  50. | col_name| data_type|comment|
  51. +----------------------------+----------+-------+
  52. | c1| int| null|
  53. | c2| string| null|
  54. | | | |
  55. |# Detailed Table Information| | |
  56. | Database| tempdb1| |
  57. | Table| v2| |
  58. | Table Properties| [....]| |
  59. +----------------------------+----------+-------+
  60. -- Change the view definition
  61. ALTER VIEW tempdb1.v2 AS SELECT * FROM tempdb1.v1;
  62. -- Use `DESC TABLE EXTENDED` to verify
  63. DESC TABLE EXTENDED tempdb1.v2;
  64. +----------------------------+---------------------------+-------+
  65. | col_name| data_type|comment|
  66. +----------------------------+---------------------------+-------+
  67. | c1| int| null|
  68. | c2| string| null|
  69. | | | |
  70. |# Detailed Table Information| | |
  71. | Database| tempdb1| |
  72. | Table| v2| |
  73. | Type| VIEW| |
  74. | View Text| select * from tempdb1.v1| |
  75. | View Original Text| select * from tempdb1.v1| |
  76. +----------------------------+---------------------------+-------+