Alter a table (C#)
Alters a table by doing one of the following:
-
Adding one or more columns to a table
-
Dropping one or more columns from a table
You cannot change a column’s type. Instead, you must drop the column and add a new column.
After you add a column, you should index the column if you want to filter or sort on the column. For more information, see Create an index (C#) and Create a vector index (C#).
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Adds or drops columns.
Returns a Table instance that represents the table after the schema change.
Although the Table instance that you used to perform the alteration will still work, it will not reflect the updated typing.
To reflect the new typing, provide the new type of the table to the Alter or AlterAsync method.
Parameters
Use the AlterAsync method, which belongs to the Table class.
You can also use Alter, which is the synchronous version of the method.
Method signature
public Task<Table<T>> AlterAsync(
IAlterTableOperation operation,
AlterTableOptions options = null
);
public Task<Table<TRowAfterAlter>> AlterAsync<TRowAfterAlter>(
IAlterTableOperation operation,
AlterTableOptions options = null
) where TRowAfterAlter : class;
| Name | Type | Summary |
|---|---|---|
|
|
The alter operation to perform. Can be one of the following:
|
|
Optional. General API options for this operation, including the timeout. For more information and examples, see Customize API interaction. |
Examples
The following examples demonstrate how to alter a table.
Add columns to a table
When you add columns, the columns are defined in the same way as they are when you create a table.
After you add a column, you should index the column if you want to filter or sort on the column. For more information, see Create an index (C#) and Create a vector index (C#).
-
Typed
-
Untyped
You can manually define a client-side type for your table to help statically catch errors. For more information and examples, see Custom typing for tables.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class ExampleRowBeforeAlter
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
}
public class ExampleRowAfterAlter : ExampleRowBeforeAlter
{
[ColumnName("is_summer_reading")]
public bool? IsSummerReading { get; set; }
[ColumnName("library_branch")]
public string? LibraryBranch { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable<ExampleRowBeforeAlter>(
"TABLE_NAME"
);
// Add columns
var newColumns = new Dictionary<string, AlterTableColumnDefinition>
{
["is_summer_reading"] = new AlterTableColumnDefinition
{
Type = "boolean",
},
["library_branch"] = new AlterTableColumnDefinition
{
Type = "text",
},
};
var alteredTable = await table.AlterAsync<ExampleRowAfterAlter>(
new AlterTableAddColumns(newColumns)
);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Add columns
var newColumns = new Dictionary<string, AlterTableColumnDefinition>
{
["is_summer_reading"] = new AlterTableColumnDefinition
{
Type = "boolean",
},
["library_branch"] = new AlterTableColumnDefinition
{
Type = "text",
},
};
await table.AlterAsync(new AlterTableAddColumns(newColumns));
}
}
Add vector columns to a table
After you add a vector column, you should index the column if you want run vector searches on the column. For more information, Create a vector index (C#).
-
Typed
-
Untyped
You can manually define a client-side type for your table to help statically catch errors. For more information and examples, see Custom typing for tables.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public abstract class ExampleRowBeforeAlter
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
}
public class ExampleRowAfterAlter : ExampleRowBeforeAlter
{
[ColumnName("example_vector")]
public float[]? ExampleVector { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable<ExampleRowBeforeAlter>(
"TABLE_NAME"
);
// Add a vector column
var newColumns = new Dictionary<
string,
AlterTableVectorColumnDefinition
>
{
["example_vector"] = new AlterTableVectorColumnDefinition
{
VectorDimension = 1024,
},
};
var alteredTable = await table.AlterAsync<ExampleRowAfterAlter>(
new AlterTableAddVectorColumns(newColumns)
);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Add a vector column
var newColumns = new Dictionary<
string,
AlterTableVectorColumnDefinition
>
{
["example_vector"] = new AlterTableVectorColumnDefinition
{
VectorDimension = 1024,
},
};
await table.AlterAsync(new AlterTableAddVectorColumns(newColumns));
}
}
Drop columns from a table
Dropping columns produces tombstones. Excessive tombstones can impact query performance.
-
Typed
-
Untyped
You can manually define a client-side type for your table to help statically catch errors. For more information and examples, see Custom typing for tables.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class ExampleRowAfterAlter
{
[ColumnPrimaryKey(1)]
[ColumnName("title")]
public string Title { get; set; } = null!;
[ColumnPrimaryKey(2)]
[ColumnName("author")]
public string Author { get; set; } = null!;
}
public class ExampleRowBeforeAlter : ExampleRowAfterAlter
{
[ColumnName("is_summer_reading")]
public bool? IsSummerReading { get; set; }
[ColumnName("library_branch")]
public string[]? LibraryBranch { get; set; }
}
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable<ExampleRowBeforeAlter>(
"TABLE_NAME"
);
// Drop columns
var alteredTable = await table.AlterAsync<ExampleRowAfterAlter>(
new AlterTableDropColumns(
new[] { "is_summer_reading", "library_branch" }
)
);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Drop columns
await table.AlterAsync(
new AlterTableDropColumns(
new[] { "is_summer_reading", "library_branch" }
)
);
}
}
Client reference
For more information, see the client reference.