Skip to main content

KEY_COLUMN_USAGE

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 11.3 LTS and above check marked yes Unity Catalog only

Preview

This feature is in Public Preview.

INFORMATION_SCHEMA.KEY_COLUMN_USAGE lists the columns of the primary or foreign key constraints within the catalog.

The rows returned are limited to constraints on the tables, depending on user privileges.

Definition

The KEY_COLUMN_USAGE relation contains the following columns:

NameData typeNullableStandardDescription
CONSTRAINT_CATALOGSTRINGNoYesCatalog containing the constraint.
CONSTRAINT_SCHEMASTRINGNoYesSchema containing the constraint.
CONSTRAINT_NAMESTRINGNoYesName of the constraint.
TABLE_CATALOGSTRINGNoYesCatalog containing the table.
TABLE_SCHEMASTRINGNoYesSchema containing the table.
TABLE_NAMESTRINGNoYesName of the table in which the constraint is defined.
COLUMN_NAMESTRINGNoYesName of the column.
ORDINAL_POSITIONINTEGERNoYesPosition (1-based) of the column in the key.
POSITION_IN_UNIQUE_CONSTRAINTINTEGERYesYesFor foreign key, position (1-based) of the column in parent unique or primary key constraint, NULL otherwise.

Constraints

The following constraints apply to the KEY_COLUMN_USAGE relation:

ClassNameColumn ListDescription
Primary keyKEY_COL_USG_PKCONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME, COLUMN_NAMEUniquely identifies the relation.
Unique keyKEY_COL_USG_OP_UKCONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME, ORDINAL_POSITIONUniquely identifies the relation.
Unique keyKEY_COL_USG_PIUK_UKCONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME, POSITION_IN_UNIQUE_CONSTRAINTUniquely identifies the relation.
Foreign keyKEY_COL_USG_COLUMNS_FKTABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAMEReferences COLUMNS.
Foreign keyKEY_COL_USG_TAB_CONS_FKCONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAMEReferences TABLE_CONSTRAINTS.

Examples

SQL
> SELECT constraint_name, column_name
FROM information_schema.key_column_usage
WHERE table_schema = 'information_schema'
AND table_name = 'tables';