Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra add column after particular column

I need to alter the table to add a new column after a particular column or as last column, I have been through the document but no luck.

like image 270
Siva Avatar asked Nov 17 '17 18:11

Siva


People also ask

How do I add a new column in Cassandra?

You can add a column in the table by using the ALTER command. While adding column, you have to aware that the column name is not conflicting with the existing column names and that the table is not defined with compact storage option.

Can we alter table in Cassandra?

You can alter a table using the command ALTER TABLE. Given below is the syntax for creating a table.

Does Cassandra support aggregate functions?

In Cassandra, these aggregate functions are pre-defined or in-built functions. Aggregate functions in Cassandra work on a set of rows. Aggregate functions receive values for each row and then return one value for the whole set.


2 Answers

Let's say I'm starting with a table that has this definition:

CREATE TABLE mykeyspace.letterstable (
    column_n TEXT,
    column_b TEXT,
    column_c TEXT,
    column_z TEXT,
    PRIMARY KEY (column_n));

1- Adding a column is a simple matter.

ALTER TABLE mykeyspace.letterstable ADD column_j TEXT;

2- After adding the new column, my table definition will look like this:

desc table mykeyspace.letterstable;

CREATE TABLE mykeyspace.letterstable (
    column_n TEXT,
    column_b TEXT,
    column_c TEXT,
    column_j TEXT,
    column_z TEXT,
    PRIMARY KEY (column_n));

This is because columns in Cassandra are stored by ASCII-betical order, after the keys (so column_n will always be first, because it is the only key). I can't tell Cassandra that I want my new column_j to go after column_z. It's going to put it between column_c and column_z on its own.

like image 79
Aaron Avatar answered Oct 17 '22 15:10

Aaron


Cassandra will store table data based on partition & clustering key.

Standard CQL for adding column:

ALTER TABLE keyspace.table ADD COLUMN column1 columnType;

Running DESC table for a given table via CQLSH does not portray how the data is stored. It will always list the partition key & clustering key first; then the remaining columns in alphabetical order.

https://docs.datastax.com/en/cql/3.1/cql/cql_reference/alter_table_r.html

Cassandra create table won't keep column order

like image 35
juniormint88 Avatar answered Oct 17 '22 14:10

juniormint88