Re: indexing

From: Jeff Duffy <jduffy(at)greatbridge(dot)com>
To: si <s(at)remail(dot)net>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: indexing
Date: 2001-02-27 15:00:03
Message-ID: Pine.LNX.4.21.0102270953420.19358-100000@cairhien.alanne.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Mon, 26 Feb 2001, si wrote:

> Hi,
>
> I am new to postgresql, Please can you guide me:
>
> I have created a table and I would like all the entries to be indexed with a number when the data is entered into the database, hence giving it a new id, every time some data is entered.
>
> e.g.
>
> I.D. | Name
> 1 | Jack
> 2 | Jill
>
>
> Does pgsql do this automatically? or do I have to create a seprate ID field like above, and check the database each time the data is entered and find out the highest number then increment it by 1 and then enter the data with the incremented number.

Well, it's not automatic, per se. What you want is to create an id
field, and use a sequence for that id field. If you use the SERIAL data
type for the id column when you create the table, the sequence will be created for you.

When you insert data, either insert nothing into the id field ( like
INSERT INTO table(name) VALUES ('Jack'), which will cause the next
sequence value to be used. Otherwise, use the function
nextval('some_table_seq') where "some_table_seq" is the name of your id
sequence during insert (use \ds to view your sequences).

This is not automatically indexed unless you make it a primary key
during creation. See the PostgreSQL docs for more info.

Jeff

--
Errors have occurred.
We won't tell you where or why.
Lazy programmers.
-- Hacking haiku

In response to

  • indexing at 2001-02-26 15:04:50 from si

Browse pgsql-admin by date

  From Date Subject
Next Message Oliver Elphick 2001-02-27 15:38:15 Re: COPY doesn't works when containing ' ' or ' ' characters on db
Previous Message Jaume Teixi 2001-02-27 09:19:12 Re: SOLVED: COPY doesn't works when containing ' ' or ' ' characters on db