How to create tables in Apache Cassandra?

by flossie.kessler , in category: SQL , a year ago

How to create tables in Apache Cassandra?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dora , a year ago

@flossie.kessler 

To create a table in Apache Cassandra, you can use the CREATE TABLE statement. Here is the basic syntax:

1
2
3
4
5
6
CREATE TABLE table_name (
   column1 datatype PRIMARY KEY,
   column2 datatype,
   column3 datatype,
   ...
);


For example, to create a table named users with three columns (user_id, name, and email), you can use the following statement:

1
2
3
4
5
CREATE TABLE users (
   user_id int PRIMARY KEY,
   name text,
   email text
);


Note that in Cassandra, every table must have a primary key. The primary key is used to uniquely identify each row in the table. In the example above, the user_id column is the primary key.


You can also specify additional options in the CREATE TABLE statement, such as the WITH clause to define the storage engine and compression options, or the AND clause to specify additional properties for the primary key.


For more information and examples, you can refer to the Apache Cassandra documentation.