Role of keys in Cassandra

27 Nov, 2019
In this article we are going to discuss why keys are important and how they are work and different from relational database. Basically, Keys are used for grouping and organizing data into columns and rows in the database, so let’s have a look.
There are many portioning keys are available in Cassandra.1. Simple Primary key
2. Composite key
3. Using a compound primary key Let’s discuss the concept of partitioning key one by one.1. Simple Primary key:In a basic primary key one column uses for column name as the partition key. In this case primary key consists of only the partition key. Only primary key can be specified when retrieving data from the table.CREATE KEYSPACE Employee
WITH REPLICATION = { 'class' : 'SimpleStrategy', 
                     'replication_factor' : 1 };USE Employee;CREATE TABLE Employee_info ( 
Employee_id UUID PRIMARY KEY, 
Emp_name text, 
Emp_domain text 
);
2. Composite key:In Cassandra composite partition key, we can use for more sorted row with the help of key. Let’s take an example to understand. Below given Employee_name is a part of primary key which is called the composite partition key for Employee_info table.CREATE TABLE Employee_info
(
Employee_id int,
Employee_name text,
Employee_domain text,
Employee_add text
PRIMARY KEY ((Emplyee_id, Employee_name), Employee_domain) 
); In the above given table Employee_id and Employee_name used for composite partition key. Here Employee_domain extra column in primary key to return sorted result. let’s take a scenario where clustering column more helpful.INSERT INTO Employee_info(Employee_id, Employee_name, 
                           Employee_domain, Employee_add) 
        VALUES (1, ‘Ashish’, ‘A’, ‘Delhi’);
INSERT INTO Employee_info(Employee_id, Employee_name, 
                          Employee_domain, Employee_add) 
       VALUES (1, ‘Ashish’, ‘B’, ‘Mumbai’);
INSERT INTO Employee_info(Employee_id, Employee_name, 
                          Employee_domain, Employee_add ) 
       VALUES (2, ‘Ashish’, ‘C’, ‘Hyd’); Select * 
from Employee_info; Output:Employee_idEmployee_nameEmployee_domainEmployee_add1AshishADelhi1AshishBMumbai2AshishCHydIt shows with the help of clustering key column we can stored data in sorted way what we are actually expecting it is exact result what we return with the help of Employee_domain column in sorted order.3. Using a Compound primary key:Use a compound primary key to create multiple columns that you can use to query and return sorted results.Let’s take an example of Employee_info table where we will de-normalize the data. To create a table having a compound primary key, use two or more columns as the primary key.Let’s take an example which uses an additional clause WITH CLUSTERING ORDER BY to order the Employee_points in descending order.CREATE TABLE Employee.Employee_info ( 
Employee_name text, 
Employee_points int, 
Employee_id UUID,  
PRIMARY KEY (Employee_name, Employee_points)
); Let’s take an example which uses an additional clause WITH CLUSTERING ORDER BY to order the Employee_points in descending order.CREATE TABLE Employee.Employee_info ( 
Employee_name text, 
Employee_points int, 
Employee_id int,  
PRIMARY KEY (Employee_name, Employee_points)
) WITH CLUSTERING ORDER BY (Employee_points DESC); Now, let’s insert data into table Employee_info and used the following CQL query for the same.INSERT INTO Employee_info (Employee_name, Employee_points, 
                                               Employee_id) 
        VALUES (‘Ashish’, 90, 1);
INSERT INTO Employee_info (Employee_name, Employee_points, 
                                          Employee_id ) 
       VALUES (‘Rana’, 95, 2);
INSERT INTO Employee_info(Employee_name, Employee_points, 
                                         Employee_id) 
       VALUES (‘Ashish’, 85, 3); Select * 
from Employee_info; Output:Employee_nameEmployee_pointsEmployee_idRana952Ashish901Ashish853My Personal Notes
arrow_drop_upSave
1. Simple Primary key
2. Composite key
3. Using a compound primary key 
Let’s discuss the concept of partitioning key one by one.
1. Simple Primary key:In a basic primary key one column uses for column name as the partition key. In this case primary key consists of only the partition key. Only primary key can be specified when retrieving data from the table.
CREATE KEYSPACE Employee
WITH REPLICATION = { 'class' : 'SimpleStrategy', 
                     'replication_factor' : 1 };
USE Employee;
CREATE TABLE Employee_info ( 
Employee_id UUID PRIMARY KEY, 
Emp_name text, 
Emp_domain text 
);

2. Composite key:In Cassandra composite partition key, we can use for more sorted row with the help of key. Let’s take an example to understand. Below given Employee_name is a part of primary key which is called the composite partition key for Employee_info table.
CREATE TABLE Employee_info
(
Employee_id int,
Employee_name text,
Employee_domain text,
Employee_add text
PRIMARY KEY ((Emplyee_id, Employee_name), Employee_domain) 
); 
In the above given table Employee_id and Employee_name used for composite partition key. Here Employee_domain extra column in primary key to return sorted result. let’s take a scenario where clustering column more helpful.
INSERT INTO Employee_info(Employee_id, Employee_name, 
                           Employee_domain, Employee_add) 
        VALUES (1, ‘Ashish’, ‘A’, ‘Delhi’);
INSERT INTO Employee_info(Employee_id, Employee_name, 
                          Employee_domain, Employee_add) 
       VALUES (1, ‘Ashish’, ‘B’, ‘Mumbai’);
INSERT INTO Employee_info(Employee_id, Employee_name, 
                          Employee_domain, Employee_add ) 
       VALUES (2, ‘Ashish’, ‘C’, ‘Hyd’); 
Select * 
from Employee_info; 
Output:
3. Using a Compound primary key:Use a compound primary key to create multiple columns that you can use to query and return sorted results.Let’s take an example of Employee_info table where we will de-normalize the data. To create a table having a compound primary key, use two or more columns as the primary key.Let’s take an example which uses an additional clause WITH CLUSTERING ORDER BY to order the Employee_points in descending order.
CREATE TABLE Employee.Employee_info ( 
Employee_name text, 
Employee_points int, 
Employee_id UUID,  
PRIMARY KEY (Employee_name, Employee_points)
); 
Let’s take an example which uses an additional clause WITH CLUSTERING ORDER BY to order the Employee_points in descending order.
CREATE TABLE Employee.Employee_info ( 
Employee_name text, 
Employee_points int, 
Employee_id int,  
PRIMARY KEY (Employee_name, Employee_points)
) WITH CLUSTERING ORDER BY (Employee_points DESC); 
Now, let’s insert data into table Employee_info and used the following CQL query for the same.
INSERT INTO Employee_info (Employee_name, Employee_points, 
                                               Employee_id) 
        VALUES (‘Ashish’, 90, 1);
INSERT INTO Employee_info (Employee_name, Employee_points, 
                                          Employee_id ) 
       VALUES (‘Rana’, 95, 2);
INSERT INTO Employee_info(Employee_name, Employee_points, 
                                         Employee_id) 
       VALUES (‘Ashish’, 85, 3); 
Select * 
from Employee_info; 
Output:
Apache
DBMS
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
CTE in SQL
Difference between Clustered and Non-clustered index
Introduction of DBMS (Database Management System) | Set 1
SQL Trigger | Student Database
Introduction of B-Tree
SQL Interview Questions
SQL | Views
Introduction of ER Model
Data Preprocessing in Data Mining
Difference between DELETE, DROP and TRUNCATE