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

相关推荐
研究司马懿7 分钟前
【ETCD】ETCD常用命令
网络·数据库·云原生·oracle·自动化·运维开发·etcd
刘一说1 小时前
深入理解 Spring Boot 中的数据库迁移:Flyway 与 Liquibase 实战指南
数据库·spring boot·oracle
August_._2 小时前
【MySQL】SQL语法详细总结
java·数据库·后端·sql·mysql·oracle
升鲜宝供应链及收银系统源代码服务2 小时前
升鲜宝生鲜配送供应链管理系统---PMS--商品品牌多语言存储与 Redis 缓存同步实现
java·开发语言·数据库·redis·缓存·开源·供应链系统
苦学编程的谢4 小时前
Redis_8_List
数据库·redis·缓存
曹天骄4 小时前
阿里云 DCDN → CDN 无缝切换教程(以 example.com 为例)
数据库·阿里云·云计算
workflower5 小时前
软件工程-练习
数据库·需求分析·个人开发·极限编程·结对编程
扶尔魔ocy5 小时前
【QT自定义2D控件】QGraphics绘制仪表盘
数据库·qt·microsoft
yookay zhang5 小时前
达梦数据库监听进程
网络·数据库·oracle
Archy_Wang_16 小时前
centos7的mysql做定时任务备份所有数据库
数据库·mysql