From: | Jerry Sievers <jerry(at)jerrysievers(dot)com> |
---|---|
To: | "Ben K(dot)" <bkim(at)coe(dot)tamu(dot)edu> |
Cc: | pgsql-admin(at)postgresql(dot)org |
Subject: | Re: How to enforce the use of the sequence for serial columns |
Date: | 2006-12-14 12:30:46 |
Message-ID: | m31wn2k6bt.fsf@homie.jerrysievers.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-admin |
"Ben K." <bkim(at)coe(dot)tamu(dot)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(at)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(at)jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry(at)jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry(at)jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry(at)jerry#
= insert into foo values (default, 1);
INSERT 0 1
jerry(at)jerry#
= select * from foo;
a | b
---+---
1 | 1
2 | 1
3 | 1
4 | 1
(4 rows)
jerry(at)jerry#
= update foo set b=2;
ERROR: new row for relation "foo" violates check constraint "foo_a_check"
jerry(at)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
From | Date | Subject | |
---|---|---|---|
Next Message | Marc Mamin | 2006-12-14 12:45:45 | Re: How to enforce the use of the sequence for serial columns |
Previous Message | Ben K. | 2006-12-14 11:57:57 | Re: How to enforce the use of the sequence for serial columns |