Alter columns
Use the ALTER TABLE
command to add new columns, drop non-primary key columns, or rename a primary key column.
To change the table settings, see Alter table properties. |
Add a column
The ALTER TABLE
command creates the column metadata, adds the column to the table schema, and sets the value to null
for all rows.
The following example adds an integer column named age
to the table cycling.cyclist_alt_stats
:
ALTER TABLE cycling.cyclist_alt_stats ADD cyclist_age int;
You can add any type of column.
This example adds a map
collection column named description
to store a name and description for each race in the cycling.upcoming_calendar
table:
ALTER TABLE cycling.upcoming_calendar
ADD description map<text,text>;
Rename a column
Only primary key columns can be renamed. If you need to rename another column, you must drop and recreate the column.
This example renames the id
primary key column to cyclist_id
:
ALTER TABLE cycling.cyclist_alt_stats RENAME id TO cyclist_id;
Drop a column
You can use ALTER TABLE
to drop (delete) a column from a table.
The column and all of it’s data are deleted. If you need to preserve the data, take action to extract the data before you drop the column.
ALTER TABLE cycling.cyclist_alt_stats DROP cyclist_age;
Verify schema changes
To verify that a column was added, renamed, or removed, use SELECT
or DESCRIBE TABLE
commands, as explained in Check column existence.