Clickhouse所提供的DDL与DML查询,在部分细节上也与其他数据库有所不同,例如update和delete是借助alter变种实现的。如下是几种常用的表操作:
- 改变表名字
rename table base_user to base_user_1;
- 按条件删除数据
可以写复杂where条件
alter table base_user_1 delete where name='Name1';
- 删除字段
对应列名及数据都会被删除
alter table base_user_1 drop column if exists tag;
- 按条件更新数据 (不加where语句运行不了)
alter table test.qobm_1 update __label_valids = bitmapAndnot(__label_valids,
bitmapBuild(dictGet('test.dict_label_valid_arrays', 'label_valid_arrays', 1136)))
where 1;
- 追加新字段
default:取默认值;
after:在哪个具体字段后添加新的字段;
alter table base_user_1 add column if not exists name Nullable(String);
alter table base_user_1 add column if not exists name Nullable(String) default 'china';
alter table base_user_1 add column if not exists name_1 Nullable(String) after tag;
- 添加备注
alter table base_user_1 comment column if exists create_time '创建时间';
- 修改数据类型
如果当前类型与期望类型不能兼容,则修改操作会失败,例如将String类型修改为IPv4类型是可行的,而尝试将String类型转为Uint类型就会出现错误;
alter table base_user_1 modify column id UInt32;
alter table yx_mt_01 modify column tag_1156 Nullable(Float32);
-- Nullable(UInt8)->Nullable(Float32);
alter table yx_mt_01 modify column tag_1153 Nullable(String);
-- Nullable(UInt8)->Nullable(String);