ClickHouse修改表字段
本文中使用的表引擎爲:
MergeTree
。注:部分表引擎,創建後,不可修改表結構。
詳情見官網
1.添加字段
之前在添加表字段時,碰到一個坑。
執行SQL:
-- 錯誤的
alter table event add column '$user_id' Nullable(String);
-- 正確的
alter table event add column `$user_id` Nullable(String);
Code: 62, e.displayText() = DB::Exception: Syntax error: failed at position 34: '$user_id' String AFTER chart_position. Expected one of: IF NOT EXISTS, compound identifier, identifier, column declaration, list of elements (version 20.4.2.9 (official build))
經過排查,發現是因爲'
符號的問題,在SQL中,因爲表字段使用$
開頭,導致不能直接寫。必須要用`這個符號才行。後來改爲下面這樣才成功執行。
2.修改字段
alter table user modify column user_name Nullable(String);
3.刪除字段
alter table user drop column user_name;