Clickhouse的基本sql语句

1,建库语句

bash 复制代码
create database if not exists mytestdb on cluster default_cluster;

注意:用户在集群中创建数据库或者表时使用ON CLUSTER语句,保证各个ClickHouse节点上数据库、表的元信息相同。

2,建表

bash 复制代码
-- 创建复制表
create table mytestdb.test_log3 on cluster default_cluster(id UInt32,user_id String,app_id String,create_time String) ENGINE=ReplicatedMergeTree('/clickhouse/tables/{shard}/mytestdb.test_log3','{replica}') order by id;

-- 创建分布式表
create table mytestdb.test_log3_all on cluster default_cluster as mytestdb.test_log3 ENGINE=Distributed('default_cluster','mytestdb','test_log3',rand());

注意:需要同时建本地表和分布式表,建表时需要加cluster

3,查询数据

bash 复制代码
select * from mytestdb.test_log3_all

注意: 查询写入都用分布式表, 写入也可以用本地表,结合replied复制的特性也会自己在副本表复制数据的。

通常都可以使用这个引擎:

ReplicatedMergeTree(/clickhouse/tables/{shard}/default/test','{replica}');

4,删除数据

bash 复制代码
alter table xbtx.xbtx_func_tb on cluster default_cluster delete where app_id='com.sinosure.xbt';

注意:删除时不要操作分布式表,操作本地表

5,更新数据

bash 复制代码
alter table xbtx.xbtx_func_tb on cluster default_cluster update func_name='公文' where id='gongwen';

6,新增数据

bash 复制代码
insert into xbtx.xbtx_func_tb_all(id,func_name,app_id) values('xxxx','xxxx','xxxx');

7,删除表

bash 复制代码
-- 删除本地表:
drop table mytestdb.xbtx_app_log_tb on cluster default_cluster no delay;
-- 删除分布式表:
drop table mytestdb.xbtx_app_log_tb_all on cluster default_cluster no delay;

注意: 删除表时需要加on cluster

相关推荐
程序员Android2 分钟前
MTK多帧拍照流程分析
数据库
不剪发的Tony老师4 分钟前
Apache Doris:一款高性能的实时数据仓库
数据库·数据仓库
甜可儿15 分钟前
redis序列化设置
数据库·redis
GGBondlctrl20 分钟前
【SpringBoot】论坛项目中如何进行实现发布文章,以及更新对应数据库的数据更新
数据库·springboot项目·三层设计思想·文章发布项目
若云止水33 分钟前
Ubuntu 下 nginx-1.24.0 源码分析 - ngx_init_cycle 函数 - 详解(4)
数据库·nginx·ubuntu
jay丿1 小时前
Django模板系统深入
数据库·django·sqlite
Habibei1 小时前
Mybatis的分页插件
sql·mybatis
千层冷面1 小时前
Redis除了做缓存还能做什么?
数据库·redis·缓存
吃汤圆的抹香鲸1 小时前
Rider 安装包 绿色版 Win/Mac/Linux 适合.NET和游戏开发者使用 2025全栈开发终极指南:从零配置到企业级实战
linux·运维·windows·sql·游戏·macos·.net
码有余悸3 小时前
Redis:高性能的键值存储系统
数据库·redis·缓存