ALTER TABLE
This section describes the basic syntax and usage of the SQL statement for modifying a table structure in ClickHouse.
Basic Syntax
ALTER TABLE [database_name].name [ON CLUSTER ClickHouse cluster name] ADD|DROP|CLEAR|COMMENT|MODIFY COLUMN ...
ALTER supports only *MergeTree, Merge, and Distributed engine tables.
Examples:
- Create a table named DB_table1.
CREATE TABLE DB_table1 ON CLUSTER default_cluster(Year UInt16,Quarter UInt8,Month UInt8,DayofMonth UInt8,DayOfWeek UInt8,FlightDate Date,FlightNum String,Div5WheelsOff String,Div5TailNum String)ENGINE = MergeTree() PARTITION BY toYYYYMM(FlightDate) PRIMARY KEY (intHash32(FlightDate)) ORDER BY (intHash32(FlightDate),FlightNum) SAMPLE BY intHash32(FlightDate) SETTINGS index_granularity= 8192;
- Add the test column to table DB_table1.
ALTER TABLE DB_table1 ADD COLUMN test String DEFAULT 'defaultvalue';
Query the table.
desc DB_tables;
- Change the type of the Year column in the DB_table1 table to UInt8.
ALTER TABLE DB_table1 MODIFY COLUMN Year UInt8;
View the table structure.
desc DB_tables;
- Delete the test column from the DB_table1 table.
ALTER TABLE DB_table1 DROP COLUMN test;
Query the table.
desc DB_tables;
- Change the name of the Month column in the DB_table1 table to Month_test.
ALTER TABLE DB_table1 RENAME COLUMN Month to Month_test;
Query the table.
desc DB_tables;
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot