sp_fkeys (Transact-SQL)
Returns logical foreign key information for the current environment. This procedure shows foreign key relationships including disabled foreign keys.
Column name | Data type | Description |
|---|---|---|
PKTABLE_QUALIFIER | sysname | Name of the table (with the primary key) qualifier. This field can be NULL. |
PKTABLE_OWNER | sysname | Name of the table (with the primary key) owner. This field always returns a value. |
PKTABLE_NAME | sysname | Name of the table (with the primary key). This field always returns a value. |
PKCOLUMN_NAME | sysname | Name of the primary key columns, for each column of the TABLE_NAME returned. This field always returns a value. |
FKTABLE_QUALIFIER | sysname | Name of the table (with a foreign key) qualifier. This field can be NULL. |
FKTABLE_OWNER | sysname | Name of the table (with a foreign key) owner. This field always returns a value. |
FKTABLE_NAME | sysname | Name of the table (with a foreign key). This field always returns a value. |
FKCOLUMN_NAME | sysname | Name of the foreign key column, for each column of the TABLE_NAME returned. This field always returns a value. |
KEY_SEQ | smallint | Sequence number of the column in a multicolumn primary key. This field always returns a value. |
UPDATE_RULE | smallint | Action applied to the foreign key when the SQL operation is an update. SQL Server returns 0 or 1 for these columns: 0=CASCADE changes to foreign key. 1=NO ACTION changes if foreign key is present. |
DELETE_RULE | smallint | Action applied to the foreign key when the SQL operation is a deletion. SQL Server returns 0 or 1 for these columns: 0=CASCADE changes to foreign key. 1=NO ACTION changes if foreign key is present. |
FK_NAME | sysname | Foreign key identifier. It is NULL if not applicable to the data source. SQL Server returns the FOREIGN KEY constraint name. |
PK_NAME | sysname | Primary key identifier. It is NULL if not applicable to the data source. SQL Server returns the PRIMARY KEY constraint name. |
The results returned are ordered byFKTABLE_QUALIFIER, FKTABLE_OWNER, FKTABLE_NAME, and KEY_SEQ.
Application coding that includes tables with disabled foreign keys can be implemented by the following:
Temporarily disabling constraint checking (ALTER TABLE NOCHECK or CREATE TABLE NOT FOR REPLICATION) while working with the tables, and then enabling it again later. For more information about the NOT FOR REPLICATION option, see Controlling Constraints, Identities, and Triggers with NOT FOR REPLICATION.
Using triggers or application code to enforce relationships.
If the primary key table name is supplied and the foreign key table name is NULL, sp_fkeys returns all tables that include a foreign key to the given table. If the foreign key table name is supplied and the primary key table name is NULL, sp_fkeys returns all tables related by a primary key/foreign key relationship to foreign keys in the foreign key table.
The sp_fkeys stored procedure is equivalent to SQLForeignKeys in ODBC.