Thread: Primary Key

Primary Key

From
Eduardo V. Rodríguez
Date:
Hi

I try to do the following in a table that has 4 columns,

id - month - day- time

The column id is the primary key, for example

id - month . day - time
1    Jan          22   16:15
2    Jan       22   16:16
3    Jan       22   16:17
4    Jan       22   16:18

But I pretend that automatically when I made an INSERT INTO table (month,
day, time) values (A,B,C) the primary key update his value increasing by one


I dont know how to do this, Iam using Postgres 7.4 with PgAdminIII

Thank you

Re: Primary Key

From
Mike G
Date:
If you want to use a primary key that increase in value by one each time consider using a sequence.

See http://www.postgresql.org/docs/7.4/static/sql-createsequence.html

On Mon, Jun 14, 2004 at 07:17:37PM -0500, Eduardo V. Rodríguez wrote:
> Hi
>
> I try to do the following in a table that has 4 columns,
>
> id - month - day- time
>
> The column id is the primary key, for example
>
> id - month . day - time
> 1    Jan          22   16:15
> 2    Jan       22   16:16
> 3    Jan       22   16:17
> 4    Jan       22   16:18
>
> But I pretend that automatically when I made an INSERT INTO table (month,
> day, time) values (A,B,C) the primary key update his value increasing by one
>
>
> I dont know how to do this, Iam using Postgres 7.4 with PgAdminIII
>
> Thank you
>
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend

Re: Primary Key

From
Nabil Sayegh
Date:
Eduardo V. Rodríguez wrote:

> id - month . day - time
> 1    Jan          22   16:15
> 2    Jan       22   16:16
> 3    Jan       22   16:17
> 4    Jan       22   16:18
>
> But I pretend that automatically when I made an INSERT INTO table (month,
> day, time) values (A,B,C) the primary key update his value increasing by one

CREATE TABLE foo (id_foo SERIAL PRIMARY KEY, month ...);

SERIAL is of type int creates automatically a sequence that increments each time you don't specify
the id in an INSERT.

HTH
--
  e-Trolley Sayegh & John, Nabil Sayegh
  Tel.: 0700 etrolley /// 0700 38765539
  Fax.: +49 69 8299381-8
  PGP : http://www.e-trolley.de

Re: Primary Key

From
Bruno Wolff III
Date:
On Mon, Jun 14, 2004 at 19:17:37 -0500,
  "Eduardo V. Rodríguez" <evazquez@insys-corp.com.mx> wrote:
>
> But I pretend that automatically when I made an INSERT INTO table (month,
> day, time) values (A,B,C) the primary key update his value increasing by one
>
>
> I dont know how to do this, Iam using Postgres 7.4 with PgAdminIII

Depending on what you really want sequences might be the solution.
Sequences really only provide you with unique values. If you really need
the values not have any gaps in the numbers then you will need to do
more work (especially if you will be deleting rows).

Re: Primary Key

From
Eduardo V. Rodríguez
Date:
Ok thanks for all the ideas I got it

-----Original Message-----
From: Bruno Wolff III [mailto:bruno@wolff.to]
Sent: Wednesday, June 16, 2004 1:13 PM
To: Eduardo V. Rodríguez
Cc: Postgres (E-mail)
Subject: Re: [NOVICE] Primary Key


On Mon, Jun 14, 2004 at 19:17:37 -0500,
  "Eduardo V. Rodríguez" <evazquez@insys-corp.com.mx> wrote:
>
> But I pretend that automatically when I made an INSERT INTO table (month,
> day, time) values (A,B,C) the primary key update his value increasing by
one
>
>
> I dont know how to do this, Iam using Postgres 7.4 with PgAdminIII

Depending on what you really want sequences might be the solution.
Sequences really only provide you with unique values. If you really need
the values not have any gaps in the numbers then you will need to do
more work (especially if you will be deleting rows).