Re: Rename or Re-Create Constraints?

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: Carlos Mennens <carlos(dot)mennens(at)gmail(dot)com>
Subject: Re: Rename or Re-Create Constraints?
Date: 2011-04-09 22:21:46
Message-ID: 201104091521.46725.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Saturday, April 09, 2011 2:59:06 pm Carlos Mennens wrote:
> On Sat, Apr 9, 2011 at 12:58 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > ALTER INDEX accounts_pkey RENAME TO whatever
> >
> > On very old versions of PG you may have to spell that "ALTER TABLE"
> > instead of "ALTER INDEX", but it's the same thing either way.
>
> Thank you so much for clearing that up for me Tom! I just couldn't
> find anything documented or do I understand SQL enough to work through
> that w/o an example.
>
> I read the PostgreSQL documentation all morning and just couldn't find
> it. Also to make sure I did this correct, if I had an existing table
> w/o a PRIMARY KEY index / constraint, is the following correct?

You want to create a PRIMARY KEY correct? If so starting from scratch:

test(5432)aklaver=>create table pk_test(id integer,fld_1 text);
CREATE TABLE

test(5432)aklaver=>\d pk_test
Table "public.pk_test"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
fld_1 | text |

test(5432)aklaver=>ALTER TABLE pk_test ADD CONSTRAINT pk PRIMARY KEY(id);
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "pk" for table
"pk_test"
ALTER TABLE

test(5432)aklaver=>\d pk_test
Table "public.pk_test"
Column | Type | Modifiers
--------+---------+-----------
id | integer | not null
fld_1 | text |
Indexes:
"pk" PRIMARY KEY, btree (id)

>
> CREATE UNIQUE INDEX users_pkey ON public.users (id);
> CREATE INDEX
>
> I'm guessing that's how I generate a index / constraint on an existing
> table when it was generated during the table creation SQL command,
> right?
>
> Is there a difference between an INDEX and a CONSTRAINT?

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2011-04-09 22:30:17 Re: Rename or Re-Create Constraints?
Previous Message Carlos Mennens 2011-04-09 21:59:06 Re: Rename or Re-Create Constraints?