REFERENTIAL_INTEGRITY_COUNT
functionsupportedGovernance (policies, tags, data quality)Enterprise+Returns the count of rows in the source table where the column value does not have a corresponding match in the referenced table. These unmatched rows are known as orphaned rows and represent violations of referential integrity. If you specify more than one column argument, returns the count of rows where the combination of the specified source columns does not match any row in the referenced table based on the corresponding reference columns. Data Quality Monitoring requires Enterprise Edition. To inquire about upgrading, please contact Snowflake Support.
Syntax
SNOWFLAKE.CORE.REFERENTIAL_INTEGRITY_COUNT ON ( <column>, TABLE(<ref_table>(<ref_column>)) )
For compound (multi-column) keys
SNOWFLAKE.CORE.REFERENTIAL_INTEGRITY_COUNT ON (
<column1>, <column2>, ...,
TABLE(<ref_table>(<ref_column1>, <ref_column2>, ...))
)
Parameters
| Name | Required | Description |
|---|---|---|
column | required | Specifies one or more columns in the source table whose values are checked for referential integrity against the referenced table. When multiple source columns are specified, they form a compound key. |
TABLE(ref_table(ref_column)) | required | Specifies the referenced (parent) table and column(s) to validate against, using the TABLE(...) syntax: ref_table — The fully qualified name of the referenced table (for example, my_db.my_schema.my_table). ref_column — One or more columns in the referenced table that correspond to the source columns. The number and order of source columns must match the number and order of reference columns. |
Returns
The function returns a NUMBER value. - A return value of 0 means every row in the source table has a corresponding match in the referenced table. Referential integrity is fully satisfied. - A return value of N > 0 means there are N rows in the source table that do not have a matching row in the referenced table. These N rows are considered orphaned.
Usage notes
- You can’t call this function directly. To learn how to associate the function with a table or view so it runs at regular intervals, see Associate a DMF. You can use the SYSTEM$DATA_METRIC_SCAN function to run the REFERENTIAL_INTEGRITY_COUNT function against a table without associating it.
- NULL values in the source column are not counted as violations. Rows where the source column value is NULL are excluded from the referential integrity check. This follows standard Foreign Key (FK) constraint semantics, where a NULL foreign key is considered valid. If you need to monitor NULL values in the source column, use the NULL_COUNT DMF alongside this function.
- The number and order of source columns must match the reference columns. This is validated when you associate the function.
- Renaming a column that is specified in the REFERENTIAL_INTEGRITY_COUNT function breaks the association between the function and the column’s table or view. If you rename the column, you must re-associate the function with the table or view.
- You cannot associate this function with the same column and reference table combination more than once.
Allowed data types
The columns specified in the column and ref_column arguments can contain any of the following data types: - DATE - FLOAT - NUMBER - TIMESTAMP_LTZ - TIMESTAMP_NTZ - TIMESTAMP_TZ - VARCHAR