How to easily make a empty table copying a existing table's schema in BigQuery?

Viewed 4958

I have a table named dataset.table_20180801, and wanna make a new empty table dataset.table_20180901 which has the same schema as dataset.table_20180801. I wanna know the easiest way to do this in BigQuery.

I can copy table using BigQuery web console's copy table, but it copies the records too.

I think a easier way is to first get the schema using

bq show --schema --format=prettyjson dataset.table_20180801 > ~/Desktop/foo_bar_schema.json

and next make a table using it. (I wonder if this is right)

bq mk --table dataset.table_20180901 ~/Desktop/foo_bar_schema.json

But this way is not easy enough.

Is there any easier ways to copy schema?

1 Answers

You can use DDL

CREATE TABLE dataset.table_20180901 AS
SELECT *
FROM dataset.table_20180801
LIMIT 0

having here limit 0 is the key, it won't incur costs and table will be created empty

Related