REdim.Learning
REdim.Learning

Reputation: 663

Update Big Query Table Schema

I have a table already in BQ that is populated with data. I want to rename the headings (update the schema) of the table. I'm using command line tool

Presuming it's something along the lines of this??

bq update --schema:Col1:STRING,Col2:STRING....... data_set.Table_Name

But I'm getting

FATAL Flags parsing error: Unknown command line flag 'schema:Col1:STRING,Col2:STRING.....'

What am I missing?

Upvotes: 4

Views: 8838

Answers (3)

SANN3
SANN3

Reputation: 10069

Now BigQuery supports renaming the column's via sql query

ALTER TABLE [IF EXISTS] table_name
RENAME COLUMN [IF EXISTS] column_to_column[, ...]

column_to_column :=
    old_column_name TO new_column_name

https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_table_rename_column_statement

Upvotes: 1

Felipe Hoffa
Felipe Hoffa

Reputation: 59155

As Mosha says, renaming columns is not supported via API, but you could run a query that scans the whole table and overwrites it.

bq query --nouse_legacy_sql \
 --destination_table p:d.table \
 --replace \
 'SELECT * EXCEPT(col1,col2), col1 AS newcol1, col2 AS newcol2 FROM `p.d.table`'

Warning: This overwrites the table. But that's what you wanted anyways.

Upvotes: 4

Mosha Pasumansky
Mosha Pasumansky

Reputation: 13994

The correct syntax on command line would be

bq update --schema col1:STRING,col2,STRING dataset.table

However, renaming fields is not supported schema change - you will get error message saying

Provided Schema does not match table

You can only add new fields or relax existing fields (i.e. from REQUIRED to NULLABLE).

Upvotes: 1

Related Questions