Pages

Saturday, September 7, 2013

SQL SERVER – Two Methods to Retrieve List of Primary Keys and Foreign Keys of Database | Journey to SQL Authority with Pinal Dave

There are two different methods of retrieving the list of Primary Keys and Foreign Keys from database. Method 1: INFORMATION_SCHEMA SELECT DISTINCT Constraint_Name AS [Constraint], Table_Schema AS [Schema], Table_Name AS [TableName] FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE GO Method 2: sys.objects 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… Continue Reading...

Source: SQL SERVER – Two Methods to Retrieve List of Primary Keys and Foreign Keys of Database | Journey to SQL Authority with Pinal Dave