Static type in Cassandra
In this article, we will discuss a static column in which we will able to understand how the static column is useful and what is the actual role. Let’s have a look.
In Cassandra Query Language (CQL) it is a special column that is shared by all the rows of a partition. the static column is very useful when we want to share a column with a single value.
In Cassandra query language static columns are only static within a given partition. The static column uses clustering columns and non-clustering column to declare the static column in table definition.
Let’s understand with an example.
Now, first, we are going to define the static column in a table.
create table bank_emp_record ( Name text, bank_name text static, Id int, primary key(Name, Id) );
Now, we are going to insert data into the bank_emp_record table.
Insert into bank_emp_record(Name, bank_name, Id) values('Ashish', 'Employee bank', 101); Insert into bank_emp_record(Name, bank_name, Id) values('Ashish', 'Employee bank change', 102); Insert into bank_emp_record(Name, bank_name, Id) values('Ashish', 'Employee bank change new', 103);
Here, Employee bank change new will finally updated and shared column value to the same static column.
Now, let’s consider if I don’t insert any value for bank_name then by default it will share the same value. let’s have a look.
BEGIN BATCH Insert into bank_emp_record(Name, Id) values('Ashish', 104); Insert into bank_emp_record(Name, Id) values('Ashish', 105); APPLY BATCH;
In the above-given CQL query, we are using the BATCH statement to insert more rows in a table. Now, let see the final output of the table.
select * from bank_emp_record;
To know about the restriction please follow the link given below to know what type of restriction when we create the static column and how we can use that properly Reference : DataStax
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.