Re: create table and data types

From: Ken Hill <ken(at)scottshill(dot)com>
To: Maciej Piekielniak <piechcio(at)isb(dot)com(dot)pl>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: create table and data types
Date: 2006-02-14 22:42:02
Message-ID: 1139956922.3083.79.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, 2006-02-14 at 23:03 +0100, Maciej Piekielniak wrote:

> Hello Ken,
>
> Tuesday, February 14, 2006, 10:30:34 PM, you wrote:
> KH> On Tue, 2006-02-14 at 22:12 +0100, Maciej Piekielniak wrote:
>
> >> Hello pgsql-sql,
> >>
> >> Is anybody know how create field in a new table with data type accuiring from a field in other table?
> >> For example:
> >>
> >> create table new_table
> >> (
> >> name other_table.name%TYPE
> >> );
> >>
>
> KH> Have you tried inheritance from one table to the new table?
>
> KH> CREATE TABLE new_table (new_column)
> KH> INHERITS (old_table)
>
> KH> All columns in 'old_table' will be inclueded in 'new_table' plus the
> KH> column 'new_column'.
>
> yes, but i don't need all colums, i need only the same data type for only
> some fields.
>
> for example
> create table new table
> (
> name other_table.name%TYPE,
> mynewfield VARCHAR(100),
> mynewfield2 VARCHAR(100)
> );
>

Have you tried restructuring the table with CREATE TABLE AS...? Try
this:

CREATE TABLE new_table
(id, mynewfield, mynewfield2)
AS SELECT id FROM old_table);

This should create a new table ('new_table') with the data-type for
'old_table' for the id column.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bryce Nesbitt 2006-02-14 23:05:10 Re: Non Matching Records in Two Tables
Previous Message Tom Lane 2006-02-14 22:06:34 Re: create table and data types