进入click(不加上-m的话,进入之后只能一次写一行,不能建表)

clickhouse client -m

查看数据库

show databases;  

创建一个数据库

create database db_doit; 

删除数据库

drop database  db_doit;   

查看表

show tables:  

查看当前使用的数据库

select currentDatabase();

创建一个表(建表的时候指定数据类型,建表的时候一定要指定表引擎)

create table tb_user(
	uid Int32,
	name String,
	age UInt32,
	gender String
)engine = TinyLog;

查看表的结构

desc tb_user;

插入语句

insert into tb_user values(1,'hello',23,'M');
insert into tb_user values(2,'上海',33,'F');

查看表

select * from  tb_user;

DateTime(以下三种都可以)

create table tb_date1(
 timestamp  DateTime
)engine = TinyLog;

create table tb_date2(
	date timestamp
)engine = TinyLog;

create table tb_date3(
	datetime date
)engine = TinyLog;

插入时间

insert into tb_date values('2020-08-24 21:06:00');

Enum 枚举(定义常量)

create table tb_enum(
	m Enum('hello'=1,'world'=2)
)engine = TinyLog;

insert into tb_enum values('hello'),('world'),('hello');

select cast(m,'Int8') from tb_enum;

将hello ,world 转为Int8类型

数组(Array(数据类型))
create table tb_array(
	name String,
	hobby Array(String)
	)engine = TinyLog;

**插入**

insert into tb_array values('你好',['h','l','hello']);

**数组有角标,然后是从1开始**

select name ,hobby[2] from tb_array;

MegerTree建表

需要主键,排序字段 ( primary key , order by) 两个一致

create table tb_megertree(
	uid Int32,
	name String,
	age UInt8,
	birthday Date,
	gender String)
engine=MergeTree()
order by uid;

插入数据

insert into tb_megertree values(2,'李白',60,'123324435','M');
insert into tb_megertree values(24,'杜甫',59,1234567,'M'), (3,'李清照',55,1234323,'F');

insert into tb_megertree values(6,'徐志摩',50,'333324435','M');

partition : 分区
create table tb_partition(
	uid Int8,
	address String
)
engine=MergeTree()
order by uid
partition by address;

insert into tb_partition values(3,'北京'),(5,'北京'),(1,'上海'),(7,'北京'),(30,'北京'),(11,'上海');

再插入

insert into tb_partition values(33,'上海'),(53,'北京'),(13,'上海');

再合并

optimize table tb_partition;
optimize table tb_partition;

一次合并一个分区,执行两次分区全部合并ReplacingMergeTree删除区内主键相同的数据 保留指定的字段中的最大版本