Truncate Table in Cassandra
In Cassandra, a table can be truncated to remove all data from it while preserving the schema. This operation is useful when we need to clear the data from a table without dropping and recreating it.
Syntax
The syntax for truncating a table in Cassandra is:
TRUNCATE table_name;
Where table_name
is the name of the table that we want to truncate.
Example
Suppose we have a table named user_profiles
, and we want to delete all data from it. We can use the TRUNCATE
command to achieve this.
TRUNCATE user_profiles;
Output
When the TRUNCATE
command is executed, all data from the specified table (user_profiles
in this example) is deleted.
Explanation
The TRUNCATE
command deletes all data from a table while preserving its schema. This means that the table will still exist, and we can insert data into it again in the future.
It is important to note that the TRUNCATE
command is a non-transactional operation, which means that it cannot be rolled back. Once the command is executed, all data from the specified table is deleted, and it cannot be recovered.
Use
The TRUNCATE
command is useful when we want to clear the data from a table quickly without dropping and recreating it. It can be used in scenarios where we have a large amount of data that we need to delete or when we need to remove all data from a table for testing purposes.
Important Points
- The
TRUNCATE
command is a non-transactional operation and cannot be rolled back. - The command deletes all data from the specified table while preserving its schema.
- It is important to use the
TRUNCATE
command carefully and only when we are sure that we want to delete all data from the table.
Summary
In this tutorial, we learned about the TRUNCATE
command in Cassandra, which is used to delete all data from a table while preserving its schema. We saw the syntax of the command, an example of how it can be used, and the output that it produces. We also discussed the importance of using this command carefully and only when necessary.