X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id A409ED1B8A1 for ; Sat, 20 Sep 2003 11:56:25 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 31439-09 for ; Sat, 20 Sep 2003 08:55:56 -0300 (ADT) Received: from anchor-post-34.mail.demon.net (anchor-post-34.mail.demon.net [194.217.242.92]) by svr1.postgresql.org (Postfix) with ESMTP id 74040D1B58E for ; Sat, 20 Sep 2003 08:55:53 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-34.mail.demon.net with esmtp (Exim 3.35 #1) id 1A0gLE-000L64-0Y; Sat, 20 Sep 2003 12:55:56 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 398DD167E0; Sat, 20 Sep 2003 12:55:55 +0100 (BST) Received: from client17.archonet.com (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 8F98A167D8; Sat, 20 Sep 2003 12:55:54 +0100 (BST) From: Richard Huxton To: "Muhyiddin A.M Hayat" , Subject: Re: auto_increment Date: Sat, 20 Sep 2003 12:55:53 +0100 User-Agent: KMail/1.5 References: <001d01c37f3e$983ed3a0$2363a8c0@ckeskin> <200309201105.30370.dev@archonet.com> <007901c37f60$058de880$1f00a8c0@middinkcomp> In-Reply-To: <007901c37f60$058de880$1f00a8c0@middinkcomp> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200309201255.53950.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/223 X-Sequence-Number: 15201 On Saturday 20 September 2003 11:14, Muhyiddin A.M Hayat wrote: > Where/How can i put this below sql statement, to set value of > guest_guest_id_seq before i do insert to table > SELECT setval('guest_guest_id_seq', max(guest.guest_id)) FROM guest; > > i have been try > > CREATE TRIGGER "before_insert_guest_update_room_number" BEFORE INSERT > ON "public"."guest" FOR EACH ROW > EXECUTE PROCEDURE "public"."generate_guest_id"(); > > but error The whole idea of sequences is that you don't need to keep altering them. Usual usage would be something like: INSERT INTO guest (guest_id, room_number) VALUES (nextval('guest_guest_id_seq'), 123); Or, if you have defined guest_id as a SERIAL (which just sets DEFAULT to the nextval() call for you). INSERT INTO guest (guest_id, room_number) VALUES (DEFAULT, 123); or INSERT INTO guest (room_number) VALUES (123); So long as you always use the sequence, then guest_id will get a different number each time. If you already have some entries in guest, and create the sequence later, then before you start you'll want to call setval(), but you'll only need to do this once, to skip the numbers you have already used. Does that make it clearer? -- Richard Huxton Archonet Ltd