TABLE_CONSTRAINTS view
TheTABLE_CONSTRAINTS view containsthe primary and foreign keyrelations in a BigQuery dataset.
Required permissions
You need the followingIdentity and Access Management (IAM) permissions:
bigquery.tables.getfor viewing primary and foreign key definitions.bigquery.tables.listfor viewing table information schemas.
Each of the followingpredefined roleshas the needed permissions to perform the workflows detailed in this document:
roles/bigquery.dataEditorroles/bigquery.dataOwnerroles/bigquery.admin
For more information about IAM roles and permissions inBigQuery, seePredefined roles and permissions.
Schema
TheINFORMATION_SCHEMA.TABLE_CONSTRAINTS view has the following schema:
| Column Name | Type | Meaning |
|---|---|---|
|
| The constraint project name. |
|
| The constraint dataset name. |
|
| The constraint name. |
|
| The constrained table project name. |
|
| The constrained table dataset name. |
|
| The constrained table name. |
|
| EitherPRIMARY KEY orFOREIGN KEY. |
|
| YES orNO depending on if a constraint is deferrable. OnlyNO is supported. |
|
| OnlyNO is supported. |
|
| YES orNO depending on if the constraint isenforced.Only NO is supported. |
For stability, we recommend that you explicitly list columns in your information schema queries instead ofusing a wildcard (SELECT *). Explicitly listing columns prevents queries frombreaking if the underlying schema changes.
Scope and syntax
Queries against this view must include a dataset qualifier. For queries with adataset qualifier, you must have permissions for the dataset. For moreinformation seeSyntax.The following table shows the region and resource scopes for this view:
| View name | Resource scope | Region scope |
|---|---|---|
[PROJECT_ID.]DATASET.INFORMATION_SCHEMA.TABLE_CONSTRAINTS; | Dataset level | Dataset location |
- Optional:
PROJECT_ID: the ID of your Google Cloud project. If not specified, the default project is used.
Examples
The following query shows the constraints for a single table in a dataset:
SELECT*FROMPROJECT_ID.DATASET.INFORMATION_SCHEMA.TABLE_CONSTRAINTSWHEREtable_name=TABLE;
Replace the following:
PROJECT_ID: Optional. The name of your cloudproject. If not specified, this command uses the default project.DATASET: The name of your dataset.TABLE: The name of the table.
Conversely, the following query shows the constraints for all tables in asingle dataset.
SELECT*FROMPROJECT_ID.DATASET.INFORMATION_SCHEMA.TABLE_CONSTRAINTS;
With existing constraints, the query results are similar to the following:
+-----+---------------------+-------------------+-----------------------+---------------------+--------------+------------+-----------------+---------------+--------------------+----------+| Row | constraint_catalog | constraint_schema | constraint_name | table_catalog | table_schema | table_name | constraint_type | is_deferrable | initially_deferred | enforced |+-----+---------------------+-------------------+-----------------------+---------------------+--------------+------------+-----------------+---------------+--------------------+----------+| 1 | myConstraintCatalog | myDataset | orders.pk$ | myConstraintCatalog | myDataset | orders | PRIMARY KEY | NO | NO | NO || 2 | myConstraintCatalog | myDataset | orders.order_customer | myConstraintCatalog | myDataset | orders | FOREIGN KEY | NO | NO | NO |+-----+---------------------+-------------------+-----------------------+---------------------+--------------+------------+-----------------+---------------+--------------------+----------+
If the table or dataset has no constraints, the query results look like this:
+-----------------------------+| There is no data to display |+-----------------------------+
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.