Overview
YCQL supports a comprehensive set of Data Definition Language (DDL) and Data Manipulation Language (DML) statements compatible with Apache Cassandra CQL.Data Definition Language (DDL)
CREATE KEYSPACE
Creates a new keyspace (database). Syntax:replication_factor: Number of replicas (default: 3)DURABLE_WRITES: Enable/disable commit log (default: true)
ALTER KEYSPACE
Modifies an existing keyspace. Syntax:DROP KEYSPACE
Deletes a keyspace and all its tables. Syntax:USE
Sets the current keyspace for the session. Syntax:CREATE TABLE
Creates a new table. Syntax:-
transactions: Enable ACID transactions -
default_time_to_live: Automatic row expiration in seconds -
CLUSTERING ORDER BY: Define clustering column sort order
ALTER TABLE
Modifies an existing table structure. Syntax:- Cannot alter primary key columns
- Cannot change column types
- Cannot add/remove columns from primary key
DROP TABLE
Deletes a table. Syntax:CREATE INDEX
Creates a secondary index on a column. Syntax:- Table must have
transactions = {'enabled': true} - Cannot index collection columns
- Cannot index primary key columns
DROP INDEX
Deletes a secondary index. Syntax:TRUNCATE
Removes all rows from a table. Syntax:Data Manipulation Language (DML)
INSERT
Inserts a new row into a table. Syntax:UPDATE
Modifies existing rows in a table. Syntax:DELETE
Removes rows or specific columns from a table. Syntax:SELECT
Retrieves rows from a table. Syntax:=- Equality>,>=,<,<=- Comparison (clustering columns only)IN- Multiple valuesCONTAINS- Collection membership (with ALLOW FILTERING)CONTAINS KEY- Map key existence (with ALLOW FILTERING)
COUNT(*)orCOUNT(column)- Row countSUM(column)- Sum of valuesMIN(column)- Minimum valueMAX(column)- Maximum valueAVG(column)- Average value
Batch Operations
BATCH
Executes multiple DML statements atomically. Syntax:- LOGGED (default): Atomic batch with performance overhead
- UNLOGGED: Non-atomic batch with better performance
- Keep batch size small (< 100 statements)
- Use UNLOGGED for same-partition writes
- Avoid cross-partition batches when possible
Prepared Statements
Bind Variables
Use placeholders for values to improve performance and security. Syntax:Transaction Control
For tables with transactions enabled:Common Patterns
Time-Series Data
User Sessions
Counters and Statistics
Best Practices
- Always specify partition key in WHERE: Avoid full table scans
- Use prepared statements: Better performance and security
- Limit batch sizes: Keep batches small for better performance
- Design for query patterns: Model data based on how you’ll query it
- Use TTL for temporary data: Automatic cleanup
- Enable transactions only when needed: Adds overhead
- Use ALLOW FILTERING sparingly: Can cause performance issues

