views:

689

answers:

6

Using this SQL on SQL Server 2005

SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = @TableName
  AND COLUMN_NAME=@ColumnName

I get the Primary Keys AND the Foreign Keys.

How can I get only Foreign Keys? How can I see if a Constraint is a Primary or a Foreign Key?

Thanks

A: 

It depends on your DBMS but a constraint should have the target table and column somewhere.

If it's a foreign key constraint, the target table will be different to the current table. Try

select *

instead of

select constraint_name

to see all the magical columns you can work with.

paxdiablo
A: 
SELECT CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = @TableName
  AND COLUMN_NAME=@ColumnName
  AND REFERENCED_COLUMN_NAME IS NOT NULL
Quassnoi
A: 

It's not the most elegant of solutions but MS SQL Server uses the PK & FK prefix naming convention for keys so you could use something like...

SELECT CONSTRAINT_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = @TableName
AND LEFT(CONSTRAINT_NAME,2) = 'FK'

If that's your DBMS :)

EDIT

When I tried the answer by Quassnoi I got an "Invalid column name 'REFERENCED_COLUMN_NAME'." error.

Rich Andrews
I'm using SQL Server 2005
Marco Bettiolo
Yep, me too the above convention is used by SQL Server 2005
Rich Andrews
+1  A: 

Found a much more elegant solution here

Code added below for completeness but all credit goes to Pinal Dave

SELECT f.name AS ForeignKey,
   OBJECT_NAME(f.parent_object_id) AS TableName,
   COL_NAME(fc.parent_object_id, 
   fc.parent_column_id) AS ColumnName,
   OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
   COL_NAME(fc.referenced_object_id, 
   fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
LEFT JOIN sys.foreign_key_columns AS fc
   ON f.OBJECT_ID = fc.constraint_object_id

And if you just want the primary keys...

SELECT i.name AS IndexName,
        OBJECT_NAME(ic.OBJECT_ID) AS TableName,
        COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnName
FROM sys.indexes AS i
        INNER JOIN sys.index_columns AS ic
        ON i.OBJECT_ID = ic.OBJECT_ID 
    AND i.index_id = ic.index_id
WHERE i.is_primary_key = 1

Added this as an additional answer because it's so far removed from my previous one :)

Rich Andrews
A: 

I use something like that. It works in SQL Server 2000 as well.

select  object_name(fkx.constid) as fk_name,
        fkx.keyno as num,
        object_name(fkx.fkeyid) as child_table,
        col_name(fkx.fkeyid, fkx.fkey) as child_column,
        object_name(fkx.rkeyid) as parent_table,
        col_name(fkx.rkeyid, fkx.rkey) as parent_column
from sysforeignkeys fkx
order by fk_name, fkx.keyno

It's all in system tables and views which are pretty well documented.

Tomek Szpakowicz
+1  A: 

I used the following SQL with SQL Server 2005 to get the Constraint Names by Primary Key:

SELECT Constraint_Name 
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
WHERE UNIQUE_CONSTRAINT_NAME = 
   (SELECT CONSTRAINT_NAME 
    FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
    WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName)

To get the Constraint Name by Foreing Key:

SELECT CONSTRAINT_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE TABLE_NAME = @TableName AND COLUMN_NAME = @ColumnName

To get the Foreign Key's Table and Field by Constraint Name:

SELECT TABLE_NAME, COLUMN_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE CONSTRAINT_NAME = @ConstraintName

To get the Primary Key's Table and Field by Constraint Name:

SELECT TABLE_NAME, COLUMN_NAME 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE 
WHERE CONSTRAINT_NAME = 
    (SELECT UNIQUE_CONSTRAINT_NAME 
     FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS 
     WHERE CONSTRAINT_NAME = @ConstraintName)
Marco Bettiolo