CREATE TABLE
Creates a new table in the specified (or current) keyspace.
If the table already exists, no table is created and the statement returns an error.
Use IF NOT EXISTS to suppress the error message.
Syntax
CREATE TABLE [ IF NOT EXISTS ] [<keyspace_name>.]<table_name>
( <column_definition> [ , ... ]
[ , PRIMARY KEY ( ( <partition_key_column_name> [ , ... ] )
[ , ( <clustering_column_name> [ ... ] ) ] ) ] )
[ [ WITH | AND ] <table_options> [ AND ...] ]
[ [ WITH | AND ] CLUSTERING ORDER BY ( <clustering_column_name> [ ASC | DESC ] [ , ... ] ) ]
[ [ WITH | AND ] ID = '<table_hash_tag>' ] ;
| Syntax conventions | Description |
|---|---|
UPPERCASE |
Literal keyword. |
Lowercase |
Not literal. |
|
Variable value. Replace with a user-defined value. |
|
Optional.
Square brackets ( |
|
Group.
Parentheses ( |
|
Or.
A vertical bar ( |
|
Repeatable.
An ellipsis ( |
|
Single quotation ( |
|
Map collection.
Braces ( |
|
Set, list, map, or tuple.
Angle bracke
ts ( |
|
End CQL statement.
A semicolon ( |
|
Separate the command line options from the command arguments with two hyphens ( |
|
Search CQL only: Single quotation marks ( |
|
Search CQL only: Identify the entity and literal value to overwrite the XML element in the schema and solrConfig files. |
Parameters
| Parameter | Description | Default | ||
|---|---|---|---|---|
|
Optional. Name of the keyspace that contains the table. .Keyspace and table name conventions
|
If no name is specified, the current keyspace is used. |
||
|
Name of the table. |
|||
|
List of |
|||
|
Name of the column. |
|||
|
||||
|
List of columns that make up the primary key. The first column is the partition key. If there is more than one column in the partition key, enclose the columns in parentheses. Additional columns are clustering columns. |
|||
|
Name of the partition key column. One or more partition key columns are required. |
|||
|
Name of the clustering column.
Optional. One or more clustering columns can be defined.
Also define |
|
Options
Specify table options using the WITH keyword.
Each option can have a single value.
Some options have sub-properties that are specified using a simple JSON format, with key-value pairs in a comma-separated list enclosed by curly braces.
| Option | Description | Default | ||
|---|---|---|---|---|
|
Same as |
|
||
|
False-positive probability for SSTable bloom filter. When a client requests data, the bloom filter checks if the row exists before executing disk I/O.
Values range from 0 to 1.0, where: The recommended and default setting is |
|
||
|
Optimizes the use of cache memory without manual tuning.
Weighs the cached data by size and access frequency.
Coordinate this setting with the global caching properties in the
|
|
||
|
Creates a Change Data Capture (CDC) log on the table. Valid values:
|
|
||
|
Specify comments for the table. |
|
||
|
Configures the compaction strategy. See Compaction sub-properties for more information. |
|||
|
Configures the compression algorithm. See Compression sub-properties for more information. |
|||
|
Probability that a read request will check the CRC of a row. |
|
||
|
TTL (Time To Live) in seconds, where zero is disabled.
The maximum configurable value is
If the value is greater than zero, TTL is enabled for the entire table and an expiration timestamp is added to each column. A new TTL timestamp is calculated each time the data is updated and the row is removed after all the data expires. |
|
||
|
Specifies the table extensions. |
|
||
|
The number of seconds after data is marked with a tombstone (deletion marker) until it is eligible for garbage collection. In a single-node cluster, this property can safely be set to zero.
You can also reduce this value for tables whose data is not explicitly deleted; for example, tables containing only data with TTL set, or tables with |
|
||
|
If the total memory usage of all index summaries reaches this value, CQL decreases the index summaries for the coldest SSTables to the maximum set by |
|
||
|
A millisecond interval at which the table’s memtables are flushed. The default is Regardless of
You can also tune memtable flush behavior with the |
|
||
|
Minimum gap between index entries in the index summary.
A lower |
|
||
|
Sets read repair behavior.
Choices are |
|
||
|
Configures rapid read protection at the table level.
The database sends read requests to the minimum number of replica nodes to satisfy the consistency level.
For example, if
In most cases, the default value is acceptable. Avoid editing this property until you understand your read latencies and how speculative retries could impact them. This property is irrelevant when the consistency level is |
|
Compression sub-properties
Configure by specifying the compression algorithm class followed by the sub-properties in simple JSON format.
Custom compression classes can be implemented using the org.apache.cassandra.io.compress.ICompressor interface.
Most applications should use the compression algorithms bundled with the database.
compression = {
['class' : '<compression_algorithm_name>',
'chunk_length_in_kb' : '<value>',
'crc_check_chance' : '<value>',]
| 'sstable_compression' : '']
}
Property |
Description |
Default |
|
Sets the compressor class name. The database provides the following built-in classes:
Choosing the right compressor depends on your requirements for space savings over read performance.
LZ4 is fastest to decompress, followed by Snappy, then by Deflate.
Compression effectiveness is inversely correlated with decompression speed.
The extra compression from |
|
|
Size (in KB) of the block. On disk, SSTables are compressed by block to allow random reads. Values larger than the default value might improve the compression rate, but increases the minimum size of data to be read from disk when a read occurs. The default value is a good middle ground for compressing tables. Adjust compression size to account for read/write access patterns (how much data is typically requested at once) and the average size of rows in the table. |
|
|
When compression is enabled, each compressed block includes a checksum of that block for the purpose of detecting disk bit rot and avoiding the propagation of corruption to other replica. This option defines the probability with which those checksums are checked during read. By default they are always checked. Set to 0 to disable checksum checking and to 0.5, for instance, to check them on every other read. |
|
|
Disables compression. |
Specify a null value. |
Compaction sub-properties
Configure by specifying the compaction algorithm class followed by the sub-properties in simple JSON format.
See Data maintenance through compaction for more details.
compaction = {
'class' : '<compaction_strategy_name>',
'<property_name>' : <value> [, ...] }
where the <compaction_strategy_name> is UnifiedCompactionStrategy, SizeTieredCompactionStrategy, TimeWindowCompactionStrategy, or LeveledCompactionStrategy.
Common properties
The following properties apply to all compaction strategies.
compaction = {
'class' : 'compaction_strategy_name',
'enabled' : (true | false),
'log_all' : (true | false),
'only_purge_repaired_tombstone' : (true | false),
'tombstone_threshold' : <ratio>,
'tombstone_compaction_interval' : <sec>,
'unchecked_tombstone_compaction' : (true | false) }
| Property | Description | Default | ||
|---|---|---|---|---|
|
Sets the compaction strategy class name. The database provides the following built-in classes:
See Data maintenance through compaction to help choose the appropriate compaction strategy for your workload. |
|
||
|
Enable or disable background compaction.
Use
|
|
||
|
Activates advanced logging for the entire cluster. |
|
||
|
Enabling this property prevents data from resurrecting when repair is not run within the
|
|
||
|
The ratio of garbage-collectable tombstones to all contained columns. If the ratio exceeds this limit, compactions starts only on that table to purge the tombstones. |
|
||
|
Number of seconds before compaction can run on an SSTable after it is created.
An SSTable is eligible for compaction when it exceeds the |
|
||
|
Setting to |
|
UnifiedCompactionStrategy
The compaction class UnifiedCompactionStrategy (UCS) is a hybrid compaction strategy that combines the benefits of STCS and LCS.
UCS uses a tiered compaction strategy for small SSTables and a leveled compaction strategy for large SSTables.
See UnifiedCompactionStrategy (UCS).
The following additional properties apply to UnifiedCompactionStrategy:
compaction = {
'class' : 'UnifiedCompactionStrategy',
'expired_sstable_check_frequency_seconds' : <sec>,
'max_sstables_to_compact' : <max>,
'unsafe_aggressive_sstable_expiration' : (true | false),
'scaling_parameters' : <foo>,
'overlap_inclusion_method': <foo>,
'min_sstable_size' : <int>,
'base_shard_count' : <foo>,
'sstable_growth' : <foo>,
'target_sstable_size' : <foo>,
'num_shards' : <int> }
| Property | Description | Default | ||
|---|---|---|---|---|
|
The frequency in seconds at which SSTables are checked for expiration. |
|
||
|
The maximum number of SSTables to include in a single compaction. |
no maximum |
||
|
Expired SSTables are dropped without checking if their data is shadowing other SSTables.
|
|
||
|
A list of one or more per-level scaling parameters, like Lf, Tf or N. Alternatively, a single integer value (ω). Examples:
See Configure compaction for more details. |
|
||
|
An integer scaling factor (ω). See Configure compaction for more details.
|
|
||
|
Specifies how to handle overlapping SSTables during compaction. Valid values are:
|
|
||
|
Minimum size of an SSTable in bytes.
Applies when the |
|
||
|
The minimum number of shards used for levels with the smallest density. |
|
||
|
Value between |
|
||
|
The target SSTable size in bytes. |
|
||
|
Setting this property enables legacy UCS V1, which is for backwards compatibility when migrating from DSE 6.8 or 6.9 to HCD.
|
Undefined, no default value. |
SizeTieredCompactionStrategy
The compaction class SizeTieredCompactionStrategy (STCS) is the default compaction strategy.
The compaction class SizeTieredCompactionStrategy (STCS) triggers a minor compaction when table meets the min_threshold.
Minor compactions do not involve all the tables in a keyspace.
See SizeTieredCompactionStrategy (STCS).
The following additional properties apply to SizeTieredCompactionStrategy:
compaction = {
'class' : 'SizeTieredCompactionStrategy',
'bucket_high' : <factor>,
'bucket_low' : <factor>,
'min_sstable_size' : <int> }
| Property | Description | Default |
|---|---|---|
|
Size-tiered compaction merges sets of SSTables that are approximately the same size. The database compares each SSTable size to the average of all SSTable sizes for this table on the node. It merges SSTables whose size in KB are within [average-size * bucket_low] and [average-size * bucket_high]. |
|
|
Size-tiered compaction merges sets of SSTables that are approximately the same size. The database compares each SSTable size to the average of all SSTable sizes for this table on the node. It merges SSTables whose size in KB are within [average-size * bucket_low] and [average-size * bucket_high]. |
|
|
STCS groups SSTables into buckets. The bucketing process groups SSTables that differ in size by less than 50%. This bucketing process is too fine-grained for small SSTables. If your SSTables are small, use this option to define a size threshold in MB below which all SSTables belong to one unique bucket. |
|
|
The minimum number of SSTables to trigger a minor compaction. |
|
|
The maximum number of SSTables before a minor compaction is triggered. |
|
|
The |
TimeWindowCompactionStrategy
The compaction class TimeWindowCompactionStrategy (TWCS) compacts SSTables using a series of time windows or buckets.
TWCS creates a new time window within each successive time period.
During the active time window, TWCS compacts all SSTables flushed from memory into larger SSTables using STCS.
At the end of the time period, all of these SSTables are compacted into a single SSTable.
Then the next time window starts and the process repeats.
See TimeWindowCompactionStrategy (TWCS).
|
All of the properties for STCS are also valid for TWCS. |
The following additional properties apply to TimeWindowCompactionStrategy:
compaction = {
'class' : 'TimeWindowCompactionStrategy,
'compaction_window_unit' : <days>,
'compaction_window_size' : <int>,
'split_during_flush' : (true | false),
'min_threshold' : <int>,
'max_threshold' : <int> }
| Property | Description | Default |
|---|---|---|
|
Time unit used to define the bucket size.
For accepted values, see Java |
|
|
Units per bucket. |
|
|
Prevents mixing older data from repairs and hints with newer data from the current time window. During a flush operation, determines whether data partitions are split based on the configured time window.
During the flush operation, the data is split into a maximum of 12 windows. Each window holds the data in a separate SSTable. If the current time is <t0> and each window has a time duration of <w>, the data is split in the SSTables as follows:
|
|
|
The minimum number of SSTables to trigger a minor compaction. |
|
|
The maximum number of SSTables before a minor compaction is triggered. |
|
LeveledCompactionStrategy
The compaction class LeveledCompactionStrategy (LCS) creates SSTables of a fixed, relatively small size (160 MB by default) that are grouped into levels.
Within each level, SSTables are guaranteed to be non-overlapping.
Each level (L0, L1, L2 and so on) is 10 times as large as the previous.
Disk I/O is more uniform and predictable on higher than on lower levels as SSTables are continuously being compacted into progressively larger levels.
At each level, row keys are merged into non-overlapping SSTables in the next level.
See LeveledCompactionStrategy (LCS).
The following additional properties apply to LeveledCompactionStrategy:
compaction = {
'class' : 'LeveledCompactionStrategy',
'sstable_size_in_mb' : <int>,
'min_threshold' : <int>,
'max_threshold' : <int> }
| Property | Description | Default | ||
|---|---|---|---|---|
|
The target size for SSTables that use the LeveledCompactionStrategy. Although SSTable sizes should be less or equal to sstable_size_in_mb, it is possible that compaction could produce a larger SSTable during compaction. This occurs when data for a given partition key is exceptionally large. The DSE database does not split the data into two SSTables.
|
|
||
|
The minimum number of SSTables to trigger a minor compaction. |
|
||
|
The maximum number of SSTables before a minor compaction is triggered. |
|
DateTieredCompactionStrategy (deprecated)
Use TimeWindowCompactionStrategy instead.
Stores data written within a certain period of time in the same SSTable.
| Property | Description | Default |
|---|---|---|
|
The size of the first time window. |
|
|
DSE does not compact SSTables if its most recent data is older than this property. Fractional days can be set. |
|
|
The maximum window size in seconds. |
|
|
Units, <MICROSECONDS> or <MILLISECONDS>, to match the timestamp of inserted data. |
|
Usage Notes
All tables must have a primary key, made up of a partition key and zero or more clustering columns. A partition key can be a single column or a composite partition key composed of multiple columns. A static column is shared by all rows in a partition and is stored only once per partition. A static column cannot be part of the primary key. Primary keys can include frozen collections and UDTs.
GC Grace Period Considerations
If you lower the value, consider its interaction with these operations:
-
hint replays: When a node goes down and then comes back up, other nodes replay the write operations (called hints) that are queued for that node while it was unresponsive. The database does not replay hints older than gc_grace_seconds after creation. The max_hint_window_in_ms setting in the cassandra.yaml file sets the time limit (3 hours by default) for collecting hints for the unresponsive node.
-
batch replays: Like hint queues, batch operations store database mutations that are replayed in sequence. As with hints, the database does not replay a batched mutation older than gc_grace_seconds after creation. If your application uses batch operations, consider the possibility that decreasing gc_grace_seconds increases the chance that a batched write operation may restore deleted data. The batchlog_replay_throttle_in_kb property in the cassandra.yaml file give some control of the batch replay process. The most important factors, however, are the size and scope of the batches you use.