一.基本语法演示
show databases;
create database if not exists test1;
use test1;
select currentDatabase();
drop database test1;
二.基本的建表语法
- 注意在ck中关键字严格区分大小写
- ck中建表的时候,一定指定表引擎
create table tb_test1(id Int8,name String)engine=Memory;
desc tb_test1;
┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ Int8 │ │ │ │ │ │
│ name │ String │ │ │ │ │ │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
三.DDL基础
create table tb_test1(id Int8,name String)engine=Memory();
create table test_alter1(
id Int8,
name String
)engine=MergeTree()order by id;
show create table test_alter1;
┌─statement────────────────────────────────────────────────────────────────────────────────────────┐
│ CREATE TABLE doit23.test_alter1(
`id` Int8,`name` String)ENGINE = MergeTreeORDER BY idSETTINGS index_granularity = 8192 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
alter table test_alter1 add column age UInt8;
desc test_alter1;
┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ Int8 │ │ │ │ │ │
│ name │ String │ │ │ │ │ │
│ age │ UInt8 │ │ │ │ │ │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
alter table test_alter1 add column gender String after name;
desc test_alter1;
┌─name───┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ Int8 │ │ │ │ │ │
│ name │ String │ │ │ │ │ │
│ gender │ String │ │ │ │ │ │
│ age │ UInt8 │ │ │ │ │ │
└────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
alter table test_alter1 drop column age;
desc test_alter1;
┌─name───┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ Int8 │ │ │ │ │ │
│ name │ String │ │ │ │ │ │
│ gender │ String │ │ │ │ │ │
└────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
alter table test_alter1 modify column gender UInt8 defalut 0;
desc test_alter1;
┌─name───┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ Int8 │ │ │ │ │ │
│ name │ String │ │ │ │ │ │
│ gender │ UInt8 │ DEFAULT │ 0 │ │ │ │
└────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
alter table test_alter1 comment column name '用户名';
desc test_alter1;
┌─name───┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ Int8 │ │ │ │ │ │
│ name │ String │ │ │ 用户名 │ │ │
│ gender │ UInt8 │ DEFAULT │ 0 │ │ │ │
└────────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
rename table test_alter1 to test_alter;
show tables;
┌─name───────┐
│ test_alter │
└────────────┘
rename table test_array to tb_array,test_date to tb_date;
show tables;
┌─name─────────┐
│ tb_array │
│ tb_date │
└──────────────┘
show tables from doit23;
┌─name───────┐
│ test_alter │
└────────────┘
rename table tb_test1 to doit23.tb_test;
show tables from doit23;
┌─name───────┐
│ tb_test │
│ test_alter │
└────────────┘
create table test_prp(
id UInt8,
age UInt8 default 0,
name String comment '用户名',
role String default 'VIP' comment '角色'
)engine=Memory;
desc test_prp;
┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ UInt8 │ │ │ │ │ │
│ age │ UInt8 │ DEFAULT │ 0 │ │ │ │
│ name │ String │ │ │ 用户名 │ │ │
│ role │ String │ DEFAULT │ 'VIP' │ 角色 │ │ │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
create table tb_insert(
id Int8,
name String,
age UInt8
)engine=Memory;
insert into tb_insert values(1,'zs',23),(2,'ls',24);
select * from tb_insert;
┌─id─┬─name─┬─age─┐
│ 1 │ zs │ 23 │
│ 2 │ ls │ 24 │
└────┴──────┴─────┘
create table tb_insert1 as tb_insert;
desc tb_insert1;
┌─name─┬─type───┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ Int8 │ │ │ │ │ │
│ name │ String │ │ │ │ │ │
│ age │ UInt8 │ │ │ │ │ │
└──────┴────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
insert into tb_insert1 select * from tb_insert;
select * from tb_insert1;
┌─id─┬─name─┬─age─┐
│ 1 │ zs │ 23 │
│ 2 │ ls │ 24 │
└────┴──────┴─────┘
create table tb_insert2(
uid Int8,
name String,
age UInt8,
city String,
gender String
)engine=Memory
cat user.txt
1,zss,23,BJ,M
2,lss,33,NJ,M
3,ww,21,SH,F
cat user.txt | clickhouse-client -q 'insert into table doit23.tb_insert2 format CSV'
select * from tb_insert2;
┌─uid─┬─name─┬─age─┬─city─┬─gender─┐
│ 1 │ zss │ 23 │ BJ │ M │
│ 2 │ lss │ 24 │ NJ │ M │
│ 3 │ ww │ 24 │ SH │ F │
└─────┴──────┴─────┴──────┴────────┘
clickhouse-client -q 'insert into doit23.tb_insert2 format CSV' < user.txtselect * from tb_insert2;
┌─uid─┬─name─┬─age─┬─city─┬─gender─┐
│ 1 │ zss │ 23 │ BJ │ M │
│ 2 │ lss │ 24 │ NJ │ M │
│ 3 │ ww │ 24 │ SH │ F │
└─────┴──────┴─────┴──────┴────────┘
┌─uid─┬─name─┬─age─┬─city─┬─gender─┐
│ 1 │ zss │ 23 │ BJ │ M │
│ 2 │ lss │ 24 │ NJ │ M │
│ 3 │ ww │ 24 │ SH │ F │
└─────┴──────┴─────┴──────┴────────┘
clickhouse-client
- 更新删除数据只有MergeTree引擎的数据才能修改
alter table test_muta drop partition 'SH';
alter table test_muta delete where id=3;
alter table tb_update update name='李四' where id=2;