Return-Path: X-Original-To: apmail-db-derby-user-archive@www.apache.org Delivered-To: apmail-db-derby-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 51BB998D6 for ; Thu, 13 Oct 2011 16:24:38 +0000 (UTC) Received: (qmail 31674 invoked by uid 500); 13 Oct 2011 16:24:38 -0000 Delivered-To: apmail-db-derby-user-archive@db.apache.org Received: (qmail 31625 invoked by uid 500); 13 Oct 2011 16:24:37 -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 31618 invoked by uid 99); 13 Oct 2011 16:24:37 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Oct 2011 16:24:37 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of john.foreign@gmail.com designates 209.85.215.172 as permitted sender) Received: from [209.85.215.172] (HELO mail-ey0-f172.google.com) (209.85.215.172) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Oct 2011 16:24:31 +0000 Received: by eyg24 with SMTP id 24so202728eyg.31 for ; Thu, 13 Oct 2011 09:24:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=+pXuXf7leIfldkazrDFzSqhC8c0AktYDtt2Zi5eXn7g=; b=SmPS+UccKxtp3c0Yiz3ZF3sRUNt2oK8PIo8uCxXmlkcL3bI8diBslaed1VLB+kBl7n QOZr82O4S4zMvlPODhKa4o1vU65ELyo/OQGPbbuzuPzO/tCE9Gc13E/enORIuYQvsM2v TnZ2Fr6r5758lpJoZ1OhoY+b72Ry4sewuUOeM= Received: by 10.223.65.76 with SMTP id h12mr7080101fai.7.1318523049650; Thu, 13 Oct 2011 09:24:09 -0700 (PDT) Received: from [192.168.1.101] (bzq-84-110-213-142.red.bezeqint.net. [84.110.213.142]) by mx.google.com with ESMTPS id x22sm582561faa.5.2011.10.13.09.24.07 (version=SSLv3 cipher=OTHER); Thu, 13 Oct 2011 09:24:08 -0700 (PDT) Message-ID: <4E9710A2.8080905@gmail.com> Date: Thu, 13 Oct 2011 18:24:02 +0200 From: John English User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 To: Derby Discussion Subject: Re: Making a table with max 1 row References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit On 10/10/2011 22:05, AmFreak@web.de wrote: > > Hi, > > i need a table with 1 or 0 rows in it. So that after 1 row is inserted > the table can only be updated or the row deleted, but there can never be > a row inserted. > I came as far as this: > > CREATE TABLE test > (field1 INTEGER NOT NULL > CHECK (field1 = 1) UNIQUE) > > but that would (obviously) mean that field1 could only have the value 1. > > I would need something like check(COUNT(*) FROM test <= 1). > > Thanks for any suggestions / answers Use a trigger: CREATE TRIGGER remove_test AFTER INSERT ON test REFERENCING NEW AS ins FOR EACH ROW MODE DB2SQL DELETE FROM test WHERE field1=ins.field1 AND (SELECT COUNT(*) FROM test) > 1; This will delete the inserted row if there is more than one row in the table. HTH, ------------------------------------------------------------------------ John English | My old University of Brighton home page is still here: | http://www.cem.brighton.ac.uk/staff/je/ ------------------------------------------------------------------------