BigQuery Equivalent of “CREATE TABLE my_table (LIKE your_table)”

后端 未结 2 488
半阙折子戏
半阙折子戏 2021-01-13 18:17

I want to create a table which schema is exactly the same as another table. In other SQL engines, I think I was able to use \"CREATE TABLE my_table (LIKE your_table

相关标签:
2条回答
  • 2021-01-13 18:26

    Use this form:

    CREATE TABLE dataset.new_table AS
    SELECT *
    FROM dataset.existing_table
    LIMIT 0
    

    This creates a new table with the same schema as the old one, and there is no cost due to the LIMIT 0.

    Note that this does not preserve partitioning, table description, etc., however. Another option is to use the CLI (or API), making a copy of the table and then overwriting its contents, e.g.:

    $ bq cp dataset.existing_table dataset.new_table
    $ bq query --use_legacy_sql --replace --destination_table=dataset.new_table \
        "SELECT * FROM dataset.new_table LIMIT 0;"
    

    Now the new table has the same structure and attributes as the original did.

    0 讨论(0)
  • 2021-01-13 18:45

    To create a partitioned and/or clustered table the syntax would be:

    CREATE TABLE project.dataset.clustered_table PARTITION BY DATE(created_time) CLUSTER BY account_id AS SELECT * FROM project.dataset.example_table LIMIT 0

    0 讨论(0)
提交回复
热议问题