How to use a Compound Primary Key In Apache
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.
Query
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 SQL 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_name | Employee_points | Employee_id |
---|---|---|
Rana | 95 | 2 |
Ashish | 90 | 1 |
Ashish | 85 | 3 |
Role of Keys in Cassandra
In this article, we are going to discuss why keys are important and how they are work and different from relational databases. 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 available in Cassandra.
1. Simple Primary key
2. Composite key
3. Using a compound primary key
Let’s discuss the concept of partitioning keys one by one.
Contact Us