Is there any way to ALTER a table and change the column name in clickhouse? I only found to change tha table name but not for an individual column in a straight forward way.
Thanks.
To change a column name, enter the following statement in your MySQL shell: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; Replace table_name , old_column_name , and new_column_name with your table and column names.
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
ALTER TABLE table_name RENAME TO new_table_name; Columns can be also be given new name with the use of ALTER TABLE. QUERY: Change the name of column NAME to FIRST_NAME in table Student.
The feature has been introduced here into v20.4.
ALTER TABLE table1 RENAME COLUMN old_name TO new_name
You can also rename multiple columns at on:
ALTER TABLE table1
RENAME COLUMN old_name1 TO new_name1,
RENAME COLUMN old_name2 TO new_name2
Old answer:
ClickHouse doesn't have that feature yet.
Implementation is not trivial, because ALTERs that changing columns are processed outside of usual replication queue, and adding rename without reworking of ALTERs will introduce race conditions in replicated tables.
https://github.com/yandex/ClickHouse/issues/146#issuecomment-255631384
As @Slash said, the solution for now is to create new table and
INSERT INTO `new_table` SELECT * FROM `old_table`
Do not forget that column aliasing won't work there (AS
).
INSERT INTO `new_table` SELECT a, b AS c, c AS b FROM `old_table`
That will still insert a
into first column, b
into second column and c
into third column. AS
has no effect there.
You can try use CREATE TABLE new_table with another field name and run INSERT INTO new_table SELECT old_field AS new_field FROM old_table
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With