Cassandra Tutorial
Cassandra Keyspace
Cassandra Table Index
Cassandra Query (CQL)
Cassandra Collections
Cassandra Interview
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:
Polymorphism in Java | Dynamic Method Dispatch
- CREATE (TABLE | COLUMNFAMILY) <tablename>
- ('<column-definition>' , '<column-definition>')
- (WITH <option> AND <option>)
Or
For declaring a primary key:
- CREATE TABLE tablename(
- column1 name datatype PRIMARYKEY,
- column2 name data type,
- column3 name data type.
- )
You can also define a primary key by using the following syntax:
- Create table TableName
- (
- ColumnName DataType,
- ColumnName DataType,
- ColumnName DataType
- .
- .
- .
- Primary key(ColumnName)
- ) with PropertyName=PropertyValue;
There are two types of primary keys:
- Primary key (ColumnName)
- Primary key(ColumnName1,ColumnName2 . . .)
Example:
Let's take an example to demonstrate the CREATE TABLE command.
Here, we are using already created Keyspace "javatpoint".
- CREATE TABLE student(
- student_id int PRIMARY KEY,
- student_name text,
- student_city text,
- student_fees varint,
- student_phone varint
- );
The table is created now. You can check it by using the following command.
- 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.