Alter a table (Go)
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.
Parameters
Use the Alter method, which belongs to the Table type.
Method signature
func (t *Table) Alter(
ctx context.Context,
op table.AlterOperation,
opts ...options.AlterTableOption
) error
| 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)
}
}
Client reference
For more information, see the client reference.