Use primary and foreign keys
Primary keys and foreign keys are table constraints that can help withquery optimization. This document explains how to create, view, and manageconstraints, and use them to optimize your queries.
BigQuery supports the following key constraints:
- Primary key: A primary key for atable is a combination of one or more columns that is unique for each row andnot
NULL. - Foreign key: A foreign key for a table is a combination of one or morecolumns that is present in the primary key column of a referenced table, oris
NULL.
Primary and foreign keys are typically used to ensure data integrity and enablequery optimization. BigQuery doesn't enforce primary and foreignkey constraints. When you declare constraints on your tables, you must ensurethat your data conforms to them. BigQuery can use tableconstraints to optimize your queries.
Manage constraints
Primary and foreign key relationships can be created and managed through thefollowing DDL statements:
- Create primary and foreign key constraints when you create a table by usingthe
CREATE TABLEstatement. - Add a primary key constraint to an existing table by using the
ALTER TABLE ADD PRIMARY KEYstatement. - Add a foreign key constraint to an existing table by using the
ALTER TABLE ADD FOREIGN KEYstatement. - Drop a primary key constraint from a table by using the
ALTER TABLE DROP PRIMARY KEYstatement. - Drop a foreign key constraint from a table by using the
ALTER TABLE DROP CONSTRAINTstatement.
You can also manage table constraints through the BigQuery APIby updating theTableConstraints object.
View constraints
The following views give you information about your table constraints:
- The
INFORMATION_SCHEMA.TABLE_CONSTRAINTSviewcontains information about all of the primary and foreign key constraintson tables within a dataset. - The
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGEviewcontains information about each table's primary key columns and columnsreferenced by foreign keys from other tables within a dataset. - The
INFORMATION_SCHEMA.KEY_COLUMN_USAGEviewcontains information about each table's columns that are constrained asprimary or foreign keys.
Optimize queries
When you create and enforce primary and foreign keys on your tables,BigQuery can use that information to eliminate or optimizecertain query joins. While it's possible to mimic these optimizations byrewriting your queries, such rewrites aren't always practical.
In a production environment, you might create views that join many fact anddimension tables. Developers can query the views instead of querying theunderlying tables and manually rewriting the joins each time. If you definethe proper constraints, join optimizations happen automatically for anyqueries they apply to.
Caution: Key constraints aren't enforced in BigQuery. You areresponsible for maintaining the constraints at all times. Queries overtables with violated constraints might return incorrect results.The examples in the following sections reference thestore_salesandcustomer tables with constraints:
CREATETABLEmydataset.customer(customer_nameSTRINGPRIMARYKEYNOTENFORCED);CREATETABLEmydataset.store_sales(itemSTRINGPRIMARYKEYNOTENFORCED,sales_customerSTRINGREFERENCESmydataset.customer(customer_name)NOTENFORCED,categorySTRING);Eliminate inner joins
Consider the following query that contains anINNER JOIN:
SELECTss.*FROMmydataset.store_salesASssINNERJOINmydataset.customerAScONss.sales_customer=c.customer_name;Thecustomer_name column is a primary key on thecustomer table, soeach row from thestore_sales table has either a single match, or no matchifsales_customer isNULL. Since the query only selects columns from thestore_sales table, the query optimizer can eliminate the join and rewrite thequery as the following:
SELECT*FROMmydataset.store_salesWHEREsales_customerISNOTNULL;Eliminate outer joins
To remove aLEFT OUTER JOIN, the join keys on the right side must be uniqueand only columns from the left side are selected. Consider the following query:
SELECTss.*FROMmydataset.store_salesssLEFTOUTERJOINmydataset.customercONss.category=c.customer_name;In this example, there is no relationship betweencategory andcustomer_name. The selected columns only come fromthestore_sales table and the join keycustomer_name is a primary key on thecustomer table, so each value isunique. This means that there is exactly one (possiblyNULL) match in thecustomer table for each row in thestore_sales table and theLEFT OUTER JOIN can be eliminated:
SELECTss.*FROMmydataset.store_sales;Reorder joins
When BigQuery can't eliminate a join, it can use tableconstraints to get information about join cardinalities and optimize the orderin which to perform joins.
Limitations
Primary keys and foreign keys are subject to the following limitations:
- Key constraints are unenforced in BigQuery. You areresponsible for maintaining the constraints at all times. Queries overtables with violated constraints might return incorrect results.
- Primary keys can't exceed 16 columns.
- Foreign keys must have values that are present in the referenced tablecolumn. These values can be
NULL. - Primary keys and foreign keys must be of one of the following types:
BIGNUMERIC,BOOLEAN,DATE,DATETIME,INT64,NUMERIC,STRING,orTIMESTAMP. - Primary keys and foreign keys can only be set on top-level columns.
- Primary keys can't be named.
- Tables with primary key constraints can't be renamed.
- A table can have up to 64 foreign keys.
- A foreign key can't refer to a column in the same table.
- Fields that are part of primary key constraints or foreign keyconstraints can't be renamed, or have their type changed.
- If youcopy,clone,restore,orsnapshota table without the
-aor--append_tableoption, the source tableconstraints are copied and overwritten to the destination table. If you usethe-aor--append_tableoption, only the source table records areadded to the destination table without the table constraints.
What's next
- Learn more about how toOptimize query computation.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.