ClickHouse常用sql语句

ClickHouse常用sql

一、查询表列名

① 利用system数据库中的parts_columns表进行查询。

select distinct column from system.parts_columns 
where database='表所属的数据库名称' and table='所需要查询的表名'

例如:
select distinct column from system.parts_columns where database='test' and table='table1'

-- 多表字段去重查询
select distinct column from system.parts_columns 
where database= 'test' 
and table='table2' or table='table3' or table='table4' 

– ② 利用system数据库中的columns表进行查询。

select distinct name from system.columns 
where database='表所属的数据库名称' and table='所需要查询的表名'

例如:
select distinct name from system.columns where database='test' and table='table1'
select distinct name from system.columns where database= 'test' and 
table='table2' or 
table='table3' or 
table='table4' 

–推荐使用第二种方式来查询表的所有列名。
–因为用第一种方式来查询某个表的所有列名时,当该表为空的时候,查询的列名也会为空,而当表不为空的情况下,才可查询到该表所有的列名。
–当用第二种方式来查询表的所有列名时,无论该表是否为空,都可以查询到该表的所有列名。

二、常用创表语句

 alter table 表名称 ON 集群名称 add column 列名称;        --添加列
 alter table 表名称 ON 集群名称 drop column 列名称;       --删除列
 alter table 表名称 ON 集群名称 modify column 列名称 数据类型;   --修改数据类型
 alter table 表名称 ON 集群名称 COMMENT COLUMN 列名称 注解;     --修改注释

-- 例如:
alter table test.table1 add column name Nullable(String) COMMENT '姓名' ;  --add列
alter table test.table1 drop column name;       --删除列
CREATE TABLE test.table1(
    `id` String COMMENT '主键',
    `chinese_name` Nullable(String) COMMENT '中文名称',
    `english_name` Nullable(String) COMMENT '英文名称',
    `update_time` Nullable(DATETIME) COMMENT '更新时间',
    `create_time` Nullable(DATETIME) COMMENT '创建时间'
)
ENGINE = MergeTree
order by id
--primary key id
--order by (id,create_time)
SETTINGS index_granularity = 8192
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

清淡的粥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值