A newer version of this documentation is available.

View Latest

INSERT statement

Inserts new records into a keyspace.

You can create a document by using the INSERT INTO. Performing an insert succeeds only if no document with that name exists. If the document exists, the INSERT fails.

insert:

INSERT INTO keyspace-ref [insert-values | insert-select] [returning-clause]

keyspace-ref:

[(namespace-name :)]  keyspace [ [AS] alias]

keyspace:

identifier

insert-values:

 [ (  [ PRIMARY ] KEY , VALUE ) ]  values-clause [, values-clause]*

values-clause:

VALUES ( expression , expression )

insert-select

( [PRIMARY] KEY expression [ , VALUE expression ] ) select

returning-clause:

RETURNING (result-expression [, result-expression]* | [RAW | ELEMENT ] expression)

result-expression:

[path . ]  * | expression [ [ AS ] alias ]

path:

identifier [ [ expression ] ] [ . path ]

keyspace-ref: Specifies the keyspace into which to insert the document.

You can add an optional namespace-name to the keyspace-name in this way:

namespace-name:keyspace-name.

For example, main:customer indicates the customer keyspace in the main namespace. If the namespace name is omitted, the default namespace in the current session is used.

insert-values: Specifies the values to be inserted.

insert-selectSpecifies the values to be inserted as a SELECT statement.

returning-clause:Returns the data you inserted as specified in the result_expression.

Example:

This statement inserts the value Odwalla for the key ID into the products document.

INSERT INTO product (KEY, VALUE) VALUES ("odwalla-juice1",
      { "productId": "odwalla-juice1", "unitPrice": 4.40, "type": "product", "color":"green"}) RETURNING *

"results": [
        {
            "color": "green",
            "productId": "odwalla-juice1",
            "type": "product",
            "unitPrice": 4.4
        }
    ]