Thread: Howto define a constraint in a existing column

Howto define a constraint in a existing column

From
Intengu Technologies
Date:
I am tying to define a primary key constraint to an existing table,
this is what I have tried so far

ALTER TABLE mytable ALTER COLUMN field13 CONSTRAINT name PRIMARY KEY;

I get the following error: ERROR: syntax error at or near "CONSTRAINT"
SQL state: 42601
Character: 61

--
Sindile Bidla

Re: Howto define a constraint in a existing column

From
Thomas Kellerer
Date:
Intengu Technologies wrote on 09.08.2009 11:23:
> I am tying to define a primary key constraint to an existing table,
> this is what I have tried so far
>
> ALTER TABLE mytable ALTER COLUMN field13 CONSTRAINT name PRIMARY KEY;
>
> I get the following error: ERROR: syntax error at or near "CONSTRAINT"
> SQL state: 42601
> Character: 61
>
That should be

ALTER TABLE mytable
    ADD PRIMARY KEY (field13);

Or if you want to give the constraint a name:

ALTER TABLE mytable
    ADD CONSTRAINT pk_mytable PRIMARY KEY (field13);

Thomas

Re: Howto define a constraint in a existing column

From
Intengu Technologies
Date:
Thanks worked like a charm

On 09/08/2009, Thomas Kellerer <spam_eater@gmx.net> wrote:
> Intengu Technologies wrote on 09.08.2009 11:23:
>> I am tying to define a primary key constraint to an existing table,
>> this is what I have tried so far
>>
>> ALTER TABLE mytable ALTER COLUMN field13 CONSTRAINT name PRIMARY KEY;
>>
>> I get the following error: ERROR: syntax error at or near "CONSTRAINT"
>> SQL state: 42601
>> Character: 61
>>
> That should be
>
> ALTER TABLE mytable
>     ADD PRIMARY KEY (field13);
>
> Or if you want to give the constraint a name:
>
> ALTER TABLE mytable
>     ADD CONSTRAINT pk_mytable PRIMARY KEY (field13);
>
> Thomas
>
>
> --
> Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-novice
>


--
Sindile Bidla