Updated on 2024-04-29 GMT+08:00

CREATE VIEW

This section describes how to create a normal view in ClickHouse.

Creating a View

CREATE VIEW [IF NOT EXISTS] [db.]view_name [ON CLUSTER ClickHouse cluster name] AS SELECT ...
Table 1 Parameter description

Parameter

Description

db

Name of the database. The default value is the selected database.

view_name

View name.

[ON CLUSTER ClickHouse cluster name]

This parameter specifies that a view is created on each node. The parameter format is ON CLUSTER ClickHouse cluster name.

SELECT ...

SELECT clause. When you insert data into the source table that is specified in the SELECT clause in the view, the inserted data is transformed by the SELECT query and the final result is inserted into the view.

Examples:

  1. Create a source table.
    create table DB.table1 ON CLUSTER default_cluster (id Int16,name String) ENGINE = MergeTree() ORDER BY (id);
  2. Create a view.
    CREATE VIEW test_view ON CLUSTER default_cluster AS SELECT * FROM DB.table1;
  3. Insert data to the source table.
    insert into DB.table1 values(1,'X'),(2,'Y'),(3,'Z');
  4. Query a view.
    SELECT * FROM test_view;
  5. Delete a view.
    drop table test_view ON CLUSTER default_cluster;
    • If the table creation statement contains ON CLUSTER ClickHouse cluster name, run the following command to delete the table:
      drop table Table name ON CLUSTER default_cluster;
    • If the table creation statement does not contain ON CLUSTER ClickHouse cluster name, run the following command to delete the table:
      drop table Table name;