Alter a table column

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 Altering table properties.

Add a column

Add an age column of type int to the table cycling.cyclist_alt_stats.

ALTER TABLE cycling.cyclist_alt_stats 
ADD cyclist_age int;

The ALTER TABLE command creates the column metadata, adds the column to the table schema, and sets the value to null for all rows.

Verify that a column is added

Verify that the column was added with null values.

  • CQL

  • Result

SELECT id, cyclist_age AS age
FROM cycling.cyclist_alt_stats
LIMIT 3;
 id                                   | age
--------------------------------------+------
 e0953617-07eb-4c82-8f91-3b2757981625 | null
 a9e96714-2dd0-41f9-8bd0-557196a44ecf | null
 ed584e99-80f7-4b13-9a90-9dc5571e6821 | null

(3 rows)

The result set shows the first three rows.

Rename a column

Rename the `id` primary key column to `cyclist_id`.
ALTER TABLE cycling.cyclist_alt_stats 
RENAME id TO cyclist_id;

Restriction: Only primary key columns can be renamed.

Add a collection column

Alter the table cycling.upcoming_calendar to add a map named description to store a name and description for each race.

ALTER TABLE cycling.upcoming_calendar 
ADD description map<text,text>;

Verify the collection column

Verify the results:

  • CQL

  • Result

SELECT description
FROM cycling.upcoming_calendar
WHERE year = 2015
  AND month = 6;

Map type columns display in a JSON format.

 description
------------------------------------------------------------------------------
 {'Criterium du Dauphine': 'Easy race', 'Tour du Suisse': 'Hard uphill race'}

(1 rows)

Related information:

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com