Alternating ways of getting both Primary and Foreign Keys on Tables

Vishalneeraj-24503
Posted by Vishalneeraj-24503 under Sql Server category on | Points: 40 | Views : 183
1st way:-
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc IN ('FOREIGN_KEY_CONSTRAINT','PRIMARY_KEY_CONSTRAINT');


2nd way:-
SELECT 
constraint_name = name,
foreign_schema = OBJECT_SCHEMA_NAME(parent_object_id),
foreign_table = OBJECT_NAME(parent_object_id),
referenced_schema = OBJECT_SCHEMA_NAME(referenced_object_id),
referenced_table = OBJECT_NAME(referenced_object_id)
FROM sys.foreign_keys
WHERE OBJECT_NAME(referenced_object_id) = 'your_table name';

Comments or Responses

Login to post response