Cassandra Query Language (CQL) - Create Data
Cassandra Query Language (CQL) is the database language used to manipulate data in Apache Cassandra. In this tutorial, we will learn about creating data in Cassandra using CQL.
Syntax
The syntax for creating data in Cassandra using CQL is:
INSERT INTO <table_name> (<column1> <operator> <value1>, <column2> <operator> <value2>, ...) VALUES (<value1>, <value2>, ...);
Example
Suppose we have a keyspace called my_keyspace
and a table called users
with the following columns: id
, first_name
, last_name
, and age
. We can insert a new row of data into this table using the following CQL statement:
INSERT INTO my_keyspace.users (id, first_name, last_name, age) VALUES (1, 'John', 'Doe', 35);
Output
If the query is successful, Cassandra will insert the new row of data into the users
table. No output is generated for this operation.
Explanation
The INSERT INTO
statement tells Cassandra that we want to add a new row of data to the specified table. We then provide a list of column names and values for the new row using the VALUES
keyword.
In the example, we are inserting a new row of data into the users
table with id
equal to 1
, first_name
equal to 'John'
, last_name
equal to 'Doe'
, and age
equal to 35
.
Use
CQL is used to create, read, update, and delete data in Cassandra. The INSERT INTO
statement is used to create new rows of data in an existing table.
Important Points
- The
INSERT INTO
statement is used to add new rows of data to an existing table. - The
VALUES
keyword is used to provide values for each column in the new row of data. - Make sure the values being inserted match the data types of the columns.
- Ensure that the primary key columns are unique, as Cassandra will not allow duplicates.
Summary
In this tutorial, we learned about creating data in Cassandra using CQL. We saw the syntax for inserting a new row of data into a table and how to specify column names and values for the new row. We also covered the output and important points to keep in mind when using the INSERT INTO
statement in CQL.