Add columns to table schema
If you need to add an attribute to something you are storing in a table, you
can add a column by sending a POST
request to add the column.
curl -s -L -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/tables/users/columns \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
"name": "email",
"typeDefinition": "text"
}'
{ "name": "email" }
To change the name or data type of an existing column, use a similar command, but
sent a PUT
request instead:
curl -s -L \
-X PUT https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/tables/users/columns/firstname \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{ "name": "first", "typeDefinition": "varchar"}'
{
"name": "first"
}
To add a set to a table, specify the data type set
along with the data type of the
set contents, such as text
:
curl -s -L -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/tables/users/columns \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
"name": "favorite_books",
"typeDefinition": "set<text>"
}'
{"name":"favorite_books"}
To add a set
as a frozen set, specify "typeDefinition": "frozen<set<text>>"
with the keyword frozen
. In the following examples, both lists and maps can
also be specified as frozen.
To add a list to a table, specify the data type list
along with the data type of the
set contents, such as text
.
The difference between a set and a list is that a set is unordered, whereas a list
is ordered:
curl -s -L -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/tables/users/columns \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
"name": "top_three_tv_shows",
"typeDefinition": "list<text>"
}'
{"name":"top_three_tv_shows"}
To add a map to a table, specify the data type map
along with the data type of the
map key and the may value:
curl -s -L -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/tables/users/columns \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
"name": "evaluations",
"typeDefinition": "map<int,text>"
}'
{"name":"evaluations"}
To add a tuple to a table, specify the data type tuple
along with the data type of the
each item in the tuple.
A tuple can consist of two or more values that can be retrieved together.
curl -s -L -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/tables/users/columns \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
"name": "current_country",
"typeDefinition": "tuple<text, date, date>"
}'
{"name":"current_country"}
To add a user-defined type (UDT) to a table, specify the data type udt
along with the data type of the
each item in the UDT.
A UDT can consist of two or more values that can be retrieved together.
UDTs must currently be created in CQL before specifying for a column in the REST API. For instance, for a UDT created with the following CQL statement:
CREATE TYPE IF NOT EXISTS users_keyspace.address (
street text,
zip int
);
we can add a UDT column to our table:
curl -s -L -X POST https://$ASTRA_CLUSTER_ID-$ASTRA_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/users_keyspace/tables/users/columns \
-H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-d '{
"name": "address",
"typeDefinition": "address_type"
}'
{ "name": "address" }