Cassandra Crud Operation – Create, Update, Read & Delete
Placement-ready Courses: Enroll Now, Thank us Later!
1. Objective – Crud Operation
In this Cassandra Tutorial, we will learn about the Cassandra CRUD Operation: Create, Update, Read & Delete. Moreover, we will cover the syntax and example of each CRUD operation in Cassandra.
So, let’s start with Cassandra CRUD Operation.
Test how much you know about Cassandra
2. What is Cassandra CRUD Operation?
Cassandra CRUD Operation stands for Create, Update, Read and Delete or Drop. These operations are used to manipulate data in Cassandra. Apart from this, CRUD operations in Cassandra, a user can also verify the command or the data.
a. Create Operation
AÂ user can insert data into the table using Cassandra CRUD operation. The data is stored in the columns of a row in the table. Using INSERT command with proper what, a user can perform this operation.
Read about Important Features of CassandraÂ
A Syntax of Create Operation-
INSERT INTO <table name> (<column1>,<column2>....) VALUES (<value1>,<value2>...) USING<option>
Let’s create a table data to illustrate the operation. Example consist of a table with information about students in college. The following table will give the details about the students.
Table.1 Cassandra Crud Operation – Create Operation
EN | NAME | BRANCH | PHONE | CITY |
001 | Ayush | Electrical Engineering | 9999999999 | Boston |
002 | Aarav | Computer Engineering | 8888888888 | New York City |
003 | Kabir | Applied Physics | 7777777777 | Philadelphia |
EXAMPLE 1: Creating a table and inserting the data into a table:
INPUT:
cqlsh:keyspace1> INSERT INTO student(en, name, branch, phone, city) VALUES(001, 'Ayush', 'Electrical Engineering', 9999999999, 'Boston'); cqlsh:keyspace1> INSERT INTO student(en, name, branch, phone, city) VALUES(002, 'Aarav', 'Computer Engineering', 8888888888, 'New York City'); cqlsh:keyspace1> INSERT INTO student(en, name, branch, phone, city) VALUES(003, 'Kabir', 'Applied Physics', 7777777777, 'Philadelphia');
Let’s Learn Cassandra Architecture in detail
Table.2 Cassandra Crud Operation – OUTPUT After Verification (READ operation)
EN | NAME | BRANCH | PHONE | CITY |
001 | Ayush | Electrical Engineering | 9999999999 | Boston |
002 | Aarav | Computer Engineering | 8888888888 | New York City |
003 | Kabir | Applied Physics | 7777777777 | Philadelphia |
b.Update Operation
The second operation in the Cassandra CRUD operation is the UPDATE operation. A user can use UPDATE command for the operation. This operation uses three keywords while updating the table.
- Where: This keyword will specify the location where data is to be updated.
- Set: This keyword will specify the updated value.
- Must: This keyword includes the columns composing the primary key.
Furthermore, at the time of updating the rows, if a row is unavailable, then Cassandra has a feature to create a fresh row for the same.
Do you know How Cassandra Stores Data
A Syntax of Update Operation-
UPDATE <table name> SET <column name>=<new value> <column name>=<value>... WHERE <condition>
EXAMPLE 2: Let’s change few details in the table ‘student’. In this example, we will update Aarav’s city from ‘New York City’ to ‘San Fransisco’.
INPUT:
cqlsh:keyspace1> UPDATE student SET city='San Fransisco' WHERE en=002;
Table.3 Cassandra Crud Operation – OUTPUT After VerificationÂ
EN | NAME | BRANCH | PHONE | CITY |
001 | Ayush | Electrical Engineering | 9999999999 | Boston |
002 | Aarav | Computer Engineering | 8888888888 | San Fransisco |
003 | Kabir | Applied Physics | 7777777777 | Philadelphia |
c. Read Operation
This is the third Cassandra CRUD Operation – Read Operation. A user has a choice to read either the whole table or a single column. To read data from a table, a user can use SELECT clause. This command is also used for verifying the table after every operation.
Have a look at Cassandra Shell Commands
SYNTAX to read the whole table-
SELECT * FROM <table name>;
EXAMPLE 3: To read the whole table ‘student’.
INPUT:
cqlsh:keyspace1> SELECT * FROM student;
Table.4 Cassandra Crud Operation – OUTPUT After VerificationÂ
EN | NAME | BRANCH | PHONE | CITY |
001 | Ayush | Electrical Engineering | 9999999999 | Boston |
002 | Aarav | Computer Engineering | 8888888888 | San Fransisco |
003 | Kabir | Applied Physics | 7777777777 | Philadelphia |
SYNTAX to read selected columns-
SELECT <column name1>,<column name2>.... FROM <table name>;
EXAMPLE 4: To read columns of name and city from table ‘student’.
INPUT:
cqlsh:keyspace1> SELECT name, city FROM student;
Read More about Why Cassandra Is So Popular?
Table.5 Cassandra Crud Operation – OUTPUT After VerificationÂ
NAME | CITY |
Ayush | Boston |
Aarav | San Fransisco |
Kabir | Philadelphia |
d. Delete Operation
Delete operation is the last Cassandra CRUD Operation, allows a user to delete data from a table. The user can use DELETE command for this operation.
A Syntax of Delete Operation-
DELETE <identifier> FROM <table name> WHERE <condition>;
EXAMPLE 5: In the ‘student’ table let us delete the ‘phone’ or phone number from 003 row.
cqlsh:keyspace1> DELETE phone FROM student WHERE en=003;
Table.6 Cassandra Crud Operation – OUTPUT After VerificationÂ
EN | NAME | BRANCH | PHONE | CITY |
001 | Ayush | Electrical Engineering | 9999999999 | Boston |
002 | Aarav | Computer Engineering | 8888888888 | San Fransisco |
003 | Kabir | Applied Physics | null | Philadelphia |
SYNTAX for deleting the entire row-
DELETE FROM <identifier> WHERE <condition>;
EXAMPLE 6: In the ‘student’ table, let us delete the entire third row.
cqlsh:keyspace1> DELETE FROM student WHERE en=003;
Let’s Explore Best Books To Learn Cassandra
Table.7 Cassandra Crud Operation – OUTPUT After VerificationÂ
EN | NAME | BRANCH | PHONE | CITY |
001 | Ayush | Electrical Engineering | 9999999999 | Boston |
002 | Aarav | Computer Engineering | 8888888888 | San Fransisco |
So, this was all about Cassandra CRUD operations Tutorial. Hope you like our explanation.
3. Conclusion
Hence, in this article, we studied the CRUD operations in Cassandra. So, by using these operations, a user can make many applications or program in Cassandra. Furthermore, if you have any query, feel free to ask in the comment section.Â
See also –Â
You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google
This page also should refer a link for creating table and other metadata first
can u create an article for cassandra crude operation using python
can u create an article for cassandra crude operation using python