From: Jaime Casanova <systemguards@gmail.com>
To: Announce <truthhurts@insightbb.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: DEFAULT Constraint based on table type?
Date: Mon, 28 Nov 2005 17:14:15 -0500
Message-ID: <c2d9e70e0511281414m5b8ed807t11ad7d85ffeeba4@mail.gmail.com> (raw)
In-Reply-To: <KBEKKNMFLELKGIADDEPEMEBPCDAA.truthhurts@insightbb.com>
References: <ECOWS01MpB9pbKWZfEA0000693e@smtp-out1.blueyonder.co.uk>
<KBEKKNMFLELKGIADDEPEMEBPCDAA.truthhurts@insightbb.com>
On 11/28/05, Announce <truthhurts@insightbb.com> 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 populate
> 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 TYPE
> field to a default value on insert but I wanted a more simple solution like
> 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 ;)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: systemguards@gmail.com, truthhurts@insightbb.com
Subject: Re: DEFAULT Constraint based on table type?
In-Reply-To: <c2d9e70e0511281414m5b8ed807t11ad7d85ffeeba4@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox