BigQuery · DDL statement
BigQuery ALTER TABLE
ALTER TABLE changes a BigQuery table without rewriting it: ADD COLUMN, DROP COLUMN, RENAME TO, RENAME COLUMN, SET OPTIONS (expiration, description, labels, partition filter) and ALTER COLUMN SET DATA TYPE for widening changes such as INT64 to NUMERIC. These are metadata changes and process no bytes.
ALTER TABLE covers the schema changes BigQuery can make in place. ADD COLUMN [IF NOT EXISTS] appends NULLABLE or REPEATED columns at the end of the schema; existing rows get NULL (or an empty array). You cannot add a REQUIRED column to an existing table, a column in the middle of the schema, a field inside an existing STRUCT, or a partitioning or clustering column. DROP COLUMN [IF EXISTS] removes a column from the schema, but not from storage right away: the bytes are freed the next time the data is rewritten, so to reclaim storage immediately rebuild the table with SELECT * EXCEPT. Partitioning and clustering columns, key columns and nested fields cannot be dropped.
RENAME TO new_name renames a table within its dataset (pass only the new table name). RENAME COLUMN a TO b renames top-level columns; several renames in one statement are applied in order, which lets you swap two names. Views that use the old names are not updated, and you cannot rename partitioning or clustering columns. SET OPTIONS (...) changes table options such as expiration_timestamp, description, labels, partition_expiration_days and require_partition_filter; setting an option to NULL clears it.
ALTER COLUMN changes a single column: SET DATA TYPE only widens types (INT64 to NUMERIC, BIGNUMERIC or FLOAT64; NUMERIC to BIGNUMERIC or FLOAT64; a larger STRING(n) or NUMERIC(p, s)), DROP NOT NULL makes a REQUIRED column NULLABLE, SET DEFAULT and DROP DEFAULT manage default values, and SET OPTIONS (description = ...) documents it. Narrowing or changing to an unrelated type (STRING to INT64, TIMESTAMP to DATE) needs a rewrite: CREATE OR REPLACE TABLE ... AS SELECT * REPLACE (CAST(col AS type) AS col) FROM ....
There is no ALTER TABLE for partitioning or clustering: partitioning is fixed at creation, and clustering is changed with bq update --clustering_fields or the API. ALTER TABLE statements count toward the limit of five table metadata updates per 10 seconds per table, so scripts that issue many in a row should batch the changes into one statement.
Syntax
ALTER TABLE [IF EXISTS] table_name ADD COLUMN [IF NOT EXISTS] column_name data_type [OPTIONS (...)][, ADD COLUMN ...]
ALTER TABLE table_name DROP COLUMN [IF EXISTS] column_name[, DROP COLUMN ...]
ALTER TABLE [IF EXISTS] table_name RENAME TO new_table_name
ALTER TABLE [IF EXISTS] table_name RENAME COLUMN [IF EXISTS] old_name TO new_name[, ...]
ALTER TABLE [IF EXISTS] table_name SET OPTIONS (option = value[, ...])
ALTER TABLE [IF EXISTS] table_name ALTER COLUMN [IF EXISTS] column_name
{ SET DATA TYPE data_type | DROP NOT NULL | SET DEFAULT expression | DROP DEFAULT | SET OPTIONS (...) }Arguments and return value
| Argument | Description |
|---|---|
ADD COLUMN | Appends NULLABLE or REPEATED columns; a new STRUCT may contain REQUIRED fields. Not for partitioning or clustering columns. |
DROP COLUMN | Removes columns from the schema. Storage is freed only when the data is next rewritten. |
RENAME TO / RENAME COLUMN | Renames the table (same dataset, name only) or top-level columns. Dependent views are not updated. |
SET OPTIONS | Table options such as expiration_timestamp, description, labels, partition_expiration_days, require_partition_filter. NULL clears an option. |
ALTER COLUMN SET DATA TYPE | Widening only: INT64 to NUMERIC, BIGNUMERIC or FLOAT64; NUMERIC to BIGNUMERIC or FLOAT64; larger parameterized types. |
Returns: No result set. The table's schema or metadata changes; no data is read or written.
Examples
Add columns with descriptions
IF NOT EXISTS makes the statement safe to rerun from a migration script.
ALTER TABLE `project.dataset.orders`
ADD COLUMN IF NOT EXISTS coupon_code STRING OPTIONS (description = 'Code applied at checkout'),
ADD COLUMN IF NOT EXISTS tags ARRAY<STRING>;Widen a column type and relax NOT NULL
ALTER TABLE `project.dataset.orders`
ALTER COLUMN amount SET DATA TYPE NUMERIC;
ALTER TABLE `project.dataset.orders`
ALTER COLUMN customer_id DROP NOT NULL;Set expiration, labels and a required partition filter
ALTER TABLE `project.dataset.events`
SET OPTIONS (
require_partition_filter = TRUE,
partition_expiration_days = 400,
labels = [('team', 'growth'), ('cost_center', 'cc_204')]
);
ALTER TABLE `project.scratch.tmp_backfill`
SET OPTIONS (expiration_timestamp = TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL 3 DAY));Rename columns, then the table
Renames run in order, so a swap needs a temporary name. RENAME TO takes only the new table name, not a dataset.
ALTER TABLE `project.dataset.orders`
RENAME COLUMN amount TO amount_gross,
RENAME COLUMN IF EXISTS net TO amount_net;
ALTER TABLE `project.dataset.orders` RENAME TO orders_v2;Common mistakes
- Changing a column to an unrelated or narrower type with
SET DATA TYPE, such as STRING to INT64 or FLOAT64 to INT64. Only widening is allowed; rebuild the table with CAST for anything else. - Expecting
DROP COLUMNto reduce storage cost right away. The column's bytes remain billed until the table is rewritten; useCREATE OR REPLACE TABLE t AS SELECT * EXCEPT (col) FROM t(repeating PARTITION BY and CLUSTER BY) when the saving matters now. - Adding a REQUIRED column:
ADD COLUMN status STRING NOT NULLfails on an existing table. Add it as NULLABLE, backfill it, then rely on the pipeline to fill it. - Passing a dataset to
RENAME TO.RENAME TO other_dataset.ordersis not allowed; a table can only be renamed within its dataset. Copy it to move it. - Running many ALTER TABLE statements in quick succession. Table metadata updates are limited to five per 10 seconds per table and fail with Exceeded rate limits: too many table update operations; combine ADD COLUMN clauses into one statement.
Cost and performance
- ALTER TABLE statements are metadata operations: they process no bytes and are not billed on demand. Rewrites that you do instead (CREATE OR REPLACE ... AS SELECT) bill a scan of the table and restart the long-term storage timer for all of it.
- After
DROP COLUMN, the table cannot be queried with legacy SQL or as part of a wildcard table, BI Engine stops accelerating it, and it can no longer be copied withbq cpuntil it is recreated with CREATE TABLE AS SELECT.
FAQ
How do I add a column to a BigQuery table?
ALTER TABLE project.dataset.t ADD COLUMN new_col STRING. The column is NULL for existing rows and is added at the end of the schema.
How do I change a column's data type in BigQuery?
Widening changes use ALTER TABLE t ALTER COLUMN c SET DATA TYPE NUMERIC. Any other change needs a rewrite: CREATE OR REPLACE TABLE t AS SELECT * REPLACE (CAST(c AS INT64) AS c) FROM t, keeping the table's PARTITION BY and CLUSTER BY clauses.
Can I add partitioning or clustering with ALTER TABLE?
No. Partitioning is fixed when the table is created; see PARTITION BY. Clustering can be changed with bq update --clustering_fields or the API; see CLUSTER BY.
Related
Errors you may hit
More DDL statements
All BigQuery DDL statements · All statements and clauses · BigQuery error messages
