Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2002
    Posts
    19

    Question Unanswered: How to fetch a primary key?

    In a create table statement I defined a primary key on two columns as table level constraint. This primary key I named mibiprod_pkey.
    Now the question:
    How can I fetch this primary key? A Select statement doesn't work because the PK isn't inserted in a column.

    regards boa

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    the pk isn't inserted in a column? can you explain what you mean? the primary key is defined as one or more columns, so just fetch those columns

    i guess i don't understand the question

    can you give the DDL used to create the table?


    rudy

  3. #3
    Join Date
    Nov 2002
    Posts
    19
    Ok - the explanation:
    I a C program I use Embedded SQL for accessing the db.
    In one function I fetch one tuple, write the data into a struct and return also the address of the tuple. This address is normaly the primary key.
    But in this special case the primary key is built from two columns. And would like to return the primary key as the address. So - how can I fetch the primary key itself? Isn't this internally representated as a number?

    regards boa

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    the primary key is those columns

    if you want the value of the primary key, select the columns


    rudy

  5. #5
    Join Date
    Dec 2002
    Posts
    8
    the solution is quite easy

    here it comes! lets say you want to get the primary keys of a table named foo!

    just try:

    "select indexdef from pg_indexes where tablename=foo"

    this select should return a string that looks like that

    "CREATE UNIQUE INDEX test1_pkey ON test1 USING btree (test1id)"

  6. #6
    Join Date
    Dec 2002
    Posts
    8
    the solution is quite easy

    here it comes! lets say you want to get the primary keys of a table named foo!

    just try:

    "select indexdef from pg_indexes where tablename=foo"

    this select should return a string that looks like that

    "CREATE UNIQUE INDEX foo_pkey ON foo USING btree (pk1, pk2)"
    ^
    |
    |
    Our PKs
    now the only thing you have to do is to extract the data from the string and vioala you get your pks
    if you want to get a particullar pk i.e. use the column names to select them

    currently i'm working on a better solution! i hope i was able to help you!

    mfg, mingy

    Last edited by mingy; 01-14-03 at 10:26.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •