How to Add a Column in ClickHouse
To add a column to a table in ClickHouse, use the ALTER TABLE
statement in combination with ADD COLUMN
.
This example adds the column my_new_column
of type TEXT
to the existing table my_table
:
ALTER TABLE my_table ADD COLUMN my_new_column TEXT;
Include the IF NOT EXISTS
clause if you don’t want the query to return an error in case my_new_colum
already exists:
ALTER TABLE my_table ADD COLUMN IF NOT EXISTS my_new_column TEXT;
Adding Multiple Columns
You can also add multiple new columns at once:
ALTER TABLE my_table
ADD COLUMN my_new_column_1 TEXT,
ADD COLUMN my_new_column_2 BOOLEAN;
Defining Default Values
This example defines a default value of 'hello'
for the new column.
ALTER TABLE my_table ADD COLUMN my_new_column TEXT DEFAULT 'hello';