If I have the name of a field is there away to know (lookup?) whether this is a PK and if so in which table?
+3
A:
It is in the INFORMATION_SCHEMA.TABLE_CONSTRAINTS
see:
You have to be a little bit carefull. In theory you could have 2 fields with the same name in different tables, and the field being a primary key in one of them.
Shiraz Bhaiji
2010-01-10 19:43:05
+3
A:
Use the INFORMATION_SCHEMA views:
SELECT
T.TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS T
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE K
ON T.CONSTRAINT_NAME = K.CONSTRAINT_NAME
WHERE
T.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND K.COLUMN_NAME = @column_name
David M
2010-01-10 19:43:38