|NO.Z.00032|——————————|BigDataEnd|——|Hadoop&OLAP_ClickHouse.V04|——|ClickHouse.v04|ClickHouse语法|视图|移动数据表|

一、视图:
### --- 普通视图和物化视图

~~~     普通视图:不保存数据,只是一层单纯的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        │ StringDEFAULT'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

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(20)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示