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