Alter a table (Go)
|
Tables with the Data API are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
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 (Go) and Create a vector index (Go).
|
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 object that represents the table after the modification.
Signature
Use the Alter method, which belongs to the Table type.
func (t *Table) Alter(
ctx context.Context,
op table.AlterOperation,
opts ...options.AlterTableOption
) error
Parameters
| Name | Type | Summary |
|---|---|---|
|
|
The context for the operation. |
|
|
The alter operation to perform. Can be one of the following:
|
|
Optional.
A builder to generate options for this operation.
See Methods of the |
| Method | Summary |
|---|---|
|
Optional. General API options for this operation, including the timeout. |
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 (Go) and Create a vector index (Go).
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/table"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
tbl := database.Table("TABLE_NAME")
// Add columns
err := tbl.Alter(ctx, table.AddColumns{
Columns: table.Columns{
{Name: "is_summer_reading", Column: table.Boolean()},
{Name: "library_branch", Column: table.Text()},
},
})
if err != nil {
log.Fatal(err)
}
}
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 (Go).
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/table"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
tbl := database.Table("TABLE_NAME")
// Add columns
err := tbl.Alter(ctx, table.AddColumns{
Columns: table.Columns{
{Name: "example_vector", Column: table.Vector(1024)},
},
})
if err != nil {
log.Fatal(err)
}
}
Drop columns from a table
Dropping columns produces tombstones. Excessive tombstones can impact query performance.
package main
import (
"context"
"log"
"github.com/datastax/astra-db-go/v2/astra"
"github.com/datastax/astra-db-go/v2/astra/options"
"github.com/datastax/astra-db-go/v2/astra/table"
)
func main() {
ctx := context.Background()
// Get an existing table
client := astra.NewClient(
options.API().SetEnvironment(options.EnvironmentHCD),
)
database := client.Database(
"API_ENDPOINT",
options.API().
SetUsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD",
).
SetKeyspace("KEYSPACE_NAME"),
)
tbl := database.Table("TABLE_NAME")
// Add columns
err := tbl.Alter(ctx, table.DropColumns{
Columns: []string{"is_summer_reading", "library_branch"},
})
if err != nil {
log.Fatal(err)
}
}
Errors
Specific error codes for this command include CANNOT_ADD_EXISTING_COLUMNS, CANNOT_ADD_PREVIOUSLY_DROPPED_COLUMN, CANNOT_ADD_UNSUPPORTED_DATA_TYPE_COLUMNS, UNKNOWN_USER_DEFINED_TYPE, CANNOT_DROP_INDEXED_COLUMNS, CANNOT_DROP_PRIMARY_KEY_COLUMNS, CANNOT_DROP_UNKNOWN_COLUMNS, CANNOT_DROP_VECTORIZE_FROM_NON_VECTOR_COLUMNS, CANNOT_DROP_VECTORIZE_FROM_UNKNOWN_COLUMNS, CANNOT_VECTORIZE_NON_VECTOR_COLUMNS, CANNOT_VECTORIZE_UNKNOWN_COLUMNS, INVALID_VECTORIZE_FIELD_CONFIGURATION, INVALID_VECTORIZE_TABLE_CONFIGURATION, and MISSING_ALTER_TABLE_OPERATIONS.
For a full list of Data API error codes and their corresponding messages, see Data API errors and error codes.
For more information about how the client handles underlying Data API errors, see Go client error handling.
Client reference
For more information, see the client reference.