Create a table in your keyspace
Create tables in your database using the DataStax Astra DB REST API. Use the application token you generated to create a table in your keyspace. A keyspace is like a bucket that holds your tables. You can create different keyspaces for groups of tables.
For example, we’ll use users_keyspace
as the keyspace.
-
Optional: A header line using a
{unique-UUID}
, a randomly-generated UUID that is unique for the authorization request, can be included in the previous command by adding the following line:
--header 'x-cassandra-request-id: {unique-UUID}'
Create a table
Send a POST
request to /api/rest/v2/schemas/keyspaces/{keyspace-name}/tables
to create a table.
Set the table name and column definitions in the JSON body in the --data
field.
curl -s --location \
--request POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/tables \
--header "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
--data '{
"name": "users",
"columnDefinitions":
[
{
"name": "firstname",
"typeDefinition": "text"
},
{
"name": "lastname",
"typeDefinition": "text"
},
{
"name": "favorite color",
"typeDefinition": "text"
}
],
"primaryKey":
{
"partitionKey": ["firstname"],
"clusteringKey": ["lastname"]
},
"tableOptions":
{
"defaultTimeToLive": 0,
"clusteringExpression":
[{ "column": "lastname", "order": "ASC" }]
}
}'
{"name":"users"}
The name of our example table is users
.
This table column definition includes:
- Partition key
-
One or more columns required.
firstname
is the partition key. - Clustering key
-
Optional, but zero, one or more columns can be defined.
lastname
is the clustering key. - Non-primary key
-
Optional, but zero, one or more columns that are not a partition key or clustering key can be defined.
favorite_color
is the non-primary key column.
Notice that each column must have a data type specified.
Information about primary keys (partition keys and clustering keys) are found in the Cassandra Query Language (CQL) reference.
Optional: Table options can be set. The two options are:
- defaultTimeToLive
-
Sets the default Time-To-Live (TTL).
- clusteringExpression
-
Defines the order, either ascending (
ASC
) or descending (DESC
) for columns that are clustering keys.ASC
is the default.
"tableOptions":
{
"defaultTimeToLive": 0,
"clusteringExpression":
[{ "column": "lastname", "order": "ASC" }]
}