Wednesday, May 30, 2012

Find list of primary keys in a database in sql server

Here i will show how to get the all list of existing primary key field's in data base in sqlserver.For this i have write a simple sql query to get constraint in tables.Here i have three table which are emp,Department,city.

Sql query:
USE [Test Krishna]

SELECT pk.TABLE_NAME AS TableName,

pk.CONSTRAINT_NAME AS PrimaryKeyFieldInTable FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk

WHERE pk.CONSTRAINT_TYPE ='PRIMARY KEY' 
Post a Comment

Bel