31

I'm in a situation where I need to change the the composite primary key as follows:

Old Primary Key: (id, source, attribute_name, updated_at);

New Primary Key I want: (source, id, attribute_name, updated_at);

I issued the following (mysql like) command:

ALTER TABLE general_trend_table 
DROP PRIMARY KEY, 
ADD PRIMARY KEY(source, id, attribute_name, updated_at);

I got the following error:

Bad Request: line 1:38 no viable alternative at input 'PRIMARY'

any idea how to get around this problem? more specifically I want to know is there any way to change the primary key in cassandra?

Hitesh
  • 3,449
  • 8
  • 39
  • 57
aroyc
  • 890
  • 2
  • 13
  • 26

2 Answers2

69

There is no way to change a primary key, as it defines how your data is physically stored.

You can create a new table with the new primary key, copy data from the old one, and then drop the old table.

Mikhail Stepura
  • 3,374
  • 20
  • 16
  • 1
    I tried following using cqlsh, ALTER COLUMNFAMILY mycolumnfamily RENAME key TO key1; and this works perfectly.While trying this I got following suggestions as well. ADD,ALTER,DROP,WITH @Mikhail Can't we use these to alter primary key? – udarakr Mar 18 '14 at 07:38
  • 1
    How do you rename the table? Can you provide a full example? – Justin Thomas Nov 25 '15 at 01:02
1

If there is a possibility to use the old primary key, you could create a materialzied view: http://docs.datastax.com/en/cql/3.3/cql/cql_using/useCreateMV.html But it is only recommendable, if you would need the old key.

CordlessWool
  • 1,388
  • 4
  • 17
  • 35