SQL Tips #39 - Listing All Keys in a Database


Tuesday, 20 August 2019

This query lists every key constraint in a database with its schema and table.

The Query

SELECT DISTINCT
    Constraint_Name AS [Constraint],
    Table_Schema AS [Schema],
    Table_Name AS [TableName] 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
ORDER BY Table_Name,Constraint_Name
GO

Disclaimer

These queries are provided as a guide, and are by no means perfect. I didn't write all of these - many are collated from MSDN documentation and community sources over the years.

Tags

SQL, Programming
Share with: 

Useful SQL tips - how to list all keys in a database


Support this Site

Buy me a coffee

Developer Courses

Pluralsight - Hardcore Developer and IT Training
Skype Web SDK: Getting Started
Skype Web SDK: Audio & Video

Popular Articles

What is Kutamo?
Kilimanjaro Climb
Kilimanjaro 2015
Kilimanjaro 2013
Australian Postcodes
New Zealand Postcodes
Worldwide City Database

Favourite Links

Kilimanjaro Climbs
Kutamo Studios
Kutamo Meetings
Litzi
ErrLog.IO
Kutamo

Tags / Keywords

SQL, Programming