Cassandra Create Table

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Cassandra Create Table

In Cassandra, CREATE TABLE command is used to create a table. Here, column family is used to store data just like table in RDBMS.

So, you can say that CREATE TABLE command is used to create a column family in Cassandra.

Syntax:

  1. CREATE (TABLE | COLUMNFAMILY) <tablename>  
  2. ('<column-definition>' , '<column-definition>')  
  3. (WITH <optionAND <option>)   

Or

For declaring a primary key:

  1. CREATE TABLE tablename(  

  2.    column1 name datatype PRIMARYKEY,  

  3.    column2 name data type,  

  4.    column3 name data type.  

  5.    )  

You can also define a primary key by using the following syntax:

  1. Create table TableName  

  2. (  

  3. ColumnName DataType,  

  4. ColumnName DataType,  

  5. ColumnName DataType  

  6. .  

  7. .  

  8. .  

  9. Primary key(ColumnName)  

  10. ) with PropertyName=PropertyValue;   

There are two types of primary keys:

  1. Primary key (ColumnName)   

  1. Primary key(ColumnName1,ColumnName2 . . .)    

Example:

Let's take an example to demonstrate the CREATE TABLE command.

Here, we are using already created Keyspace "javatpoint".

  1. CREATE TABLE student(  

  2.    student_id int PRIMARY KEY,  

  3.    student_name text,  

  4.    student_city text,  

  5.    student_fees varint,  

  6.    student_phone varint  

  7.    );   

Cassandra Create table 1

The table is created now. You can check it by using the following command.

  1. SELECT * FROM student;  

  • Single primary key: Use the following syntax for single primary key.

  • Compound primary key: Use the following syntax for single primary key.