DDL Statements (Data Definition Language) in Amazon Keyspaces - Amazon Keyspaces (for Apache Cassandra)

DDL Statements (Data Definition Language) in Amazon Keyspaces

Data definition language (DDL) is the set of Cassandra Query Language (CQL) statements that you use to manage data structures in Amazon Keyspaces (for Apache Cassandra), such as keyspaces and tables. You use DDL to create these data structures, modify them after they are created, and remove them when they're no longer in use. Amazon Keyspaces performs DDL operations asynchronously. For more information about how to confirm an asynchronous operation has completed, see Asynchronous Creation and Deletion of Keyspaces and Tables.

The following DDL statements are supported:

Topics

Keyspaces

A keyspace groups related tables that are relevant for one or more applications. In terms of a relational database management system (RDBMS), keyspaces are roughly similar to databases, tablespaces, or similar constructs.

Note

In Apache Cassandra, keyspaces determine how data is replicated among multiple storage nodes. However, Amazon Keyspaces is a fully managed service: The details of its storage layer are managed on your behalf. For this reason, keyspaces in Amazon Keyspaces are logical constructs only, and aren't related to the underlying physical storage.

For information about quota limits and constraints for Amazon Keyspaces keyspaces, see Quotas for Amazon Keyspaces (for Apache Cassandra).

The following is the only available replication strategy for keyspaces:

SingleRegionStrategy – Replicates data across three Availability Zones in its Region.

CREATE KEYSPACE

Use the CREATE KEYSPACE statement to create a new keyspace.

Syntax

create_keyspace_statement ::= CREATE KEYSPACE [ IF NOT EXISTS ] keyspace_name WITH options

Where:

  • keyspace_name is the name of the keyspace to be created.

  • options are one or more of the following:

    • REPLICATION – A map indicating the replication strategy for the keyspace (SingleRegionStrategy), with additional values as necessary. (Required)

    • DURABLE_WRITES – Writes to Amazon Keyspaces are always durable, so this option isn't required. However, if specified, the value must be true.

    • TAGS – A list of key-value pair tags to be attached to the resource on creation.

Example

Create a keyspace as follows.

CREATE KEYSPACE "myGSGKeyspace" WITH REPLICATION = {'class': 'SingleRegionStrategy'} and TAGS ={'key1':'val1', 'key2':'val2'} ;

ALTER KEYSPACE

Use the ALTER KEYSPACE to add or remove tags from a keyspace.

Syntax

alter_keyspace_statement ::= ALTER KEYSPACE keyspace_name [[ADD | DROP] TAGS

Where:

  • keyspace_name is the name of the keyspace to be altered.

  • TAGS – A list of key-value pair tags to be added or removed from the keyspace.

Example

Alter a keyspace as follows.

ALTER KEYSPACE "myGSGKeyspace" ADD TAGS {'key1':'val1', 'key2':'val2'};

DROP KEYSPACE

Use the DROP KEYSPACE statement to remove a keyspace—including all of its contents, such as tables.

Syntax

drop_keyspace_statement ::= DROP KEYSPACE [ IF EXISTS ] keyspace_name

Where:

  • keyspace_name is the name of the keyspace to be dropped.

Example

DROP KEYSPACE "myGSGKeyspace";

Tables

Tables are the primary data structures in Amazon Keyspaces. Data in a table is organized into rows and columns. A subset of those columns is used to determine partitioning (and ultimately data placement) through the specification of a partition key.

Another set of columns can be defined into clustering columns, which means that they can participate as predicates in query execution.

By default, new tables are created with on-demand throughput capacity. You can change the capacity mode for new and existing tables. For more information about read/write capacity throughput modes, see Read/Write Capacity Modes in Amazon Keyspaces.

For information about quota limits and constraints for Amazon Keyspaces tables, see Quotas for Amazon Keyspaces (for Apache Cassandra).

CREATE TABLE

Use the CREATE TABLE statement to create a new table.

Syntax

create_table_statement ::= CREATE TABLE [ IF NOT EXISTS ] table_name '(' column_definition ( ',' column_definition )* [ ',' PRIMARY KEY '(' primary_key ')' ] ')' [ WITH table_options ] column_definition ::= column_name cql_type [ PRIMARY KEY] primary_key ::= partition_key [ ',' clustering_columns ] partition_key ::= column_name | '(' column_name ( ',' column_name )* ')' clustering_columns ::= column_name ( ',' column_name )* table_options ::= [table_options] | CLUSTERING ORDER BY '(' clustering_order ')' [ AND table_options ] | options | CUSTOM_PROPERTIES | WITH TAGS clustering_order ::= column_name (ASC | DESC) ( ',' column_name (ASC | DESC) )*

Where:

  • table_name is the name of the table to be created.

  • column_definition consists of the following:

    • column_name – A name of the column.

    • cql_type – An Amazon Keyspaces data type (see Data Types).

    • PRIMARY KEY – Designates this column as the table's primary key.

  • primary_key consists of the following:

    • partition_key

    • clustering_columns

  • partition_key:

    • The partition key can be a single column, or it can be a compound value composed of two or more columns. The partition key portion of the primary key is required and determines which partition of your cluster the data is stored in.

  • clustering_columns:

    • The optional clustering column portion of your primary key determines how the data is clustered and sorted within each partition.

  • table_options consists of the following:

    • CLUSTERING ORDER BY – The default CLUSTERING ORDER on a table is composed of your clustering keys in the “ASCending” sort direction. Specify it to override the default sort behavior.

    • CUSTOM_PROPERTIES – A map of settings specific to Amazon Keyspaces.

      • capacity_mode: Specifies the read/write throughput capacity mode for the table. The options are throughput_mode:PAY_PER_REQUEST and throughput_mode:PROVISIONED. The provisioned capacity mode requires read_capacity_units and write_capacity_units as inputs. The default is throughput_mode:PAY_PER_REQUEST.

      • point_in_time_recovery: Specifies if point-in-time restore is enabled or disabled for the table. The options are status:enabled and status:disabled. If not specified, the default is status:disabled.

    • TAGS: A list of key-value pair tags to be attached to the resource on creation.

  • clustering_order consists of the following:

    • column_name – A name of the column.

    • ASC | DESC – Sets the ascendant (ASC) or descendant (DESC) order modifier. If not specified, the default order is ASC.

Example

CREATE TABLE IF NOT EXISTS "my_keyspace".my_table ( id text, name text, region text, division text, project text, role text, pay_scale int, vacation_hrs float, manager_id text, PRIMARY KEY (id,division)) WITH CUSTOM_PROPERTIES={'capacity_mode':{'throughput_mode': 'PROVISIONED', 'read_capacity_units': 10, 'write_capacity_units': 20},'point_in_time_recovery': {'status': 'enabled'}} AND CLUSTERING ORDER BY (division ASC) AND TAGS={'key1':'val1', 'key2':'val2'};

ALTER TABLE

Use the ALTER TABLE statement to add new columns, tags, or change the table's custom properties.

Syntax

alter_table_statement ::= ALTER TABLE table_name [ ADD ( column_definition | column_definition_list) ] [[ADD | DROP] TAGS {'key1':'val1', 'key2':'val2'}] [ WITH table_options [ , ... ] ] ; column_definition ::= column_name cql_type

Where:

  • table_name is the name of the table to be altered.

  • column_defintion is the name of the column and data type to be added.

  • column_definition_list is a comma-separated list of columns placed inside parentheses.

  • TAGS is a list of key-value pair tags to be attached to the resource.

  • table_options consists of the following:

    • CUSTOM_PROPERTIES – A map of settings specific to Amazon Keyspaces.

      • capacity_mode: Specifies the read/write throughput capacity mode for the table. The options are throughput_mode:PAY_PER_REQUEST and throughput_mode:PROVISIONED. The provisioned capacity mode requires read_capacity_units and write_capacity_units as inputs. The default is throughput_mode:PAY_PER_REQUEST.

      • point_in_time_recovery: Specifies if point-in-time restore is enabled or disabled for the table. The options are status:enabled and status:disabled. The default is status:disabled.

Note

With ALTER TABLE, you can either add columns, add tags, or change a custom property. You cannot combine more than one ALTER TABLE command in the same statement.

Examples

ALTER TABLE "myGSGKeyspace".employees_tbl ADD (first_name text); or ALTER TABLE "myGSGKeyspace".employees_tbl WITH custom_properties={'capacity_mode':{'throughput_mode': 'PROVISIONED', 'read_capacity_units': 10, 'write_capacity_units': 20}} ; or ALTER TABLE mykeyspace.mytable WITH custom_properties={'point_in_time_recovery': {'status': 'enabled'}};

RESTORE TABLE

Use the RESTORE TABLE statement to restore a table to a point in time. This statement requires point-in-time recovery to be enabled on a table. For more information, see Point-in-Time Recovery for Amazon Keyspaces.

Syntax

restore_table_statement ::= RESTORE TABLE table_name FROM TABLE table_name [ WITH table_options [ , ... ] ] ;

Where:

  • restored_table_name is the name of the restored table.

  • source_table_name is the name of the source table.

  • table_options consists of the following:

    • restore_timestamp is the restore point time. If it's not specified, the current timestamp is used.

    • CUSTOM_PROPERTIES – A map of settings specific to Amazon Keyspaces.

      • capacity_mode: Specifies the read/write throughput capacity mode for the table. The options are throughput_mode:PAY_PER_REQUEST and throughput_mode:PROVISIONED. The provisioned capacity mode requires read_capacity_units and write_capacity_units as inputs. The default is the current setting from the source table.

      • point_in_time_recovery: Specifies if point-in-time restore is enabled or disabled for the table. The options are status:enabled and status:disabled. Unlike when you create new tables, the default status for restored tables is status:enabled because the setting is inherited from the source table. To disable PITR for restored tables, you must set status:disabled explicitly.

    • TAGS is a list of key-value pair tags to be attached to the resource.

Note

Deleted tables can only be restored to the time of deletion.

Example

RESTORE TABLE mykeyspace.my_tablerestored from table mykeyspace.my_table WITH restore_timestamp = '2020-06-30T04:05:00+0000' AND custom_properties = {'point_in_time_recovery':{'status':'disabled'}, 'capacity_mode':{'throughput_mode': 'PROVISIONED', 'read_capacity_units': 10, 'write_capacity_units': 20}} AND TAGS={'key1':'val1', 'key2':'val2'};

DROP TABLE

Use the DROP TABLE statement to remove a table from the keyspace.

Syntax

drop_table_statement ::= DROP TABLE [ IF EXISTS ] table_name

Where:

  • IF EXISTS prevents DROP TABLE from failing if the table doesn't exist. (Optional)

  • table_name is the name of the table to be dropped.

Example

DROP TABLE "myGSGKeyspace".employees_tbl;