score:-1
Just run the following:
EXEC sp_fkeys @pktable_name = '<your_PK_table_name>'
score:0
SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
WHERE PK.TABLE_NAME=@PK_TABLE_NAME
---- optional:
ORDER BY
1,2,3,4
More questions
- Getting Foreign Key Ids from Primary Key
- Getting autonumber primary key from MS SQL Server
- How to add a primary (Surrogate) key from one table into a foreign key of another table?
- How to get newly inserted Ids from identity primary key column when using SqlBulkCopy
- Foreign keys from 2 columns of the same table to the same primary key
- SQL Server : multiple foreign keys from first table linking to the primary key of second table
- Update Column Of All Rows In A Table From Another Table Using Primary Key And Foreign Key
- How to insert primary key from one table as a foreign key into secondary table
- How to copy parent and child data from one database to another while assigning the newly created parent ids in the foreign key column of child tables?
- Update new foreign key column of existing table with ids from another table in SQL Server
- Getting the primary key from a username and password entry
- Get first record from joined table for each foreign key without duplicate primary keys
- Why primary key is getting repeated in the child tables as foreign key?
- set id primary key from on table to another table as foreign key
- Is it fine to have multiple foreign keys from one table to single primary key in another table?
- There are no primary or candidate keys in the referenced table that match the referencing column list in the foreign key
- Primary and Foreign Key at the same time
- SQL Server: Howto get foreign key reference from information_schema?
- can we have a foreign key which is not a primary key in any other table?
- Getting the primary key of an newly inserted row in SQL Server 2008
- SQL Server update primary key that's also a foreign key in two tables
- Change a Primary Key from Nonclustered to Clustered
- How to Identify the primary key duplication from a SQL Server 2008 error code?
- How to Delete all data from a table which contain self referencing foreign key
- Output last inserted primary key value from stored procedure
- delete primary key row which is foreign key of another table
- How to select a primary key which has exact foreign keys matches a given list of values?
- SQL Table Foreign Key that is part of a Composite Primary Key
- Is it bad to have a non-clustered index that contains the primary key from the clustered index?
- Primary Key Generated From External System
More questions with similar tag
- How to profile and address insert/update performance issues?
- How to prevent duplicate update in sql stored procedure
- "argument 'sql' cannot be null" when including "Go" in EF DbMigration
- How to delete the contents of a database in SQL Server Management Studio?
- SQL query to sum the data
- how to split single column values into multiple columns based on other column values
- How to detect READ_COMMITTED_SNAPSHOT is enabled?
- How to group datas by week in sql?
- get sum in query with two tables
- Table Variable In a table Valued function
- SQL Server - using DATALENGTH in where clause causes truncation error
- How to find rows in SQL DB with a definite word length?
- Sql server error Incorrect syntax near '('
- SQL Server : how to return several row into one variable
- SQL Server Group by Count of DateTime Per Hour?
- Different view of a query
- PDOException: SQLSTATE[42S02]
- How do convert row column values in to a column value
- Ensure that if key-word is not present for the category, then the data from its parent should be returned
- Executing merge statement via C# SqlCommand not working
- How to use system function in where SQL Server 2008?
- Row into column SQL Server 2005/8
- what is the difference between Coalesce and nullif
- SQL Server Group data by week but show the start of the week
- Why Parallel update database with .net leads to consistent result
- Preventing SQL Injection in non-executed SQL
- Calculate YTD and Prior YTD for any given month using SQL
- How to correctly set expression Query.CommandText in SSRS?
- SQL Server: Multiple max and min values of various fields with respective timestamps in a single row resultset
- SQL Server Decimal precision
Source:
stackoverflow.com