Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change column name in a table in Clickhouse

Tags:

clickhouse

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.

like image 498
Yind Avatar asked Nov 17 '17 11:11

Yind


People also ask

How do I rename a column in a table?

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.

Can we change the column name of an existing table?

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

How can I change the column name in existing table in SQL?

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.


2 Answers

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.

like image 57
simPod Avatar answered Sep 30 '22 01:09

simPod


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

like image 22
Slach Avatar answered Sep 29 '22 23:09

Slach