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 andnotNULL.
  • 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, orisNULL.

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:

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:

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:

What's next

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.