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.

Similarly, you cannot rename a table. Instead, you must drop and re-create the 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).

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

ctx

context.Context

The context for the operation.

op

table.AlterOperation

The alter operation to perform.

Can be one of the following:

opts

…​options.AlterTableOption

Optional. A builder to generate options for this operation. See Methods of the AlterTableOption builder for more details.

Methods of the AlterTableOption builder
Method Summary

UpdateAPIOptions(v …​APIOption)

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.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM