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

相关推荐
什么芮.1 分钟前
spark-streaming
pytorch·sql·spark·kafka·scala
hnlucky17 分钟前
redis 数据类型新手练习系列——Hash类型
数据库·redis·学习·哈希算法
LucianaiB1 小时前
【金仓数据库征文】_AI 赋能数据库运维:金仓KES的智能化未来
运维·数据库·人工智能·金仓数据库 2025 征文·数据库平替用金仓
时序数据说1 小时前
时序数据库IoTDB在航空航天领域的解决方案
大数据·数据库·时序数据库·iotdb
.生产的驴2 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
AnsenZhu2 小时前
2025年Redis分片存储性能优化指南
数据库·redis·性能优化·分片
oydcm2 小时前
MySQL数据库概述
数据库·mysql
oioihoii2 小时前
C++23中if consteval / if not consteval (P1938R3) 详解
java·数据库·c++23
带娃的IT创业者2 小时前
《AI大模型趣味实战》基于RAG向量数据库的知识库AI问答助手设计与实现
数据库·人工智能
husterlichf4 小时前
MYSQL 常用数值函数 和 条件函数 详解
数据库·sql·mysql