offdataai
BigQuerySQLDDL

BigQuery Schema: Create Datasets, Define Table Schemas and Read Them Back

In BigQuery a schema is a dataset, and every table has a column schema. How to create a dataset with CREATE SCHEMA, define table schemas in DDL and JSON, use nested fields, partitioning and keys, and read a schema back.

ooffdataai3 min read

"BigQuery schema" means two things. In BigQuery SQL, a schema is a dataset: the container for tables and views, created with CREATE SCHEMA. Every table also has a table schema: its column names, types and modes. This guide covers both.

Create a dataset with CREATE SCHEMA

sql
CREATE SCHEMA IF NOT EXISTS `my-project.analytics`OPTIONS (  location = 'EU',  description = 'Star schemas for reporting',  default_table_expiration_days = NULL,  labels = [('team', 'data')]);

The location is fixed when the dataset is created and cannot be changed later, and queries cannot join tables in different locations, so choose it deliberately. The same can be done from the command line:

bash
bq mk --dataset --location=EU --description "Star schemas for reporting" my-project:analytics

Define a table schema in DDL

sql
CREATE TABLE IF NOT EXISTS `my-project.analytics.fct_orders` (  order_id     STRING NOT NULL,  customer_key INT64  NOT NULL,  order_ts     TIMESTAMP NOT NULL,  net_amount   NUMERIC(18, 2) NOT NULL,  channel      STRING OPTIONS (description = 'web, app or store'),  lines ARRAY<STRUCT<    product_key INT64,    quantity    INT64,    amount      NUMERIC(18, 2)  >>,  PRIMARY KEY (order_id) NOT ENFORCED)PARTITION BY DATE(order_ts)CLUSTER BY customer_key, channelOPTIONS (description = 'One row per order');

Points to note:

  • Modes: a column is NULLABLE by default, REQUIRED with NOT NULL, and REPEATED when it is an ARRAY.
  • Nested fields: STRUCT and ARRAY<STRUCT<...>> let you store order lines inside the order row. This can save joins, but a star schema with a separate line-level fact is often easier for BI tools and semantic layers.
  • Keys: BigQuery supports PRIMARY KEY and FOREIGN KEY constraints declared NOT ENFORCED. They are not checked, but the optimiser can use them, so declare only true constraints.
  • Partitioning and clustering reduce the data scanned, which is what you pay for on on-demand pricing.

Define a table schema in JSON

Loads and the bq tool use a JSON schema:

json
[  {"name": "order_id", "type": "STRING", "mode": "REQUIRED"},  {"name": "order_ts", "type": "TIMESTAMP", "mode": "REQUIRED"},  {"name": "net_amount", "type": "NUMERIC", "mode": "REQUIRED"},  {"name": "lines", "type": "RECORD", "mode": "REPEATED", "fields": [    {"name": "product_key", "type": "INTEGER", "mode": "NULLABLE"},    {"name": "quantity", "type": "INTEGER", "mode": "NULLABLE"}  ]}]
bash
bq mk --table my-project:analytics.fct_orders ./fct_orders_schema.json

Read a schema back

sql
SELECT column_name, data_type, is_nullableFROM `my-project.analytics`.INFORMATION_SCHEMA.COLUMNSWHERE table_name = 'fct_orders'ORDER BY ordinal_position;
bash
bq show --schema --format=prettyjson my-project:analytics.fct_orders

INFORMATION_SCHEMA.COLUMN_FIELD_PATHS lists nested fields individually.

Change a schema

sql
ALTER TABLE `my-project.analytics.fct_orders` ADD COLUMN coupon_code STRING;ALTER TABLE `my-project.analytics.fct_orders` ALTER COLUMN channel SET OPTIONS (description = 'Sales channel');ALTER TABLE `my-project.analytics.fct_orders` DROP COLUMN coupon_code;

Adding columns and relaxing REQUIRED to NULLABLE are cheap. Changing a column's type is limited to certain conversions; otherwise recreate the table with CREATE OR REPLACE TABLE ... AS SELECT.

Frequently asked questions

Is a BigQuery dataset the same as a schema?

Yes. In BigQuery SQL, CREATE SCHEMA creates a dataset, and the two words mean the same thing.

How do I see the schema of a BigQuery table?

Query INFORMATION_SCHEMA.COLUMNS for the dataset, or run bq show --schema --format=prettyjson project:dataset.table.

Does BigQuery enforce primary keys?

No. Primary and foreign keys must be declared NOT ENFORCED. They document the model and help the optimiser but are not checked.

What does DDL in BigQuery mean?

Data definition language: the CREATE, ALTER and DROP statements for datasets, tables, views and functions shown above.

Generate BigQuery DDL from a description

offdataai generates BigQuery DDL with datasets, partitioning, clustering and keys, plus a complete dbt project, from plain English or one of 174 templates. See the BigQuery schema generator.

Try offdataai

Ship a warehouse from a single conversation.

Describe your domain. Get ERDs, DDL, dbt projects, and seed data for every major cloud warehouse.

Start building

Keep reading

All posts →