From: Jerry Sievers <jerry@jerrysievers.com>
To: Ben K. <bkim@coe.tamu.edu>
Cc: pgsql-admin@postgresql.org
Subject: Re: How to enforce the use of the sequence for serial columns
Date: 14 Dec 2006 07:30:46 -0500
Message-ID: <m31wn2k6bt.fsf@homie.jerrysievers.com> (raw)
In-Reply-To: <Pine.GSO.4.64.0612140543270.20249@coe.tamu.edu>
References: <Pine.GSO.4.64.0612140543270.20249@coe.tamu.edu>
"Ben K." <bkim@coe.tamu.edu> writes:
> > create table mytable (myid serial primary key constraint
> mytable_myid_chk check (myid = currval('mytable_myid_seq'), mydata
> varchar(255), ...);
>
>
> I'd like to clarify that this will not be a full solution, since it
> will not allow update of the table unless nextval has been used in the
> same sequence already.
It's not a full solution anyway since it prevents any kind of update
on the table due to check constraints firing even if target field not
updated. See below;
jerry@jerry#
= create table foo (a serial check (a = currval('foo_a_seq')), b int);
NOTICE: CREATE TABLE will create implicit sequence "foo_a_seq" for serial column "foo.a"
CREATE TABLE
jerry@jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry@jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry@jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry@jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry@jerry#
= select * from foo;
a | b
---+---
1 | 1
2 | 1
3 | 1
4 | 1
(4 rows)
jerry@jerry#
= update foo set b=2;
ERROR: new row for relation "foo" violates check constraint "foo_a_check"
jerry@jerry#
=
>
> There seems to be a hack in case of oracle that allows using currval
> without nextval, but it's a hack and I don't know if there's an
> equivalent in postgresql
> (http://rootshell.be/~yong321/computer/sequence.txt). (Oracle seems to
> have "disable" option when adding check constraint by alter table, but
> the context seem a bit different from ours so may not be useful in our
> case, at any rate.)
>
>
>
>
> Regards
>
> Ben K.
> Developer
> http://benix.tamu.edu
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: Don't 'kill -9' the postmaster
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
>
--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) Production Database Administrator
305 321-1144 (mobil WWW E-Commerce Consultant
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-admin@postgresql.org
Cc: jerry@jerrysievers.com, bkim@coe.tamu.edu
Subject: Re: How to enforce the use of the sequence for serial columns
In-Reply-To: <m31wn2k6bt.fsf@homie.jerrysievers.com>
References: <Pine.GSO.4.64.0612140543270.20249@coe.tamu.edu> <m31wn2k6bt.fsf@homie.jerrysievers.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox