|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 │ 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
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」