Thread: SQL-Query 2 get primary key

SQL-Query 2 get primary key

From
Marc Grimme
Date:
Hi,

if I create a table like this:
CREATE TABLE test (
   id decimal(3) primary key,
   name varchar(32));

how can I ask postgres which is the primary key from table test?
I hope to get something like "id is the primary key from table test.".
Any ideas
Marc
--
-----------------------------------------------------------
Marc Grimme - An der Muehle 1 - 85716 Unterschleissheim
Fon: +49 89 37 48 81 22 - +49 89 37 48 92 7 - 3/0 -
mail2: grimme@informatik.tu-muenchen.de, grimme@atix.de
-----------------------------------------------------------
The UNIX Guru's View of Sex:
# unzip ; strip ; touch ; finger ; mount ; fsck ; more ; yes ; umount ;
sleep

Re: [SQL] SQL-Query 2 get primary key

From
"jose' soares"
Date:
You may query pg_indexes as in:

select * from pg_indexes where tablename = 'test';

tablename|indexname|indexdef
---------+---------+-----------------------------------------------------------------------
test     |test_pkey|CREATE UNIQUE INDEX "test_pkey" ON "test" USING btree ("id" "int4_ops")
(1 row)
 
 

Marc Grimme ha scritto:

Hi,

if I create a table like this:
CREATE TABLE test (
   id decimal(3) primary key,
   name varchar(32));

how can I ask postgres which is the primary key from table test?
I hope to get something like "id is the primary key from table test.".
Any ideas
Marc
--
-----------------------------------------------------------
Marc Grimme - An der Muehle 1 - 85716 Unterschleissheim
Fon: +49 89 37 48 81 22 - +49 89 37 48 92 7 - 3/0 -
mail2: grimme@informatik.tu-muenchen.de, grimme@atix.de
-----------------------------------------------------------
The UNIX Guru's View of Sex:
# unzip ; strip ; touch ; finger ; mount ; fsck ; more ; yes ; umount ;
sleep

--
                               - Jose' -

And behold, I tell you these things that ye may learn wisdom; that ye may
learn that when ye are in the service of your fellow beings ye are only
in the service of your God.                - Mosiah 2:17 -
 

Re: [SQL] SQL-Query 2 get primary key

From
Marc Grimme
Date:
Hi,

jose' soares wrote:
>
> You may query pg_indexes as in:
>
> select * from pg_indexes where tablename = 'test';
>
> tablename|indexname|indexdef
> ---------+---------+-----------------------------------------------------------------------
>
> test     |test_pkey|CREATE UNIQUE INDEX "test_pkey" ON "test" USING
> btree ("id" "int4_ops")
> (1 row)
>
>
>
I read about the pg_indexes table and stuff but I am interested in the
"columnname" of the primary key in the specified table.
Isn´t this feature very important?
Cheers,
Marc
--
-----------------------------------------------------------
Marc Grimme - An der Muehle 1 - 85716 Unterschleissheim
Fon: +49 89 37 48 81 22 - +49 89 37 48 92 7 - 3/0 -
mail2: grimme@informatik.tu-muenchen.de, grimme@atix.de
-----------------------------------------------------------
The UNIX Guru's View of Sex:
# unzip ; strip ; touch ; finger ; mount ; fsck ; more ; yes ; umount ;
sleep