Return-Path: Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: (qmail 1890 invoked from network); 29 Feb 2008 06:14:14 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 29 Feb 2008 06:14:14 -0000 Received: (qmail 34069 invoked by uid 500); 29 Feb 2008 06:14:08 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 34053 invoked by uid 500); 29 Feb 2008 06:14:08 -0000 Mailing-List: contact derby-user-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: List-Id: Reply-To: "Derby Discussion" Delivered-To: mailing list derby-user@db.apache.org Received: (qmail 34042 invoked by uid 99); 29 Feb 2008 06:14:08 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Feb 2008 22:14:08 -0800 X-ASF-Spam-Status: No, hits=-1.0 required=10.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [192.18.19.7] (HELO sineb-mail-2.sun.com) (192.18.19.7) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Feb 2008 06:13:32 +0000 Received: from fe-apac-06.sun.com (fe-apac-06.sun.com [192.18.19.177] (may be forged)) by sineb-mail-2.sun.com (8.13.6+Sun/8.12.9) with ESMTP id m1T6DpsP020369 for ; Fri, 29 Feb 2008 06:13:54 GMT Received: from conversion-daemon.mail-apac.sun.com by mail-apac.sun.com (Sun Java System Messaging Server 6.2-6.01 (built Apr 3 2006)) id <0JWZ00801L2GRJ00@mail-apac.sun.com> (original mail from V.Narayanan@Sun.COM) for derby-user@db.apache.org; Fri, 29 Feb 2008 14:13:37 +0800 (SGT) Received: from [129.158.228.35] by mail-apac.sun.com (Sun Java System Messaging Server 6.2-6.01 (built Apr 3 2006)) with ESMTPSA id <0JWZ00B54LALMHJ2@mail-apac.sun.com> for derby-user@db.apache.org; Fri, 29 Feb 2008 14:13:33 +0800 (SGT) Date: Fri, 29 Feb 2008 11:43:33 +0530 From: Narayanan Subject: Re: Version column generated by default ? In-reply-to: <6ac05c90802281516p12550221x358fc12d16b66996@mail.gmail.com> Sender: V.Narayanan@Sun.COM To: Derby Discussion Message-id: <47C7A28D.4060303@sun.com> MIME-version: 1.0 Content-type: text/plain; format=flowed; charset=ISO-8859-1 Content-transfer-encoding: 7BIT References: <6ac05c90802281516p12550221x358fc12d16b66996@mail.gmail.com> User-Agent: Thunderbird 2.0.0.6 (X11/20071022) X-Virus-Checked: Checked by ClamAV on apache.org bruehlicke wrote: > Hi, > > I would like a table something like > > ID BIGINT (standart generated Identity generated always) > NAME VARCHAR(40) > VERSION INT > > Now, I would like to be able to have the system automatically increase > the version number, but also allow to use my own and of course if > version 1, 2 and 3 are used and the user selects to delete version 3 > and re-create he will not get verision 4 bu a new version 3 > > QUESTION: Is there a standard "pattern" we use for this kind of > columns representing "versions" of a given thing ? Or is is good old > "my own algorithm" to maintain this baby ? > > B-) > Sorry about the garbled select outputs in the previous reply :-( I have tried to improve the pasted select outputs in this email I tried the following experiment with the jars of the latest trunk. If you notice after I insert a column with the identity column value as 5 the generated column value still continues from where it left the increment previously. ij> create table greetings(i int generated by default as identity, ch char(50)); 0 rows inserted/updated/deleted ij> insert into greetings values (DEFAULT, 'salut'); 1 row inserted/updated/deleted ij> insert into greetings(ch) values ('bonjour'); 1 row inserted/updated/deleted ij> insert into greetings values (5, 'me'); 1 row inserted/updated/deleted ij> insert into greetings values (DEFAULT,'you'); 1 row inserted/updated/deleted ij> select * from greetings; I |CH -------------------------------------------------------------- 1 |salut 2 |bonjour 5 |me 3 |you ij> insert into greetings values (DEFAULT, 'all'); 1 row inserted/updated/deleted ij> insert into greetings values (DEFAULT, 'all'); 1 row inserted/updated/deleted ij> insert into greetings values (DEFAULT, 'all'); 1 row inserted/updated/deleted ij> select * from greetings; I |CH -------------------------------------------------------------- 1 |salut 2 |bonjour 5 |me 3 |you 4 |all 5 |all 6 |all 7 rows selected Also I experimented with deleting a row ij> delete from greetings where i=6; 1 row inserted/updated/deleted ij> insert into greetings values (DEFAULT, 'all'); 1 row inserted/updated/deleted ij> select * from greetings; I |CH -------------------------------------------------------------- 1 |salut 2 |bonjour 5 |me 3 |you 4 |all 5 |all 7 |all 7 rows selected So the value continues from 7 and not from 6 as you expect. The document here http://db.apache.org/derby/docs/10.2/ref/rrefsqlj37836.html that generated by always does not guarantee uniqueness. You could use a primary key or unique constraint on a column to guarantee uniqueness. I found http://db.apache.org/derby/docs/10.2/ref/rrefsqlj37836.html very informative. I am not sure if a newer version of the document exists. I guess you could refer that for more information. Narayananan