Single Column Primary Key - Cassandra / CQL Tutorial

How to create table with Single Column Primary Key in Cassandra

The table can be created with single column Primary Key ( Simple Primary Key). As the very first Column in Primary Key is Partition Key, your Primary Key is also the Partition Key. Cassandra use Partition Key to distribute the data across different nodes.

Syntax:

CQLSH:techbrotherstutorials>CREATE TABLE tablename 
             ( 
                          column1 DATATYPE PRIMARY KEY, 
                          column2 DATATYPE, 
                          column3 DATATYPE, 
                          ............., 
                          .............); 

 Or you can use below

CQLSH:techbrotherstutorials>CREATE TABLE tablename 
             ( 
                          column1 DATATYPE, 
                          column2 DATATYPE, 
                          column3 DATATYPE, 
                          ............., 
                          ............., 
                          PRIMARY KEY (column1)); 

             

 

Example :

Let's create a Employee table with Simple Primary Key (EmployeeId of UUid data type) with below script and insert sample data.

CQLSH:techbrotherstutorials>CREATE TABLE employee 
             ( 
                          employeeid UUID PRIMARY KEY, 
                          fname TEXT, 
                          lname TEXT, 
                          address TEXT, 
                          age TINYINT 
             );

  Insret data into Employee table by using CQL Insert statement.

CQLSH:techbrotherstutorials>INSERT INTO employee 
            ( 
                        employeeid, 
                        fname, 
                        lname, 
                        address, 
                        age 
            ) 
            VALUES 
            ( 
                        Uuid(), 
                        'John', 
                        'Doe', 
                        ' ABC Address', 
                        23 
            );

 

Select the data from Employee table by using Select query.

1 comment: