Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
| Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 29th, 2004, 10:42 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Determining Which Column is Primary Key

Hi all..

Using SQL commands, how can I determine which is the primary key column(s) for a certain table?

Thank you.

  #2 (permalink)  
Old December 29th, 2004, 11:37 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

you can try this

Code:
select      COLUMN_NAME
from      INFORMATION_SCHEMA.TABLE_CONSTRAINTS ,
     INFORMATION_SCHEMA.KEY_COLUMN_USAGE
where      INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_TYPE = 'PRIMARY KEY'
and      INFORMATION_SCHEMA.KEY_COLUMN_USAGE.CONSTRAINT_NAME = INFORMATION_SCHEMA.TABLE_CONSTRAINTS.CONSTRAINT_NAME
and      INFORMATION_SCHEMA.KEY_COLUMN_USAGE.TABLE_NAME = INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME
and      INFORMATION_SCHEMA.TABLE_CONSTRAINTS.TABLE_NAME = '<your table name>'
Jaime E. Maccou
  #3 (permalink)  
Old December 29th, 2004, 11:41 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

You can use this part for all tables within a particular database

Code:
select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS  where CONSTRAINT_TYPE = 'PRIMARY KEY'
Jaime E. Maccou
  #4 (permalink)  
Old December 30th, 2004, 12:03 AM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It works.. Thanks jemacc..

  #5 (permalink)  
Old August 16th, 2010, 09:16 AM
Registered User
 
Join Date: Aug 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default This serves the purpose

http://www.handysql.com/index.php?op...-2005&Itemid=1

Please also contribute to the website with your articles.

Thanks


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting duplicate values in primary key column, Shuchik SQL Server ASP 1 August 31st, 2007 05:38 AM
Foreign key not updating with Primary key xavier1945 BOOK: Access 2003 VBA Programmer's Reference 2 July 4th, 2007 09:48 PM
Finding Primary Key Column in Script johnpirkey SQL Server 2000 9 April 26th, 2007 10:06 AM
FOREIGN KEY and PRIMARY KEY Constraints junemo Oracle 10 June 15th, 2004 01:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.