Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Cassandra: DELETE: Delete a row or column from table

You can delete a row from cassandra table using 'DELETE FROM' statement.

Syntax:
delete-stmt> ::= DELETE (  ( ','  )* )?
FROM
( USING TIMESTAMP integer>)?
WHERE where-clause>
( IF ( EXISTS | ( ( AND )*) ) )?

::=
| '[' ']'
| '.' field>

where-clause> ::= ( AND )*

::=
| '(' (',' )* ')'
| IN '(' ( ( ',' )* )? ')'
| IN variable>
| '(' (',' )* ')' IN '(' ( ( ',' )* )? ')'
| '(' (',' )* ')' IN variable>

::= '=' | ' | '>' | ' | '>='
in
-values> ::= (variable
> | '(' ( ( ',' )* )? ')')

::= ( | '!=')
| IN in-values>
| '[' ']' ( | '!=')
| '[' ']' IN in-values>
| '.' field> ( | '!=')
| '.' field> IN in-values>

cqlsh> SELECT * FROM cassandratutorial.employee;

id | age | firstname | lastname
----+-----+-----------+----------
1 | 31 | Hari | Gurram
2 | 31 | Ram | Gurram
3 | 33 | Ram | Ponnam

Delete a row from table employee
cqlsh> SELECT * FROM cassandratutorial.employee;

id | age | firstname | lastname
----+-----+-----------+----------
1 | 31 | Hari | Gurram
2 | 31 | Ram | Gurram
3 | 33 | Ram | Ponnam

(3 rows)
cqlsh> DELETE FROM cassandratutorial.employee WHERE id=2;
cqlsh>
cqlsh> SELECT * FROM cassandratutorial.employee;

id | age | firstname | lastname
----+-----+-----------+----------
1 | 31 | Hari | Gurram
3 | 33 | Ram | Ponnam

(2 rows)

Delete a column from table employee
DELETE age from cassandratutorial.employee WHERE id=3;

Above statement deletes the column age from the employee table for the id=3.

cqlsh> SELECT * FROM cassandratutorial.employee;

id | age | firstname | lastname
----+-----+-----------+----------
1 | 31 | Hari | Gurram
3 | 33 | Ram | Ponnam

(2 rows)
cqlsh>
cqlsh> DELETE age from cassandratutorial.employee WHERE id=3;
cqlsh>
cqlsh> SELECT * FROM cassandratutorial.employee;

id | age | firstname | lastname
----+------+-----------+----------
1 | 31 | Hari | Gurram
3 | null | Ram | Ponnam

(2 rows)

You can also delete particular column by updating its value to null. For example, below statement deletes lastname column for the employee with id=1.
UPDATE cassandratutorial.employee SET lastname=null WHERE id=1;

cqlsh> SELECT * FROM cassandratutorial.employee;

id | age | firstname | lastname
----+------+-----------+----------
1 | 31 | Hari | Gurram
3 | null | Ram | Ponnam

(2 rows)
cqlsh> UPDATE cassandratutorial.employee SET lastname=null WHERE id=1;
cqlsh>
cqlsh> SELECT * FROM cassandratutorial.employee;

id | age | firstname | lastname
----+------+-----------+----------
1 | 31 | Hari | null
3 | null | Ram | Ponnam

(2 rows)

You can even perform the same using INSERT statement.

cqlsh> SELECT * FROM cassandratutorial.employee;

id | age | firstname | lastname
----+------+-----------+----------
1 | 31 | Hari | null
3 | null | Ram | Ponnam

(2 rows)
cqlsh>
cqlsh> INSERT INTO cassandratutorial.employee JSON '{"id" : 4, "firstName" : "Chitra", "lastName" : null, "age" : 45}';
cqlsh>
cqlsh> SELECT * FROM cassandratutorial.employee;

id | age | firstname | lastname
----+------+-----------+----------
1 | 31 | Hari | null
4 | 45 | Chitra | null
3 | null | Ram | Ponnam

(3 rows)


Previous                                                    Next                                                    Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Cassandra: DELETE: Delete a row or column from table

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×