How to perform CRUD opearation on Cassandra using CQLSH

This article will show the useful commands to perform CRUD operation on Cassandra using CQLSH.

1) Insert the data

Syntax is:

insert into <TABLE_NAME>(COLUMN1, COLUMN2) values (VALUES1, VALUES2);

The table structure to insert the data is:

CREATE TABLE cassandrademo.users (
user_id int PRIMARY KEY,
user_email text,
user_name text,
user_pwd text
)
Use the following command to insert the values in the users table.
insert into users(user_id, user_email, user_name, user_pwd) values (1, '22@gmail.com', 'FondOfIt', 'fondpwd');
Now, verify the output by reading the data
select * from users;
Output:
user_id | user_email | user_name | user_pwd
---------+--------------+-----------+----------
1 | 22@gmail.com | FondOfIt | fondpwd
2) Read the data
Syntax is:
select * from <TABLE_NAME>;
Use the following command to read the data from the table:
select * from users;
Output:
user_id | user_email | user_name | user_pwd
---------+--------------+-----------+----------
1 | 22@gmail.com | FondOfIt | fondpwd
3) Update the data
Syntax is:
update <TABLE_NAME> set <COLUMN_NAME> = <VALUE> where <CONDITION>;
Use the following command to update the data in the table:
update users set user_email='33@gmail.com' where user_id=1;
Verify the output:
select * from users:
Before update:
user_id | user_email | user_name | user_pwd
---------+--------------+-----------+----------
1 | 22@gmail.com | FondOfIt | fondpwd
After update:
user_id | user_email | user_name | user_pwd
---------+--------------+-----------+----------
1 | 33@gmail.com | FondOfIt | fondpwd
4) Delete the data
Delete the data
Syntax:
delete <COLUMN_NAME> from <TABLE_NAME> where <CONDITION>;
Use the following command to delete the data from the table:
delete user_email from users where user_id=1;
Verify the output:
select * from users;
Output:
user_id | user_email | user_name | user_pwd
---------+------------+-----------+----------
1 | null | FondOfIt | fondpwd
Delete the entire row
Syntax:
delete from <TABLE_NAME> where <CONDITION>;
Use the following command to delete the data from the table:
delete from users where user_id=1;
Verify the output:
select * from users;
Output:
user_id | user_email | user_name | user_pwd
---------+------------+-----------+----------
(0 rows)
Delete all the rows in the table
Syntax:
truncate <TABLE_NAME>;
Use the following command to delete the data from the table:
truncate users;
Verify the output:
select * from users;
Output:
user_id | user_email | user_name | user_pwd
---------+------------+-----------+----------
(0 rows)

Leave a Reply

Your email address will not be published. Required fields are marked *