Thread: create table and data types

create table and data types

From
Maciej Piekielniak
Date:
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
); 

-- 
Best regards,Maciej                          mailto:piechcio@isb.com.pl



Re: create table and data types

From
Ken Hill
Date:
On Tue, 2006-02-14 at 22:12 +0100, Maciej Piekielniak wrote: <blockquote type="CITE"><pre>
<font color="#000000">Hello pgsql-sql,</font>

<font color="#000000">  Is anybody know how create field in a new table with data type accuiring from a field in other
table?</font>
<font color="#000000">  For example:</font>

<font color="#000000">create table new_table</font>
<font color="#000000">( </font>
<font color="#000000"> name other_table.name%TYPE</font>
<font color="#000000">); </font>

</pre></blockquote> Have you tried inheritance from one table to the new table?<br /><br /> CREATE TABLE new_table
(new_column)<br/>      INHERITS (old_table)<br /><br /> All columns in 'old_table' will be inclueded in 'new_table'
plusthe column 'new_column'.  

Re: create table and data types

From
Maciej Piekielniak
Date:
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)
);

-- 
Best regards,Maciej



Re: create table and data types

From
Tom Lane
Date:
Maciej Piekielniak <piechcio@isb.com.pl> writes:
>   Is anybody know how create field in a new table with data type accuiring from a field in other table?

Sorry, the %TYPE syntax only works in function declarations at the
moment.

It could possibly be made to work in table declarations --- there are
syntactic conflicts with allowing it in general, but I'm not sure that
objection applies to table declarations.  But it's not there today.
        regards, tom lane


Re: create table and data types

From
Ken Hill
Date:
On Tue, 2006-02-14 at 23:03 +0100, Maciej Piekielniak wrote: <blockquote type="CITE"><pre>
<font color="#000000">Hello Ken,</font>

<font color="#000000">Tuesday, February 14, 2006, 10:30:34 PM, you wrote:</font>
<font color="#000000">KH> On Tue, 2006-02-14 at 22:12 +0100, Maciej Piekielniak wrote:</font>

<font color="#000000">>> Hello pgsql-sql,</font>
<font color="#000000">>> </font>
<font color="#000000">>>   Is anybody know how create field in a new table with data type accuiring from a field
inother table?</font>
 
<font color="#000000">>>   For example:</font>
<font color="#000000">>> </font>
<font color="#000000">>> create table new_table</font>
<font color="#000000">>> ( </font>
<font color="#000000">>>  name other_table.name%TYPE</font>
<font color="#000000">>> ); </font>
<font color="#000000">>> </font>

<font color="#000000">KH> Have you tried inheritance from one table to the new table?</font>

<font color="#000000">KH> CREATE TABLE new_table (new_column)</font>
<font color="#000000">KH>      INHERITS (old_table)</font>

<font color="#000000">KH> All columns in 'old_table' will be inclueded in 'new_table' plus the</font>
<font color="#000000">KH> column 'new_column'.</font>

<font color="#000000">yes, but i don't need all colums, i need only the same data type for only</font>
<font color="#000000">some fields.</font>

<font color="#000000">for example</font>
<font color="#000000">create table new table</font>
<font color="#000000">(</font>
<font color="#000000"> name other_table.name%TYPE,</font>
<font color="#000000"> mynewfield VARCHAR(100),</font>
<font color="#000000"> mynewfield2 VARCHAR(100)</font>
<font color="#000000">);</font>

</pre></blockquote> Have you tried restructuring the table with CREATE TABLE AS...? Try this:<br /><br /> CREATE TABLE
new_table<br/>      (id, mynewfield, mynewfield2)<br />      AS SELECT id FROM old_table);<br /><br /> This should
createa new table ('new_table') with the data-type for 'old_table' for the id column.  

Re: create table and data types

From
"Bath, David"
Date:
On Tue, 2006-02-14 at 22:12 +0100, Maciej Piekielniak wrote: (snipped)
>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);

On Wed, 15 Feb 2006 09:42, Ken Hill wrote: (snipped)
> 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);

I use a similar technique, but add a "WHERE 1=0" so the new table is
empty.  Of course, more than a single table can be referenced in the
FROM clause.
--
David T. Bath
dave.bath@unix.net