CC00032.clickhouse——|Hadoop&OLAP_ClickHouse.V04|——|ClickHouse.v04|ClickHouse语法|视图|

本文主要介绍了ClickHouse数据库的语法重点,特别是视图的使用,并探讨了如何在ClickHouse中操作数据表进行移动。适合对大数据分析和SQL感兴趣的读者。
摘要由CSDN通过智能技术生成
一、视图:
### --- 普通视图和物化视图

~~~     普通视图:不保存数据,只是一层单纯的select查询映射,起着简化查询、明晰语义的作用。
~~~     物化视图:保存数据,如果源表被写入新数据,物化视图也会同步更新。
~~~     POPULATE修饰符:决定在创建物化视图的过程中是否将源表的数据同步到物化视图里。
### --- 数据表的基本操作:

~~~     只有MergeTree、Merge、Distribution这三类表引擎支持alter操作。
### --- 追加字段,两种方式:

alter table partition_v1 add column os String default 'mac';
alter table partition_v1 add column IP String after ID;
### --- 视图示例

~~~     # 查看表结构
hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID        │ String │              │                    │         │                  │                │
│ URL       │ String │              │                    │         │                  │                │
│ EventTime │ Date   │              │                    │         │                  │                │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
~~~     # 追加字段

hadoop01 :) alter table partition_v1 add column os String default 'mac';

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID        │ String │              │                    │         │                  │                │
│ URL       │ String │              │                    │         │                  │                │
│ EventTime │ Date   │              │                    │         │                  │                │
│ os        │ String │ DEFAULT      │ 'mac'              │         │                  │                │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
~~~     # 追加字段

hadoop01 :) alter table partition_v1 add column IP String after ID;

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID        │ String │              │                    │         │                  │                │
│ IP        │ String │              │                    │         │                  │                │
│ URL       │ String │              │                    │         │                  │                │
│ EventTime │ Date   │              │                    │         │                  │                │
│ os        │ String │ DEFAULT      │ 'mac'              │         │                  │                │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
### --- 修改数据类型:

~~~     alter...modify column...
~~~     注意:类型需要相互兼容
~~~     # 修改数据类型

hadoop01 :) alter table partition_v1 modify column IP IPv4;

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID        │ String │              │                    │         │                  │                │
│ IP        │ IPv4   │              │                    │         │                  │                │
│ URL       │ String │              │                    │         │                  │                │
│ EventTime │ Date   │              │                    │         │                  │                │
│ os        │ String │ DEFAULT      │ 'mac'              │         │                  │                │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
### --- 修改备注:

alter...comment column ...
~~~     # 修改备注:主键ID

hadoop01 :) ALTER TABLE partition_v1 COMMENT COLUMN ID '主键ID';

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID        │ String │              │                    │ 主键ID  │                  │                │
│ IP        │ IPv4   │              │                    │         │                  │                │
│ URL       │ String │              │                    │         │                  │                │
│ EventTime │ Date   │              │                    │         │                  │                │
│ os        │ String │ DEFAULT      │ 'mac'              │         │                  │                │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
### --- 删除已有字段:

~~~     会把该字段下的数据一起删除
~~~     # 删除已有字段

hadoop01 :) alter table partition_v1 drop column URL;

hadoop01 :) desc partition_v1;

┌─name──────┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ ID        │ String │              │                    │ 主键ID  │                  │                │
│ IP        │ IPv4   │              │                    │         │                  │                │
│ EventTime │ Date   │              │                    │         │                  │                │
│ os        │ String │ DEFAULT      │ 'mac'              │         │                  │                │
└───────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
七、移动数据表
### --- 移动数据表

~~~     rename... to...
~~~     注意:表的移动只能在单节点内完成
### --- 移动数据表示例

~~~     # 移动数据表
hadoop01 :) rename table default.partition_v1 to system.partition_v1;
~~~     # 查看移动的数据表
hadoop01 :) use system;
hadoop01 :) show tables;
~~~输出参数
partition_v1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值