INSERT

AttentionThis page documents an earlier version. Go to the latest (v2.1)version.

Synopsis

The INSERT statement adds a row to a specified table.

Syntax

Diagram

INSERT - 图1

using_expression

  1. using_expression = ttl_or_timestamp_expression { 'AND' ttl_or_timestamp_expression };

INSERT - 图2

ttl_or_timestamp_expression

  1. ttl_or_timestamp_expression = 'TTL' ttl_expression | 'TIMESTAMP' timestamp_expression;

INSERT - 图3

Grammar

  1. insert ::= INSERT INTO table_name '(' column [ ',' column ... ] ')'
  2. VALUES '(' value [ ',' value ... ] ')'
  3. [ IF { [ NOT ] EXISTS | if_expression } ]
  4. [ USING using_expression ];

Where

  • table_name and column are identifiers (table_name may be qualified with a keyspace name).
  • value can be any expression although Apache Cassandra requires that values must be literals.
  • Restrictions for if_expression and ttl_expression are covered in the Semantics section below.
  • See Expressions for more information on syntax rules.

Semantics

  • An error is raised if the specified table_name does not exist.
  • The columns list must include all primary key columns.
  • The USING TIMESTAMP clause indicates we would like to perform the INSERT as if it was done at thetimestamp provided by the user. The timestamp is the number of microseconds since epoch.
  • NOTE: You should either use the USING TIMESTAMP clause in all of your statements or none ofthem. Using a mix of statements where some have USING TIMESTAMP and others do not will lead tovery confusing results.

VALUES Clause

  • The values list must have the same length as the columns list.
  • Each value must be convertible to its corresponding (by position) column type.
  • Each value literal can be an expression that evaluates to a simple value.

IF Clause

  • The if_expression can only apply to non-key columns (regular columns).
  • The if_expression can contain any logical and boolean operators.

USING Clause

  • ttl_expression must be an integer value (or a bind variable marker for prepared statements).
  • timestamp_expression must be an integer value (or a bind variable marker for prepared statements).

Examples

Insert a row into a table

You can do this as shown below.

  1. cqlsh:example> CREATE TABLE employees(department_id INT,
  2. employee_id INT,
  3. name TEXT,
  4. PRIMARY KEY(department_id, employee_id));
  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 1, 'John');
  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 2, 'Jane');
  1. cqlsh:example> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 1 | 1 | John
  4. 1 | 2 | Jane

Conditional insert using the IF clause

You can do this as shown below.

  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (2, 1, 'Joe') IF name = null;

  1. [applied]

  1. True

  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (2, 1, 'Jack') IF NOT EXISTS;

  1. [applied]

  1. False

cqlsh:example> SELECT * FROM employees;

department_id | employee_id | name———————-+——————-+——— 2 | 1 | Joe 1 | 1 | John 1 | 2 | Jane

Insert a row with expiration time using the USING TTL clause

You can do this as shown below.

  1. cqlsh:example> INSERT INTO employees(department_id, employee_id, name) VALUES (2, 2, 'Jack') USING TTL 10;
  1. cqlsh:example> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 2 | 1 | Joe
  4. 2 | 2 | Jack
  5. 1 | 1 | John
  6. 1 | 2 | Jane

Approximately 11 seconds after the insert.

  1. cqlsh:example> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 2 | 1 | Joe
  4. 1 | 1 | John
  5. 1 | 2 | Jane

Insert a row with USING TIMESTAMP clause.

Insert a row with a low timestamp.

You can do this as shown below.

  1. cqlsh:foo> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 3, 'Jeff') USING TIMESTAMP 1000;
  2. cqlsh:foo> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+------
  3. 1 | 1 | John
  4. 1 | 2 | Jane
  5. 1 | 3 | Jeff
  6. 2 | 1 | Joe
  7. (4 rows)

Overwrite the row with a higher timestamp.

You can do this as shown below.

  1. cqlsh:foo> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 3, 'Jerry') USING TIMESTAMP 2000;
  2. cqlsh:foo> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+-------
  3. 1 | 1 | John
  4. 1 | 2 | Jane
  5. 1 | 3 | Jerry
  6. 2 | 1 | Joe
  7. (4 rows)

Try to overwrite the row with a lower timestamp.

You can do this as shown below.

  1. cqlsh:foo> INSERT INTO employees(department_id, employee_id, name) VALUES (1, 3, 'James') USING TIMESTAMP 1500;
  2. cqlsh:foo> SELECT * FROM employees;
  1. department_id | employee_id | name
  2. ---------------+-------------+-------
  3. 1 | 1 | John
  4. 1 | 2 | Jane
  5. 1 | 3 | Jerry
  6. 2 | 1 | Joe
  7. (4 rows)

See Also

CREATE TABLEDELETESELECTUPDATEExpressionOther CQL Statements