Altering columns in a table

Adding or changing columns to a table with the ALTER TABLE command.

The ALTER TABLE command can be used to add new columns to a table and to alter the column type of an existing column.

Procedure

  • Add a age column of type int to the table cycling.cyclist_alt_stats.
    cqlsh> ALTER TABLE cycling.cyclist_alt_stats ADD age int;

    This creates the column metadata and adds the column to the table schema, and sets the value to NULL for all rows.



  • Add a column favorite_color of varchar, and then change the data type of the same column to text.
    cqlsh> ALTER TABLE cycling.cyclist_alt_stats ADD favorite_color varchar;
    ALTER TABLE cycling.cyclist_alt_stats ALTER favorite_color TYPE text;
    Note: There are limitations on altering the data type of a column. The two data types, the original and the one changing to, must be compatible.