Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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.

Reply With Quote
  #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
Reply With Quote
  #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
Reply With Quote
  #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..

Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:01 PM.


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