[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[[["\u003cp\u003eThe \u003ccode\u003eKEY_COLUMN_USAGE\u003c/code\u003e view provides information about columns in tables that are designated as keys, specifically those constrained by \u003ccode\u003ePRIMARY KEY\u003c/code\u003e and \u003ccode\u003eFOREIGN KEY\u003c/code\u003e constraints.\u003c/p\u003e\n"],["\u003cp\u003eThis view's schema includes details such as \u003ccode\u003eCONSTRAINT_NAME\u003c/code\u003e, \u003ccode\u003eTABLE_NAME\u003c/code\u003e, \u003ccode\u003eCOLUMN_NAME\u003c/code\u003e, \u003ccode\u003eORDINAL_POSITION\u003c/code\u003e, and \u003ccode\u003ePOSITION_IN_UNIQUE_CONSTRAINT\u003c/code\u003e, which help describe the relationships between constrained columns and their keys.\u003c/p\u003e\n"],["\u003cp\u003eQueries against \u003ccode\u003eKEY_COLUMN_USAGE\u003c/code\u003e require a dataset qualifier and appropriate dataset permissions, with different levels of scope depending on whether a dataset or region qualifier is used.\u003c/p\u003e\n"],["\u003cp\u003eThe view can be queried to show the constraints for a single table or for all tables in a dataset, but will return no data if a table or dataset lacks constraints.\u003c/p\u003e\n"],["\u003cp\u003eExample queries are provided to demonstrate how to retrieve constraint data for a specific table and how to create tables with \u003ccode\u003ePRIMARY KEY\u003c/code\u003e and \u003ccode\u003eFOREIGN KEY\u003c/code\u003e constraints.\u003c/p\u003e\n"]]],[],null,["# KEY_COLUMN_USAGE view\n=====================\n\nThe `KEY_COLUMN_USAGE` view contains columns of the tables from\n`TABLE_CONSTRAINTS` that are constrained as keys by\n[primary and foreign key](/bigquery/docs/primary-foreign-keys) constraints.\n\nSchema\n------\n\nThe `INFORMATION_SCHEMA.KEY_COLUMN_USAGE` view has the following schema:\n\nScope and syntax\n----------------\n\nQueries against this view must include a dataset qualifier. For queries with a\ndataset qualifier, you must have permissions for the dataset. For more\ninformation, see\n[Syntax](/bigquery/docs/information-schema-intro#syntax).\nThe following table shows the region and resource scopes for this view:\n\nReplace the following:\n\n- Optional: \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the ID of your Google Cloud project. If not specified, the default project is used.\n\n\u003cbr /\u003e\n\nFor queries with a dataset qualifier, you must have permissions for the dataset.\nFor queries with a region qualifier, you must have permissions for the project.\n\nExamples\n--------\n\n##### Example 1:\n\nThe following query shows the constraints for a single table in a dataset: \n\n```googlesql\nSELECT *\nFROM PROJECT_ID.DATASET.INFORMATION_SCHEMA.KEY_COLUMN_USAGE\nWHERE table_name = TABLE;\n```\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: Optional. The name of your cloud project. If not specified, this command uses the default project.\n- \u003cvar translate=\"no\"\u003eDATASET\u003c/var\u003e: The name of your dataset.\n- \u003cvar translate=\"no\"\u003eTABLE\u003c/var\u003e: The name of the table.\n\nConversely, the following query shows the key columns usage for all tables in a single dataset. \n\n```googlesql\nSELECT *\nFROM PROJECT_ID.DATASET.INFORMATION_SCHEMA.KEY_COLUMN_USAGE;\n```\n\nIf a table or a dataset has no constraints, the query results look like this: \n\n```\n+-----------------------------+\n| There is no data to display |\n+-----------------------------+\n```\n\n##### Example 2:\n\nThe following DDL statements create a primary key table and a foreign key table. \n\n```googlesql\nCREATE TABLE composite_pk (x int64, y string, primary key (x, y) NOT ENFORCED);\n``` \n\n```googlesql\nCREATE TABLE table composite_fk (x int64, y string, z string, primary key (x, y)\nNOT ENFORCED, CONSTRAINT composite_fk foreign key (z, x)\nREFERENCES composite_pk (y, x) NOT ENFORCED);\n```\n\nIf queried with the statement in [Example 1](#example_01), the query results\nare similar to the following. Note that `CONSTRAINT_CATALOG`,\n`CONSTRAINT_SCHEMA`, and duplicate columns are not included in the example results. \n\n```\n+---------------------------+--------------+-------------+------------------+-------------------------------+\n| CONSTRAINT_NAME | TABLE_NAME | COLUMN_NAME | ORDINAL_POSITION | POSITION_IN_UNIQUE_CONSTRAINT |\n+---------------------------+--------------+-------------+------------------+-------------------------------+\n| composite_pk.pk$ | composite_pk | x | 1 | NULL |\n| composite_pk.pk$ | composite_pk | y | 2 | NULL |\n| composite_fk.pk$ | composite_fk | x | 1 | NULL |\n| composite_fk.pk$ | composite_fk | y | 2 | NULL |\n| composite_fk.composite_fk | composite_fk | z | 1 | 2 |\n| composite_fk.composite_fk | composite_fk | x | 2 | 1 |\n+---------------------------+--------------+-------------+------------------+-------------------------------+\n```"]]