Re: how to create a new composite type using already existing composite types

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: Iain Barnett <iainspeed(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: how to create a new composite type using already existing composite types
Date: 2010-02-15 14:24:00
Message-ID: b42b73151002150624p72cbc4a6wa6db69f2fd1007d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sat, Feb 13, 2010 at 7:24 PM, Jeff Davis <pgsql(at)j-davis(dot)com> wrote:
> On Wed, 2010-02-10 at 03:46 +0000, Iain Barnett wrote:
>
>> CREATE TYPE inventory_item2 AS (
>>     name            text,
>>     supplier_id     integer,
>>     price           numeric,
>> size  integer
>> );
>>
>>
>> but it would be handy if I could reuse inventory_item instead of
>> having to retype the whole lot. I can't work out or find the right
>> syntax, can anyone show me how? Any help would be much appreciated.
>
> What about:
>
>  CREATE TYPE inventory_item2 AS (
>    ii inventory_item,
>    size integer
>  );
>
> or:
>
>  CREATE TABLE inventory_item2 (
>    LIKE inventory_item,
>    size integer
>  );

Pretty neat. I wasn't aware you could mix LIKE with explicit field
declarations.

Those expressions produce different results...the first creates a type
within a type (nested) and the second creates a new unnested type
based on the fields of the original type. The first expression
creates a dependency while the second does not. This flexibility is
only possible if you create types with the 'create table' statement.

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Marcin Krol 2010-02-15 14:43:22 vacuum analyze GROWS db ?!
Previous Message dipti shah 2010-02-15 08:49:34 Re: Set the permissions while creating database, schema, and tables