Thread: Primary key

Primary key

From
"Peter Evens"
Date:

hello,

i have a question about the PRIMARY KEY,

how can we let it start from for example 1000 instead of 1?

This is our program:

CREATE TABLE hy3_pack

(

hy3_id serial NOT NULL,

hy3_serie_nummer text NOT NULL,

hy3_barcode text NOT NULL,

hy3_type_vulling text NOT NULL,

hy3_tarra_gewicht text,

hy3_artikel_id numeric NOT NULL,

hy3_refill boolean,

vernietigd boolean,

opmerking text,

CONSTRAINT hy3_pack_pkey PRIMARY KEY (hy3_id)

)

WITH (

OIDS=FALSE

);

ALTER TABLE hy3_pack OWNER TO postgres;

what must i change in my program?

Met vriendelijke groeten,
Peter Evens
BANDIT nv/sa
Nijverheidslaan 1547
B-3660 Opglabbeek
België
Tel. 0032/89/85.85.65
Fax 0032/89/51.85.47
peter@bandit.be

Re: Primary key

From
Allan Kamau
Date:
On Mon, Mar 14, 2011 at 10:58 AM, Peter Evens <peter@bandit.be> wrote:
> hello,
>
> i have a question about the PRIMARY KEY,
>
> how can we let it start from for example 1000 instead of 1?
>
> This is our program:
>
> CREATE TABLE hy3_pack
>
> (
>
> hy3_id serial NOT NULL,
>
> hy3_serie_nummer text NOT NULL,
>
> hy3_barcode text NOT NULL,
>
> hy3_type_vulling text NOT NULL,
>
> hy3_tarra_gewicht text,
>
> hy3_artikel_id numeric NOT NULL,
>
> hy3_refill boolean,
>
> vernietigd boolean,
>
> opmerking text,
>
> CONSTRAINT hy3_pack_pkey PRIMARY KEY (hy3_id)
>
> )
>
> WITH (
>
> OIDS=FALSE
>
> );
>
> ALTER TABLE hy3_pack OWNER TO postgres;
>
> what must i change in my program?
>
> Met vriendelijke groeten,
> Peter Evens
> BANDIT nv/sa
> Nijverheidslaan 1547
> B-3660 Opglabbeek
> België
> Tel. 0032/89/85.85.65
> Fax 0032/89/51.85.47
> peter@bandit.be

Not sure about SERIAL. In postgreSQL we have sequence database object
("http://www.postgresql.org/docs/9.0/interactive/sql-createsequence.html").

Perhaps the DDL SQL statements below may help.

CREATE SEQUENCE hy3_pack_seq MINVALUE 1000;
CREATE TABLE hy3_pack
(
hy3_id INTEGER NOT NULL DEFAULT nextval('hy3_pack_seq') -- or hy3_id
BIGINT NOT NULL DEFAULT nextval('hy3_pack_seq')
,hy3_serie_nummer text NOT NULL
,hy3_barcode text NOT NULL
,hy3_type_vulling text NOT NULL
,hy3_tarra_gewicht text
,hy3_artikel_id numeric NOT NULL
,hy3_refill boolean
,vernietigd boolean
,opmerking text
,CONSTRAINT hy3_pack_pkey PRIMARY KEY(hy3_id)
)
;


Allan.

Re: Primary key

From
Raymond O'Donnell
Date:
On 14/03/2011 15:35, Allan Kamau wrote:

> CREATE SEQUENCE hy3_pack_seq MINVALUE 1000;
> CREATE TABLE hy3_pack
> (
> hy3_id INTEGER NOT NULL DEFAULT nextval('hy3_pack_seq') -- or hy3_id
> BIGINT NOT NULL DEFAULT nextval('hy3_pack_seq')

That's what SERIAL does for you, in one go - it's just syntactic sugar
for the above. Read all about it here:

http://www.postgresql.org/docs/9.0/static/datatype-numeric.html#DATATYPE-SERIAL


Ray.


--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie

Re: Primary key

From
Raymond O'Donnell
Date:
On 14/03/2011 15:56, Raymond O'Donnell wrote:
> On 14/03/2011 15:35, Allan Kamau wrote:
>
>> CREATE SEQUENCE hy3_pack_seq MINVALUE 1000;
>> CREATE TABLE hy3_pack
>> (
>> hy3_id INTEGER NOT NULL DEFAULT nextval('hy3_pack_seq') -- or hy3_id
>> BIGINT NOT NULL DEFAULT nextval('hy3_pack_seq')
>
> That's what SERIAL does for you, in one go - it's just syntactic sugar
> for the above.

....with the exception of the MINVALUE bit, of course... :-)

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie

Re: Primary key

From
"Joshua D. Drake"
Date:
On Mon, 2011-03-14 at 16:03 +0000, Raymond O'Donnell wrote:
> On 14/03/2011 15:56, Raymond O'Donnell wrote:
> > On 14/03/2011 15:35, Allan Kamau wrote:
> >
> >> CREATE SEQUENCE hy3_pack_seq MINVALUE 1000;
> >> CREATE TABLE hy3_pack
> >> (
> >> hy3_id INTEGER NOT NULL DEFAULT nextval('hy3_pack_seq') -- or hy3_id
> >> BIGINT NOT NULL DEFAULT nextval('hy3_pack_seq')
> >
> > That's what SERIAL does for you, in one go - it's just syntactic sugar
> > for the above.
>
> ....with the exception of the MINVALUE bit, of course... :-)

You can update the dependent sequence that serial creates though.

JD

>
> Ray.
>
> --
> Raymond O'Donnell :: Galway :: Ireland
> rod@iol.ie
>

--
PostgreSQL.org Major Contributor
Command Prompt, Inc: http://www.commandprompt.com/ - 509.416.6579
Consulting, Training, Support, Custom Development, Engineering
http://twitter.com/cmdpromptinc | http://identi.ca/commandprompt