How to query for primary keys

  • Mark,

    You could try this query:

    select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE

    Good Luck,

    Darrell

  • Yes indeed - there's more than 1 way to skin this cat...

    don't forget to add "where table_name = 'TableName' to Darrell's query!

    psarrell - I've been meaning to thank you for your comments the other day ..sorry about doing so on a completely different and irrelevant post but want to do it before I forget...<:-)







    **ASCII stupid question, get a stupid ANSI !!!**

  • Sorry sushila!  You are right!  My fault.

    The test database I am working with has no primary keys in any tables.  When I used your query with another database, I can see that indeed the rows in sysobjects beginning with PK expose primary keys.

    One more question....

    How do I determine the NAME of the primary key column?  Nothing in the SYSOBJECTS table seems to tie it to SYSCOLUMNS.

    Thanks!

    Mark

  • Here's ONE way...

    using Darrell's query:

    select column_name from INFORMATION_SCHEMA.KEY_COLUMN_USAGE

    where table_name = 'TableName' and constraint_name like 'PK%'







    **ASCII stupid question, get a stupid ANSI !!!**

  • sushila,

    You are welcome for the comments!

  • Darrell - as Remi is my current role model (may change next week)...

    hth! Amen! <:-)







    **ASCII stupid question, get a stupid ANSI !!!**

  • All that because I'm going on vacation???

  • Yes indeed - for Phill may well take your place and beat you at your own game!

    hth!!!







    **ASCII stupid question, get a stupid ANSI !!!**

  • So be it... have other things to do on my vacations .

  • Amen!







    **ASCII stupid question, get a stupid ANSI !!!**

  • Thanks to all.  This query works great!

    select column_name from INFORMATION_SCHEMA.KEY_COLUMN_USAGE

    where table_name = 'TableName' and constraint_name like 'PK%'

    What does HTH mean?

    Mark

  • Hope that/this helps!

    I've been learning a whole bunch of acronyms ever since I became a member - I frequently play guessing games myself....!!!







    **ASCII stupid question, get a stupid ANSI !!!**

  • Oops, I thaught it meant happy to help .

  • That works too - as does "heart to heart", "hell this hurts" etc. depending on the context...and splly. for you Remi - for the coming 2 weeks - "hit the highway"....!!!







    **ASCII stupid question, get a stupid ANSI !!!**

  • HTP

    Hit the poolroom (hard) .

Viewing 15 posts - 16 through 30 (of 35 total)

You must be logged in to reply to this topic. Login to reply