How to add a column to existing table in BigQuery?

ALTER TABLE ADD COLUMN statement

Alter statement allows us to modify the structure of the existing table. Using Alter Table Add Column statement, we can add one or more columns to the existing table in BigQuery.

Syntax

  • table_name – It is a name of the existing table.
  • IF NOT EXISTS – If the given column name is already exist, the statement has no effect.
  • column_name – The column to add in the table.
  • data_type – It is a data type of the new column.

Example 1: Add new column to a table

Let’s try to add the new columns in the existing table. We have a table loan_details as below in BigQuery. It maintains the customer’s loan details. The table has the following columns loan_no, customer_acc_no, loan_amt, interest_rate and approved_date.

Alter table add column in BigQuery
Alter table add column in BigQuery

Consider that we got the requirement to add the new column mode_of_payment in the existing table loan_details. The data type of this column is String. We can perform this task using Alter Table Add Column statement.

The alter statement has executed successfully and added the new column in the table as below.

Alter table add column example in BigQuery
Alter table add column example in BigQuery

By default, the value of the new column is NULL in BigQuery. Later on ,we can update the values in this column.

add column to existing BigQuery table
add column to existing BigQuery table

Example 2: Add multiple columns to a table

In this example, we will add the multiple new columns to the existing table loan_details. We don’t see the loan type and repayment period details in the table.

So we decided to two columns such as loan_type and repayment_period_in_yrs. The data type of those columns are String and Int respectively.

If we look at the Alter table statement, the keyword add column is mentioned for each column. Then the statement is executed successfully as below.

Add multiple columns to a table in BigQuery
Add multiple columns to a table in BigQuery

Example 3: Add nested columns in Struct data type

The data type Struct is a kind of container which holds the collection of fields with a field name and data type. Let’s add the new column with Struct type. Within that, we can define the set of columns.

To maintain the customer contact details, we are adding a new column as customer_contact which contains the customer’s mobile number, email_address and linked account list.

As mentioned above, the data type of the new column customer_contact is mentioned as Struct. Within that column, we are adding the set of columns using Alter statement.

Add nested columns in existing BigQuery table
Add nested columns in existing BigQuery table

Now all the new columns has null values. We tried to update those values to check the nested columns.

Struct data type in BigQuery
Struct data type in BigQuery

Finally we have added the nested columns using Alter table add column statement in BigQuery.

Recommended Articles