473,385 Members | 1,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using SQL to find primary keys

What query do I use to list the primary key for each user table, i.e.

TABLE | PRIMARY_KEY |

Regards,
Alan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
4 42681
One method:

SELECT
TABLE_SCHEMA,
TABLE_NAME,
CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY'
ORDER BY
TABLE_SCHEMA,
TABLE_NAME

--
Hope this helps.

Dan Guzman
SQL Server MVP

"alan" <an*******@devdex.com> wrote in message
news:40***********************@news.frii.net...
What query do I use to list the primary key for each user table, i.e.

TABLE | PRIMARY_KEY |

Regards,
Alan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #2
"alan" <an*******@devdex.com> wrote in message news:40***********************@news.frii.net...
What query do I use to list the primary key for each user table, i.e.

TABLE | PRIMARY_KEY |

Regards,
Alan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


USE pubs
go

SELECT KU.*
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME
ORDER BY KU.TABLE_NAME, KU.ORDINAL_POSITION

CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME
COLUMN_NAME ORDINAL_POSITION
pubs dbo UPKCL_auidind pubs dbo authors au_id 1
pubs dbo PK_emp_id pubs dbo employee emp_id 1
pubs dbo PK__jobs__117F9D94 pubs dbo jobs job_id 1
pubs dbo UPKCL_pubinfo pubs dbo pub_info pub_id 1
pubs dbo UPKCL_pubind pubs dbo publishers pub_id 1
pubs dbo UPKCL_sales pubs dbo sales stor_id 1
pubs dbo UPKCL_sales pubs dbo sales ord_num 2
pubs dbo UPKCL_sales pubs dbo sales title_id 3
pubs dbo UPK_storeid pubs dbo stores stor_id 1
pubs dbo UPKCL_taind pubs dbo titleauthor au_id 1
pubs dbo UPKCL_taind pubs dbo titleauthor title_id 2
pubs dbo UPKCL_titleidind pubs dbo titles title_id 1

Regards,
jag
Jul 20 '05 #3
jag,

That's exactly what I was looking for.

Thanks.

Regards,
Alan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4
Dan,

Your query produced results such as (Northwind):

Table | Key
Customers | PK_Customers
Employees | PK_Employees

What I wanted was:

Table | Key
Customers | CustomerID
Employees | EmployeeID

However, the info about INFORMATION_SCHEMA was gold dust. I didn't know
all these tables existed.

John Gilson's reply gave me the answer that I was looking for.

Thanks.

Regards,
Alan

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ilan Sebba | last post by:
I am trying to add a record using SQL. My problem is that the primary keys are foreign keys, and these foreign keys are autonumbers. I therefore do not know the primary keys of the record I am...
7
by: Philip | last post by:
Hey all, (Access 2000) I've been having a horror story with this design problem. My Database is Structured like This: AUTHORS, BOOKS, PAGES. Those are the Tables and each Item in each table...
7
by: Dave | last post by:
Hi, Maybe I'm missing something with the DataKeyField attribute of a datagrid but it seems that it's somewhat limiting since this only allows you to specify one field as the key. I have a...
3
by: rnettle | last post by:
Can anyone tell me how to use the DataRowCollection.Find Method on 3 primary keys, 2 of the keys are of the String type and the other is of the DateTime type. I have tried the code below but get...
115
by: LurfysMa | last post by:
Most of the reference books recommend autonum primary keys, but the Access help says that any unique keys will work. What are the tradeoffs? I have several tables that have unique fields. Can...
8
by: Imicola | last post by:
Hi, I want to use primary key IDs in my tables which are autonumbers, with a text prefix. I.e. for table tblVillage, the ID's will by V1, V2, V3... and for table tblHumanCases, the ID's will be...
2
by: Danny | last post by:
Hello, We imported a bunch of tables from a database and realized that the primary keys weren't copied to the destination db. In order to re- create the keys, we need to know which tables have...
4
by: Peter | last post by:
I am interested in informed feedback on the use of Constraints, Primary Keys and Unique. The following SQL statement creates a Bands tables for a database of bookings Bands into Venues, where the...
1
by: =?Utf-8?B?Rm9lZg==?= | last post by:
I have a BindingSource with an underlying table with two primary keys. To set the position with one primary key works fine as shown in the code snippet below: Dim index As Integer =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.