X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 6461A9DCBFB for ; Mon, 28 Nov 2005 18:14:17 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 61502-03 for ; Mon, 28 Nov 2005 18:14:17 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from xproxy.gmail.com (xproxy.gmail.com [66.249.82.206]) by postgresql.org (Postfix) with ESMTP id 810349DCBED for ; Mon, 28 Nov 2005 18:14:13 -0400 (AST) Received: by xproxy.gmail.com with SMTP id h27so2556865wxd for ; Mon, 28 Nov 2005 14:14:15 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=mjvnw0UMjPZAeCNq8RtPahh+14KO15QKgZgiGk9Gqi44HwA/ZIm/h6+/w7zcSfHm2WAUtF3UBGo/PpDePwxsPrFssRft9vOmuiFVedbFrzvP1CM3p5f4lQnj+BIE7TFesl4cliKQnqX/wcKx8Mbrs1AXZBoNztl4h2J33YcHYS4= Received: by 10.65.43.12 with SMTP id v12mr548485qbj; Mon, 28 Nov 2005 14:14:15 -0800 (PST) Received: by 10.65.180.14 with HTTP; Mon, 28 Nov 2005 14:14:15 -0800 (PST) Message-ID: Date: Mon, 28 Nov 2005 17:14:15 -0500 From: Jaime Casanova To: Announce Subject: Re: DEFAULT Constraint based on table type? Cc: pgsql-sql@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200511/263 X-Sequence-Number: 23514 On 11/28/05, Announce wrote: > Lets say I have the following tables. > > CREATE TABLE animals(id primary key, name varchar, type varchar); > CREATE TABLE dogs (breed varchar) INHERITS (animals); > CREATE TABLE birds (bool hasFeathers) INHERITS (animals); > > Is there a way I can specify a default on the child table that will popul= ate > the 'type' column? For example, if I am inserting a row in table DOGS, I > would always want the default value for column TYPE to be 'DOG'. If I am > inserting into BIRDS.... type 'BIRD'. > > I know that I could add individual triggers on each table that set the TY= PE > field to a default value on insert but I wanted a more simple solution li= ke > setting a DEFAULT table-constraint. > > Also, In java, this could be done on a parent object by overriding a > constructor or method, using the Class object or instanceof. Is there > anyway for a table to "know" it's "class" in this scenario? > > Thanks, > > -Aaron > CREATE TABLE animals(id primary key, name varchar, type varchar); CREATE TABLE dogs (breed varchar) INHERITS (animals); ALTER TABLE dogs ALTER COLUMN type SET DEFAULT 'DOG'; CREATE TABLE birds (bool hasFeathers) INHERITS (animals); ALTER TABLE birds ALTER COLUMN type SET DEFAULT 'BIRD'; -- regards, Jaime Casanova (DBA: DataBase Aniquilator ;)