Thread: how to add primary key to existing table with multiple primary keys

how to add primary key to existing table with multiple primary keys

From
"Daniel Joo"
Date:
<div class="Section1"><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Hi there,</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">I am trying to add another primary key to an existing table with two other primary keys.  I got the
followingerror when I tried this command:</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">alter table extprobe2tissue ADD primary key (expid);</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial">ERROR:  multiple primary keys for table "extprobe2tissue" are not allowed</span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">The details of the extprobe2tissue table is: </span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">\d extprobe2tissue;</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial">          Table "public.extprobe2tissue"</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial">   Column    |          Type          | Modifiers</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial">-------------+------------------------+-----------</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial"> probeid     | integer                | not null</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial"> tissueid    | integer                | not null</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial"> value       | double precision       | not null</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial"> entrydate   | date                   | not null</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial"> description | character varying(200) |</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial"> qval        | double precision       |</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial"> expid       | integer                | not null</span></font><p class="MsoNormal"><font
face="Arial"size="2"><span style="font-size:10.0pt; 
font-family:Arial">Indexes:</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial">    "extprobe2tissue_pkey" PRIMARY KEY, btree (probeid, tissueid)</span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial">    "extprobe2tissue_probeid" btree (probeid)</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial">    "extprobe2tissue_tissueid" btree (tissueid)</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial">Foreign-key constraints:</span></font><p class="MsoNormal"><font face="Arial" size="2"><span
style="font-size:10.0pt;
font-family:Arial">    "extprobe2tissue_tissueid_fkey" FOREIGN KEY (tissueid) REFERENCES
tissue(tissueid)</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial">    "extprobe2tissue_probeid_fkey" FOREIGN KEY (probeid) REFERENCES
extprobe(probeid)</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Any suggestion would be much appreciated.</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanstyle="font-size:10.0pt; 
font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span style="font-size:10.0pt;
font-family:Arial">Thanks!  </span></font></div>

Re: how to add primary key to existing table with multiple

From
Stephan Szabo
Date:
On Thu, 23 Feb 2006, Daniel Joo wrote:

> I am trying to add another primary key to an existing table with two
> other primary keys.  I got the following error when I tried this
> command:

You only can have one primary key.  The table you gave has a single
primary key with two columns.  Are you trying to guarantee that expid
along is unique or that the set (probeid, tissueid, expid) is unique?

You can make expid UNIQUE and NOT NULL for the first case which is similar
to a primary key (although it's not a default foreign key target).  In the
second you can drop the current constraint and add one on the three
columns.

> Indexes:
>
>     "extprobe2tissue_pkey" PRIMARY KEY, btree (probeid, tissueid)
>     "extprobe2tissue_probeid" btree (probeid)
>     "extprobe2tissue_tissueid" btree (tissueid)


Re: how to add primary key to existing table with multiple primary keys

From
"Mauricio Fernandez A."
Date:
Your table has a primary key yet, so you can´t add a second primary key.  You only can define one pk, however it can be composed as your table has (it now has 2 columns), so I think you need to drop the current pk and add again the new pk with your 3 columns (probeid, tissueid, expid).
 
 

Mauricio Fernández A.
Ingeniero de Sistemas
Universidad Autónoma de Manizales
(Colombia)

-----Mensaje original-----
De: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]En nombre de Daniel Joo
Enviado el: viernes, 24 febrero, 2006 2:45
Para: pgsql-sql@postgresql.org
Asunto: [SQL] how to add primary key to existing table with multiple primary keys

Hi there,

 

I am trying to add another primary key to an existing table with two other primary keys.  I got the following error when I tried this command:

 

alter table extprobe2tissue ADD primary key (expid);

ERROR:  multiple primary keys for table "extprobe2tissue" are not allowed

 

The details of the extprobe2tissue table is:

 

\d extprobe2tissue;

          Table "public.extprobe2tissue"

   Column    |          Type          | Modifiers

-------------+------------------------+-----------

 probeid     | integer                | not null

 tissueid    | integer                | not null

 value       | double precision       | not null

 entrydate   | date                   | not null

 description | character varying(200) |

 qval        | double precision       |

 expid       | integer                | not null

Indexes:

    "extprobe2tissue_pkey" PRIMARY KEY, btree (probeid, tissueid)

    "extprobe2tissue_probeid" btree (probeid)

    "extprobe2tissue_tissueid" btree (tissueid)

Foreign-key constraints:

    "extprobe2tissue_tissueid_fkey" FOREIGN KEY (tissueid) REFERENCES tissue(tissueid)

    "extprobe2tissue_probeid_fkey" FOREIGN KEY (probeid) REFERENCES extprobe(probeid)

 

Any suggestion would be much appreciated.

 

Thanks!