All Products
Search
Document Center

Lindorm:DDL

Last Updated:Apr 19, 2024

This topic describes the DDL syntaxes that are supported by Lindorm Cassandra Query Language (CQL). This topic also provides examples to show how to use the DDL syntaxes.

Overview

Lindorm CQL can be used to store data in tables. The schema of a table defines the layout of data that is stored in the table. Tables are grouped into keyspaces. Multiple options are available for keyspaces. If you specify an option for a keyspace, the option is applied to all tables in the keyspace. The replication strategy is the most frequently used option. Lindorm CQL uses the default settings to determine the replication strategy and the number of replicas. These settings in Lindorm CQL cannot be modified. Lindorm CQL allows you to use CQL to manage high-performance secondary indexes and full-text indexes. This allows you to use indexes based on your business requirements.

Syntaxes for defining keyspaces and tables

You can use the following syntax to define a keyspace:

keyspace_name ::=  name
name          ::=  unquoted_name | quoted_name
unquoted_name ::=  re('[a-zA-Z_0-9]{1, 48}')
quoted_name   ::=  '"' unquoted_name '"'

You can use the following syntax to define a table:

table_name    ::=  [ keyspace_name '.' ] name
name          ::=  unquoted_name | quoted_name
unquoted_name ::=  re('[a-zA-Z_0-9]{1, 48}')
quoted_name   ::=  '"' unquoted_name '"'
Note
  • The name of a keyspace or table can contain only letters, digits, and special characters. The name cannot be an empty string.

  • The name of a keyspace or table can be up to 48 characters in length. This limit helps ensure that the length of the name of each keyspace or table matches the naming rules of file systems.

  • By default, the names of keyspaces and tables are not case-sensitive. For example, myTable and mytable specify the same table. However, if a name is specified in double quotation marks ("), the name is case-sensitive. For example, "myTable" and "mytable" specify different tables.

  • Tables are included in keyspaces. You can specify the name of a table based on the name of the keyspace to which the table belongs. For example, you want to create a table named table in the ks keyspace and gc keyspace. You can specify ks.table as the name of table in the ks keyspace and gc.table as the name of table in the gc keyspace.

Indexes

Lindorm CQL supports secondary indexes and full-text indexes.

  • Secondary indexes

    You can use Lindorm CQL to create secondary indexes on a table. Then, you can use the secondary indexes to query data from the table. You can execute the following statement to specify the name of a secondary index.

    index_name ::=  re('[a-zA-Z_0-9]+')
  • Search indexes

    You can use search indexes in the following scenarios:

    • Queries that are performed based on multiple dimensions. You can specify conditions based on multiple columns to query data in an efficient manner.

    • Sorting results. Lindorm supports the ORDER BY clause. You can use the ORDER BY clause to sort results based on values in specified columns.

    • Fuzzy match.

    Before you use search indexes in Lindorm CQL, you must enable the search index feature and Lindorm Tunnel Service (LTS). For more information, see Overview.

CREATE KEYSPACE

Creates a keyspace.

Syntax

CREATE KEYSPACE [ IF NOT EXISTS ] keyspace_name WITH options
Note

Keyspaces in Lindorm are similar to namespaces. Lindorm CQL does not allow you to modify the replication strategy and the number of replicas. The default values of the related parameters are used.

Parameters

Parameter

Example

Description

keyspace_name

testks

The name of the keyspace.

options

replication

Valid values: replication and durable_writes.

  • replication: The value is of the MAP data type. This option specifies the number of data replicas. By default, Lindorm uses the 2-replica storage mode.

  • durable_writes: The value is of the BOOLEAN data type. This option specifies whether to enable durable writes. Default value: True.

Example

CREATE KEYSPACE testks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
Note

You cannot configure replication and durable_writes at the compatibility layer of Lindorm CQL keyspaces. The following default settings are used: the number of replicas is 2 and durable_writes is set to True.

ALTER KEYSPACE

Modifies the options of a keyspace.

Syntax

ALTER KEYSPACE keyspace_name WITH options
Note

In Lindorm CQL, the options parameter in the ALTER KEYSPACE statement works in the same manner as the options parameter in the CREATE KEYSPACE statement. You do not need to configure the options parameter in the ALTER KEYSPACE statement because the default settings are used.

Parameters

The parameters that are included in the values of the options parameter in the ALTER KEYSPACE statement are the same as the parameters that are included in the values of the options parameter in the CREATE KEYSPACE statements. For more information, see Parameters.

Example

ALTER KEYSPACE testks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};

USE

Uses a keyspace. In Lindorm CQL, objects such as tables, user-defined types (UDTs), and user-defined functions (UDFs) are bound to a keyspace.

Syntax

USE keyspace_name

Parameters

Parameter

Example

Description

keyspace_name

testks

The name of the keyspace.

Example

USE testks;

DROP KEYSPACE

Deletes a keyspace.

Syntax

DROP KEYSPACE [ IF EXISTS ] keyspace_name
Note
  • After the DROP KEYSPACE statement is executed to delete a keyspace, the keyspace is immediately deleted and cannot be restored. Data in the keyspace is also deleted, including the tables, UDTs, and UDFs in the keyspace.

  • If the keyspace that you specify does not exist, the system returns an error. You can use the IF EXISTS option to prevent the system from returning an error when the keyspace that you specify does not exist. This way, if the keyspace does not exist, the delete operation is not performed.

  • Only the root user can delete keyspaces.

Parameters

Parameter

Example

Description

keyspace_name

testks

The name of the keyspace.

Example

DROP KEYSPACE testks;

CREATE TABLE

Creates a table.

Syntax

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

Description

When you use Lindorm CQL to create a table, you need to specify a name for the table, the columns in the table, the columns of which the primary key consists, and the table options. You can use the IF NOT EXISTS keyword to prevent the system from returning an error when the table name that you specify already exists. This way, if the table already exists, the create operation is not performed.

Each row in a table consists of data in the columns of the table. When you create a table, you define columns in the value of the column_definition parameter. You can execute the ALTER statement to add columns after a table is created.

The column_definition parameter specifies the name and data type of a column. This specifies the data that the column stores. You can also specify a modifier for a column.

  • STATIC: specifies that the column is a static column. Lindorm CQL does not support the STATIC modifier.

  • PRIMARY KEY: specifies that the column is the sole component of the primary key of the table.

In a table, a data row is uniquely identified by a value of the primary key. You must specify a unique primary key for a table. Each table has only one primary key. A primary key consists of one or more columns in the table. In the CREATE TABLE statement, the primary key is specified by using the PRIMARY KEY keyword that is followed by a string of column names within parentheses. If the primary key contains only one column, you can add the PRIMARY KEY keyword to the column definition. The order of the columns in the primary key definition affects the distribution and storage order of data rows in the table.

A primary key consists of the following parts:

  • partition_key: the first part of the primary key. The partition key can consist of a single column or multiple columns that are enclosed in a pair of parentheses (). A table contains at least one partition key.

  • clustering_columns: the columns that are specified after the partition key in the primary key definition. The order of these columns determines the clustering order. You are not required to specify clustering columns.

The following list provides examples of how to define a primary key:

  • PRIMARY KEY(a): In this example, the a column is used as the partition key. No clustering columns are specified.

  • PRIMARY KEY(a, b, c): In this example, the a column is used as the partition key, and the b column and c column are used as clustering columns.

  • PRIMARY KEY((a, b), c): In this example, the a column and b column are used to specify a composite partition key, and the c column is used as a clustering column.

In Lindorm CQL, primary keys are important. A primary key consists of a partition key and clustering columns. Lindorm SQL cannot identify the node on which a row of data is stored based on only the partition key or clustering columns.

You also can use Lindorm CQL specify a partitioning policy for a table. Data rows in a partition have the same partition key value. If a partition key consists of multiple columns, the rows in the same partition contains the same values in the partition key columns. You can execute the following statements to create a table and query data from the table:

CREATE TABLE persioninfo (
    a int,
    b int,
    c int,
    d int,
    PRIMARY KEY ((a, b), c, d)
);

SELECT * FROM persioninfo;
   a | b | c | d
  ---+---+---+---
   0 | 0 | 0 | 0    // row 1
   0 | 0 | 1 | 1    // row 2
   0 | 1 | 2 | 2    // row 3
   0 | 1 | 3 | 3    // row 4
   1 | 1 | 4 | 4    // row 5

The partition key and clustering columns of the primary key of a table have the same priority. The partition key is used in combination with the clustering columns to identify the node on which a row of data is stored. Lindorm SQL cannot identify the node on which a row of data is stored based on only the partition key. In the preceding example, Row 1 and Row 2 reside on the same node. If you use a Lindorm CQL, Row 1 and Row 2 may reside on different nodes. This is the main difference between Lindorm CQL and traditional CQLs.

Examples

CREATE TABLE tb (name text PRIMARY KEY , age int);   // Sets the primary key column to the name column. 
CREATE TABLE ttltb (name text PARIMARY KEY, age int) WITH default_time_to_live = 1000;  // Sets the Time-to-Live (TTL) of data in the table to 1,000 seconds. 
CREATE TABLE cptb (name text PRIMARY KEY, age int) WITH compression = {'sstable_compression': 'LZ4Compressor'};  // Sets the compression algorithm to LZ4. The default compression algorithm is SNAPPY. 
CREATE TABLE hct ( name text PRIMARY KEY , age int  ) WITH extensions = {'COLD_BOUNDARY':'10'};  // Sets the hot data and cold data boundary to 10 seconds. 

Features that are unsupported by Lindorm CQL

Lindorm CQL cannot identify the physical node on which a row of data is stored based on only the partition key.

Supported table options

The table options supported by Lindorm CQL are different from the table options supported by Apache Cassandra. The following table describes the options that are supported by Lindorm CQL.

Option

Type

Description

default_time_to_live

int

The TTL of data in the table. Unit: seconds. Default value: 0.

compression

map

The compression algorithm for the sorted strings tables (SSTables). Valid values: LZ4, ZSTD, and SNAPPY. For more information, see the "Compression" section.

extensions

map

The extensions that are supported. Extensions such as the cold storage, cold and hot data separation, and consistency level of data in the table are supported. For more information, see the "Extensions" section.

Compression

Lindorm CQL supports the following compression algorithms. Lindorm CQL uses the default coefficient to configure each compression algorithm.

  • LZ4Compressor (LZ4). You can execute the following statement to create a table in which data is compressed by using the LZ4 algorithm:

    CREATE TABLE persioninfo (
       id int,
       name text,
       address text,
       PRIMARY KEY (id, name)
    ) with compression = {'class': 'LZ4Compressor'};
  • ZstdCompressor (Zstd). You can execute the following statement to create a table in which data is compressed by using the Zstd algorithm:

    CREATE TABLE persioninfo (
       id int,
       name text,
       address text,
       PRIMARY KEY (id, name)
    ) with compression = {'class': 'ZstdCompressor'};
  • SnappyCompressor (SNAPPY). You can execute the following statement to create a table in which data is compressed by using the SNAPPY algorithm:

    CREATE TABLE persioninfo (
       id int,
       name text,
       address text,
       PRIMARY KEY (id, name)
    ) with compression = {'class': 'SnappyCompressor'};
Note

You can execute the ALTER TABLE statement to modify the value of the compression property.

Extensions

You can use the extensions option to configure the following properties that are supported by Lindorm:

  • Cold storage. This property is specified by the STORAGE_TYPE keyword. The value COLD specifies cold storage. The value DEFAULT specifies hot storage.

    CREATE TABLE persioninfo (name text PRIMARY KEY, age int) WITH extensions = {'STORAGE_POLICY' : 'COLD'};  // Specifies cold storage. 
    ALTER TABLE persioninfo WITH extensions = {'STORAGE_POLICY' : 'DEFAULT'};  // Changes the storage policy from cold storage to hot storage. 
  • Hot data and cold data separation. This property is specified by the COLD_BOUNDARY keyword. If you want to enable the hot data and cold data separation feature for a table or a column family, you do not need to configure the STORAGE_POLICY parameter to COLD. If the value of the STORAGE_POLICY parameter is set to COLD, you must delete the STORAGE_POLICY property. For more information, see Overview.

    CREATE TABLE persioninfo (name text PRIMARY KEY, age int) with extensions = {'COLD_BOUNDARY':'86400'};  // Sets the hot data and cold data boundary to 86,400 seconds. The system archives data whose retention period exceeds the specified period of time in the cold storage. 
    ALTER TABLE persioninfo with extensions = {'COLD_BOUNDARY':''};   // Deletes the hot data and cold data boundary. 
    ALTER TABLE persioninfo with extensions = {'COLD_BOUNDARY':'1000'};   // Changes the value of the hot data and cold data boundary to 1,000 seconds. 
  • CONSISTENCY_TYPE: specifies the consistency level of data in a table that are deployed in the multiple zones. Valid values: eventual, timestamp, basic, and strong.

    CREATE TABLE persioninfo (name text PRIMARY KEY, age int) with extensions = {'CONSISTENCY_TYPE':'strong'};  // Sets the consistency level to strong. 
    ALTER TABLE persioninfo with extensions = {'CONSISTENCY_TYPE':'eventual'};  // Changes the consistency level to eventual. 
  • MUTABILITY: Before you use secondary indexes and full-text indexes, you must configure this parameter. Valid values: IMMUTABLE, IMMUTABLE_ROWS, MUTABLE_LATEST, and MUTABLE_ALL. The IMMUTABLE_ROWS, MUTABLE_LATEST, and MUTABLE_ALL values are valid only when the CONSISTENCY_TYPE parameter is set to strong. Default value: MUTABLE_ALL.

    CREATE TABLE persioninfo (name text PRIMARY KEY, age int) with extensions = {'MUTABILITY':'IMMUTABLE'};  // Sets the MUTABILITY parameter to IMMUTABLE. 
    ALTER TABLE persioninfo with extensions = {'MUTABILITY':'MUTABLE_LATEST'};   // Changes the value of the MUTABILITY parameter to MUTABLE_LATEST. 

Table options that you cannot configure

Option

Type

Description

comment

string

The description of the table. Default value: f.

speculative_retry

simple simple

Default value: 99PERCENTILE.

cdc

boolean

Specifies whether to generate change data capture (CDC) logs for the table. Default value: false.

gc_grace_seconds

int

The period of time to wait before data is marked with a tombstone. The tombstone is used as a delete marker. Default value: 86400.

bloom_filter_fp_chance

float

The probability of a false positive for each stable Bloom filter. The system determines the probability based on the size of each Bloom filer. If you reduce the value of this parameter, the size of each Bloom filer that resides in memory or on disks is affected. Default value: 0.00075.

compaction

map

Default value: STCS.

caching

map

N/A

memtable_flush_period_in_ms

map

Default value: 0.

read_repair

int

Default value: BLOCKING.

ALTER TABLE

Modifies a table.

Syntax

ALTER TABLE table_name alter_table_instruction
alter_table_instruction ::=  ADD column_name cql_type ( ',' column_name cql_type )*
                             | DROP column_name ( column_name )*
                             | WITH options

Description

The following list describes how to use the ALTER TABLE statement:

  • You can use the ADD instruction to add columns to a table. You cannot use the ALTER TABLE statement to modify the primary key of a table after the table is created. Therefore, you cannot add a new column to primary key columns. Take note of the limits when you add columns to compact tables.

  • Lindorm CQL does not support the DROP instruction that is used to delete columns from a table.

  • You can use the WITH instruction to modify the settings of a table. You can modify the options that are specified for a table, except the CLUSTERING ORDER option. The default_time_to_live, compression and extensions options can be modified.

Examples

CREATE TABLE persioninfo (name text PRIMARY KEY, age int);
ALTER TABLE persioninfo ADD address text;

DROP TABLE

Deletes a table.

Syntax

DROP TABLE [ IF EXISTS ] table_name
Note
  • After the DROP TABLE statement is executed to delete a table, the table and data in the table are immediately deleted and cannot be restored.

  • If you specify a table that does not exist, the system returns an error. You can use the IF EXISTS option to prevent the system from returning an error when the table that you specify does not exist. This way, if the table does not exist, the delete operation is not performed.

  • Only the root user can delete tables.

Parameters

Parameter

Example

Description

table_name

persioninfo

The name of the table.

Example

DROP TABLE persioninfo;

TRUNCATE

Clears the data of a table.

Syntax

TRUNCATE [ TABLE ] table_name
Note
  • You can execute the TRUNCATE statement to permanently delete all data from a table. The data of the schema and status of the table is not deleted.

  • Only the root user can execute the TRUNCATE TABLE statement to clear data from tables.

Parameters

Parameter

Example

Description

table_name

persioninfo

The name of the table.

Example

TRUNCATE TABLE persioninfo;

CREATE INDEX

Creates a secondary index.

Syntax

CREATE [ CUSTOM ] INDEX [ IF NOT EXISTS ] [ index_name ]
                                ON table_name '(' index_identifier ')'
                                [ USING string [ WITH OPTIONS = map_literal ] ]
index_identifier       ::=  column_name
                           | '(' column_name ')'

Description

You can execute the CREATE INDEX statement to create a secondary index based on a column in a table. You can specify the name of the index on the left side of the ON keyword. The data that was written to the column before you create the index is asynchronously indexed. After the index is created, the system automatically indexes new data that is written to the column.

If the index name that you specify already exists, the system returns an error. You can use the IF NOT EXISTS option to prevent the system from returning an error message when the specified index name already exists. This way, if the index name already exists, the create operation is not performed.

The CREATE INDEX statement can be used to create an index based on only a single column. If you want to create an indexes based on multiple columns in a table, specify USING 'com.alibaba.lindorm.cserver.schema.index.LindormSecondaryIndex' in the CREATE CUSTOM INDEX statement.

Examples

CREATE INDEX myindex ON persioninfo (c2);
CREATE INDEX ON persioninfo (c2);
CREATE CUSTOM INDEX myindex ON persioninfo (c1,c2) USING 'com.alibaba.lindorm.cserver.schema.index.LindormSecondaryIndex';

DROP INDEX

Deletes a secondary index.

Syntax

DROP INDEX [ IF EXISTS ] index_name
Note
  • You can execute the DROP INDEX statement to delete an existing secondary index. The index_name parameter specifies the index that you want to delete. You can also specify the keyspace to which the index belongs.

  • If the index that you specify does not exist, the system returns an error. You can use the IF EXISTS option to prevent the system from returning an error when the index that you specify does not exist. This way, if the specified index does not exist, the delete operation is not performed.

Example

DROP INDEX myindex;

CREAT SEARCH INDEX

Creates a full-text index.

Syntax

CREATE SEARCH INDEX [ IF NOT EXISTS ] index_name ON [keyspace_name.]table_name
| [ WITH [ COLUMNS (column1,...,columnn) ]
| [ WITH [ COLUMNS (*) ]
Note
  • You can execute the CREATE SEARCH INDEX statement to create a full-text index based on one or multiple columns in the base table.

  • WITH COLUMNS (column): specifies one or more columns based on which a full-text index is created. Separate multiple columns with commas (,).

  • WITH COLUMNS (*): The * wildcard specifies all columns in the base table.

  • You can use the extensions keyword to specify more properties for the base table when you create a full-text index. If your Lindorm instance is deployed in multiple zones, you must specify the CONSISTENCY_TYPE and MUTABILITY properties. If your Lindorm instance is deployed in a single zone, you are not required to specify these properties.

Example

CREATE SEARCH INDEX schidx ON persioninfo WITH COLUMNS (c2, c3); 

DROP SEARCH INDEX

Deletes a full-text index.

Syntax

DROP SEARCH INDEX [IF EXISTS] ON [keyspace_name.]table_name;

Example

DROP SEARCH INDEX ON testks.persioninfo;

REBUILD SEARCH INDEX

Sets the status of a full-text index to active.

Syntax

REBUILD SEARCH INDEX [ASYNC] [IF EXISTS] ON [keyspace_name.]table_name;
Note

After you create a full-text index, the status of the index is INACTIVE. You must execute the REBUILD SEARCH INDEX statement to activate the index. The REBUILD SEARCH INDEX statement also creates the search index based on historical data. The system requires a long period of time to complete the operation. You can specify the ASYNC keyword to create the search index on historical data in an asynchronous manner.

Examples

REBUILD SEARCH INDEX ON persioninfo;
REBUILD SEARCH INDEX ASYNC ON persioninfo;

ALTER SEARCH INDEX

Modifies a full-text index. This statement can be used to change the status of a full-text index, add an index key column, or delete an index key column.

Syntax

ALTER SEARCH INDEX SCHEMA [IF EXISTS] ON [keyspace_name.]table_name
  ( ADD FIELD column_name
  | DROP FIELD column_name) ;
Note
  • After you execute the ALTER SEARCH INDEX statement to modify a full-text index, you must execute the REBUILD SEARCH INDEX statement to reactivate the index.

  • If you execute the ADD SEARCH INDEX statement to add an index key column or the DROP SEARCH INDEX statement to delete an index key column, make sure that the column that you specify exists in the table.

Examples

ALTER SEARCH INDEX SCHEMA ON persioninfo ADD  (c3);
ALTER SEARCH INDEX SCHEMA ON persioninfo DROP  (c2);