Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.

Scylla Java Driver Manual Query builder Schema builder Table

Table¶

Data in Apache Cassandra is stored in tables. SchemaBuilder offers API methods for creating, altering, and dropping tables.

Creating a Table (CREATE TABLE)¶

To start a CREATE TABLE query, use createTable in SchemaBuilder:

import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.*;

CreateTableStart create = createTable("cycling", "cyclist_name");

Like all other CREATE queries, one may supply ifNotExists() to require that the table should only be created if it doesn’t already exist, i.e.:

CreateTableStart create = createTable("cycling", "cyclist_name").ifNotExists();

Note that, at this stage, the query cannot be completed yet. You need to provide at least one partition key column using withPartitionKey(), i.e.:

CreateTable create = createTable("cycling", "cyclist_name").withPartitionKey("id", DataTypes.UUID);
// CREATE TABLE cycling.cyclist_name (id UUID PRIMARY KEY)

A table with only one column is not so typical however. At this point you may provide partition, clustering, regular and static columns using any of the following API methods:

  • withPrimaryKey(name, dataType)

  • withClusteringColumn(name, dataType)

  • withColumn(name, dataType)

  • withStaticColumn(name, dataType)

Primary key precedence is driven by the order of withPrimaryKey and withClusteringKey invocations, for example:

CreateTable create = createTable("cycling", "cyclist_by_year_and_name")
    .withPartitionKey("race_year", DataTypes.INT)
    .withPartitionKey("race_name", DataTypes.TEXT)
    .withClusteringColumn("rank", DataTypes.INT)
    .withColumn("cyclist_name", DataTypes.TEXT);
// CREATE TABLE cycling.cyclist_by_year_and_name (race_year int,race_name text,rank int,cyclist_name text,PRIMARY KEY((race_year,race_name),rank))

After providing the column specification, clustering order and many table options may be provided. Refer to CreateTableWithOptions for the variety of configuration options available.

The following configures compaction and compression options and includes a clustering order.

CreateTableWithOptions create = createTable("cycling", "cyclist_by_year_and_name")
    .withPartitionKey("race_year", DataTypes.INT)
    .withPartitionKey("race_name", DataTypes.TEXT)
    .withClusteringColumn("rank", DataTypes.INT)
    .withColumn("cyclist_name", DataTypes.TEXT)
    .withCompaction(leveledCompactionStrategy())
    .withSnappyCompression()
    .withClusteringOrder("rank", ClusteringOrder.DESC);
// CREATE TABLE cycling.cyclist_by_year_and_name (race_year int,race_name text,rank int,cyclist_name text,PRIMARY KEY((race_year,race_name),rank))
// WITH CLUSTERING ORDER BY (rank DESC)
// AND compaction={'class':'LeveledCompactionStrategy'}
// AND compression={'class':'SnappyCompressor'}

Altering a Table (ALTER TABLE)¶

To start an ALTER TABLE query, use alterTable:

alterTable("cycling", "cyclist_name");

From here, you can modify the table in the following ways:

  • dropCompactStorage(): Drops COMPACT STORAGE from a table, removing thrift compatibility mode and migrates to a CQL-compatible format.

  • addColumn(columnName, dataType): Adds a new column to the table.

  • alterColumn(columnName, dataType): Changes the type of an existing column. This is not recommended.

  • dropColumn(columnName): Removes an existing column from the table.

  • renameColumn(from, to): Renames a column.

  • API methods from AlterTableWithOptions

Invoking any of these methods returns a complete query and you may make successive calls to the same API methods, with exception to alter column, which may only be invoked once.

Dropping a Table (DROP TABLE)¶

To create a DROP TABLE query, use dropTable:

dropTable("cycling", "cyclist_name");
// DROP TABLE cycling.cyclist_name

You may also specify ifExists:

dropTable("cyclist_name").ifExists();
// DROP TABLE IF EXISTS cyclist_name
PREVIOUS
Materialized View
NEXT
Type
  • 4.11.1.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • API conventions
    • Case sensitivity
    • Core driver
      • Address resolution
      • Asynchronous programming
      • Authentication
      • Bill of Materials (BOM)
      • Compression
      • Configuration
        • Reference configuration
      • Control connection
      • Custom codecs
      • Detachable types
      • Query idempotence
      • Integration
      • Load balancing
      • Logging
      • Metadata
        • Node metadata
        • Schema metadata
        • Token metadata
      • Metrics
      • Native protocol
      • Non-blocking programming
      • Paging
      • Performance
      • Connection pooling
      • Query timestamps
      • Reactive Style Programming
      • Reconnection
      • Request tracker
      • Retries
      • Using the shaded JAR
      • Speculative query execution
      • SSL
      • Statements
        • Batch statements
        • Per-query keyspace
        • Prepared statements
        • Simple statements
      • Temporal types
      • Request throttling
      • Query tracing
      • Tuples
      • User-defined types
    • Developer docs
      • Administrative tasks
      • Common infrastructure
        • Concurrency
        • Driver context
        • Event bus
      • Native protocol layer
      • Netty pipeline
      • Request execution
    • Mapper
      • Integration
        • Kotlin
        • Lombok
        • Java 14 Records
        • Scala
      • DAOs
        • Custom result types
        • Delete methods
        • GetEntity methods
        • Increment methods
        • Insert methods
        • Null saving strategy
        • Query methods
        • Query provider methods
        • Select methods
        • SetEntity methods
        • Statement attributes
        • Update methods
      • Entities
      • Mapper interface
    • OSGi
    • Query builder
      • Conditions
      • DELETE
      • Idempotence in the query builder
      • INSERT
      • Relations
      • Schema builder
        • Aggregate
        • Function
        • Index
        • Keyspace
        • Materialized View
        • Table
        • Type
      • SELECT
      • Terms
      • TRUNCATE
      • UPDATE
  • Upgrade guide
  • Frequently asked questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • Table
    • Creating a Table (CREATE TABLE)
    • Altering a Table (ALTER TABLE)
    • Dropping a Table (DROP TABLE)
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2