Re: Stupid question on INDEXES

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Giorgio Ponza" <giorgio(at)opla(dot)it>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Stupid question on INDEXES
Date: 2001-12-19 15:11:51
Message-ID: 29521.1008774711@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice pgsql-sql

"Giorgio Ponza" <giorgio(at)opla(dot)it> writes:
> I'd like to know if i think in the right way. With
> CREATE TABLE Test1 (
> my_id int,
> descr text,
> primary key (my_id))
> Postgres automatically creates an index named test1_pkey.
> If i use
> CREATE TABLE Test1 (
> my_id int,
> descr text)
> without specifying a pkey, i can write
> create unique index i_test1_pkey on (my_id).
> The only difference is that in the 2nd case i can use the name i want, or
> the usage is different?

There are two things that are different:

1. PRIMARY KEY implies NOT NULL. You'd need to declare the column as
NOT NULL in the second CREATE TABLE to have exactly equivalent
constraints.

2. When a PRIMARY KEY is declared, the column(s) become the default
columns for a FOREIGN KEY declaration in another table, so you could
write "REFERENCES Test1" without having to mention my_id as the
referenced column. Without a PRIMARY KEY, you'll have to mention my_id
explicitly. But this is only a notational convenience.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2001-12-19 15:14:12 Re: Query hangs when getting too complex...
Previous Message Troy.Campano 2001-12-19 15:06:06 Outputting select into file.

Browse pgsql-novice by date

  From Date Subject
Next Message Josh Berkus 2001-12-19 17:16:45 Re: Storing number '001' ?
Previous Message Giorgio Ponza 2001-12-19 13:03:50 Stupid question on INDEXES

Browse pgsql-sql by date

  From Date Subject
Next Message Louis-David Mitterrand 2001-12-19 16:11:28 return a text agreggate from a subselect
Previous Message Giorgio Ponza 2001-12-19 13:03:50 Stupid question on INDEXES