Modifying Tables

Learn how to modify tables.

You modify a table to:

  • Add new fields to an existing table

  • Delete currently existing fields in a table

  • To change the default TTL value

Examples of DDL statements are:
/* Add a new field to the table */
ALTER TABLE users (ADD age INTEGER)

/* Drop an existing field from the table */
ALTER TABLE users (DROP age)

/* Modify the default TTL value*/
ALTER TABLE users USING TTL 4 days
You can change the definition of the table. The TTL value is changed below.
/* Alter the users table to modify the TTL value to 4 days. 
* When modifying the table schema or other table state you cannot also 
* modify the table limits. These must be independent operations. 
*/
String alterTableDDL = "ALTER TABLE users " + "USING TTL 4 days";
TableRequest treq = new TableRequest().setStatement(alterTableDDL); 
/* start the operation, it is asynchronous */
TableResult tres = handle.tableRequest(treq);
/* wait for completion of the operation */ 
tres.waitForCompletion(handle, 60000, 1000);
/* wait for 60 sec */
/* delay in ms for poll */
You can change the TTL value of a table as shown below.
/* Alter the users table to modify the TTL value to 4 days. 
* When modifying the table schema or other table state you cannot also 
* modify the table limits. These must be independent operations. 
*/
TableRequest statement = "ALTER TABLE users " + "USING TTL 4 days";
request = TableRequest().set_statement(statement)
# assume that a handle has been created, as handle, make the request 
#wait for 60 seconds, polling every 1 seconds
result = handle.do_table_request(request, 60000, 1000) 
result.wait_for_completion(handle, 60000, 1000)
Specify the DDL statement and other information in a TableRequest, and execute the request using the nosqldb.DoTableRequest() or nosqldb.DoTableRequestAndWait() function.
req:=&nosqldb.TableRequest{
    Statement: "ALTER TABLE users (ADD age INTEGER)",
}
res, err:=client.DoTableRequestAndWait(req, 5*time.Second, time.Second)
You can change the TTL value of a table as shown below.
/* Alter the users table to modify the TTL value to 4 days. 
*/
const statement = 'ALTER TABLE users ' + 'USING TTL 4 days';
let result = await client.tableDDL(statement, complete: true });
console.log('Table users altered');

Use ExecuteTableDDLAsync or ExecuteTableDDLWithCompletionAsync to modify a table by issuing a DDL statement against this table.

You can change the TTL value of a table as shown below.
/* Alter the users table to modify the TTL value to 4 days. 
*/
var statement = "ALTER TABLE users " + "USING TTL 4 days";
var result = await client.ExecuteTableDDLAsync(statement);
await result.WaitForCompletionAsync(); 
Console.WriteLine("Table users altered."); 
To modify a table, you can use the NosqlTemplate.runTableRequest() method. For details, see SDK for Spring Data API Reference.

Note:

While the Spring Data SDK provides an option to modify the tables, it is not recommended to alter the schemas as the Spring Data SDK expects tables to comply with the default schema (two columns - the primary key column of types String, int, long, or timestamp and a JSON column called kv_json_).