Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate a table in GBQ

I am trying to truncate an existing table in GBQ but the below command fails when I run it. Is there any specific command or syntax to do that. I looked into GBQ documentation but no luck.

TRUNCATE TABLE [dw_test.test]; 
like image 914
Teja Avatar asked May 13 '15 17:05

Teja


People also ask

How do I truncate a table?

To remove all data from an existing table, use the SQL TRUNCATE TABLE order. You can also use the DROP TABLE command to delete an entire table. But Truncate will remove the entire table structure from the database, and you will need to recreate the table if you want to store any data.

How do you delete data from a BQ table?

You can delete a BigQuery table in the following ways: BigQuery Delete Table: Using the Cloud Console. BigQuery Delete Table: Using a Data Definition Language (DDL) DROP TABLE statement. BigQuery Delete Table: Using the bq command-line tool bq rm command.

How do you truncate a row in SQL?

TRUNCATE TABLE removes all rows from a table, but the table structure and its columns, constraints, indexes, and so on remain. To remove the table definition in addition to its data, use the DROP TABLE statement.


2 Answers

While BigQuery didn't used to support anything other than SELECTs, it now does as long as you uncheck "Use Legacy SQL" in the query options. There is no truncation, but you can delete:

DELETE from my_table WHERE 1=1 

Note that BigQuery requires the use of WHERE in the DELETE, so if you want to delete everything you need to use a statement that will always be true.

like image 144
Xiong Chiamiov Avatar answered Sep 18 '22 13:09

Xiong Chiamiov


Good news, TRUNCATE TABLE is supported by now: https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#truncate_table_statement

TRUNCATE TABLE [[project_name.]dataset_name.]table_name 

However, please note that this will not work / is not supported, if a partition filter is required through your table definition.

like image 24
laol Avatar answered Sep 20 '22 13:09

laol