"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
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:
bq mk --dataset --location=EU --description "Star schemas for reporting" my-project:analyticsDefine a table schema in DDL
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
NULLABLEby default,REQUIREDwithNOT NULL, andREPEATEDwhen it is anARRAY. - Nested fields:
STRUCTandARRAY<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 KEYandFOREIGN KEYconstraints declaredNOT 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:
[ {"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"} ]}]bq mk --table my-project:analytics.fct_orders ./fct_orders_schema.jsonRead a schema back
SELECT column_name, data_type, is_nullableFROM `my-project.analytics`.INFORMATION_SCHEMA.COLUMNSWHERE table_name = 'fct_orders'ORDER BY ordinal_position;bq show --schema --format=prettyjson my-project:analytics.fct_ordersINFORMATION_SCHEMA.COLUMN_FIELD_PATHS lists nested fields individually.
Change a schema
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.
