只有MergeTree系列、Merge、Distributed表引擎支持alter操作
clickhouse1 :)
clickhouse1 :) create table alter_table_test(
:-] id UInt32,
:-] name String,
:-] city String
:-] ) engine = MergeTree()
:-] order by id;
clickhouse1 :)
clickhouse1 :) alter table alter_table_test add column if not exists score Float32 default 8.8 after city;
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) alter table alter_table_test modify column if exists score Float64 default 0.0;
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) alter table alter_table_test comment column if exists score '分数';
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) alter table alter_table_test drop column if exists score;
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) rename table default.alter_table_test to default.alter_table_rename_test;
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) truncate table if exists default.alter_table_rename_test;
clickhouse1 :)
测试表和测试数据的准备
clickhouse1 :)
clickhouse1 :) create table partition_table_test(
:-] id UInt32,
:-] name String,
:-] city String
:-] ) engine = MergeTree()
:-] order by id
:-] partition by city;
clickhouse1 :)
clickhouse1 :) insert into partition_table_test(id, name, city) values(1, 'name1', 'Beijing');
clickhouse1 :) insert into partition_table_test(id, name, city) values(2, 'name2', 'Shanghai');
clickhouse1 :)
clickhouse1 :) create table partition_table_test2(
:-] id UInt32,
:-] name String,
:-] city String
:-] ) engine = ReplacingMergeTree()
:-] order by id
:-] partition by city;
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) select database, table, partition, partition_id, name, path from system.parts where database = 'default' and table = 'partition_table_test';
┌─database─┬─table────────────────┬─partition─┬─partition_id─────────────────────┬─name───────────────────────────────────┬─path────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ default │ partition_table_test │ Shanghai │ 6a9748c898bf80cb661db240706867aa │ 6a9748c898bf80cb661db240706867aa_2_2_0 │ /root/clickhouse/store/9eb/9ebd4336-b065-48ac-9ebd-4336b06588ac/6a9748c898bf80cb661db240706867aa_2_2_0/ │
│ default │ partition_table_test │ Beijing │ 8d2db6c332407299b732139fd8a261c0 │ 8d2db6c332407299b732139fd8a261c0_1_1_0 │ /root/clickhouse/store/9eb/9ebd4336-b065-48ac-9ebd-4336b06588ac/8d2db6c332407299b732139fd8a261c0_1_1_0/ │
└──────────┴──────────────────────┴───────────┴──────────────────────────────────┴────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) alter table partition_table_test drop partition 'Beijing'
:-] ;
clickhouse1 :)
clickhouse1 :) select * from partition_table_test;
┌─id─┬─name──┬─city─────┐
│ 2 │ name2 │ Shanghai │
└────┴───────┴──────────┘
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) alter table partition_table_test2 replace partition 'Shanghai' from partition_table_test;
clickhouse1 :)
clickhouse1 :) select * from partition_table_test2;
┌─id─┬─name──┬─city─────┐
│ 2 │ name2 │ Shanghai │
└────┴───────┴──────────┘
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) alter table partition_table_test clear column name in partition 'Shanghai';
clickhouse1 :)
clickhouse1 :) select * from partition_table_test;
┌─id─┬─name─┬─city─────┐
│ 2 │ │ Shanghai │
└────┴──────┴──────────┘
clickhouse1 :)
clickhouse1 :)
clickhouse1 :) alter table partition_table_test detach partition 'Shanghai';
clickhouse1 :)
clickhouse1 :) select * from partition_table_test;
SELECT *
FROM partition_table_test
Query id: 45460933-7b2e-4389-a056-85d3d75184a8
Ok.
0 rows in set. Elapsed: 0.005 sec.
clickhouse1 :)
clickhouse1 :) alter table partition_table_test attach partition 'Shanghai';
clickhouse1 :)
clickhouse1 :) select * from partition_table_test;
┌─id─┬─name─┬─city─────┐
│ 2 │ │ Shanghai │
└────┴──────┴──────────┘
clickhouse1 :)