Обсуждение: insert into help

Поиск
Список
Период
Сортировка

insert into help

От
Nicholas I
Дата:
Hi, <br /><br /> i have two tables, <br />---------------------------------------------------------------<br
/><b>table1<br/><br />id type serial, name varchar;</b><br
/><b>--------------------------------------------------------------<br/> table 2<br /><br />name varchar;</b><br
/>---------------------------------------------------------------<br/><br />i want to insert the values of table 2 into
table1, with automatic id's.<br /><br />insert into table1(select * from table2);<br /><br />is not working, how can i
appendthe data to table 1 with auto incremented or nextval.<br /><br />-Nicholas I<br /> 

Re: insert into help

От
Guillaume Lelarge
Дата:
Le 22/09/2010 09:32, Nicholas I a écrit :
> Hi,
> 
>  i have two tables,
> ---------------------------------------------------------------
> *table1
> 
> id type serial, name varchar;*
> *--------------------------------------------------------------
> table 2
> 
> name varchar;*
> ---------------------------------------------------------------
> 
> i want to insert the values of table 2 into table 1, with automatic id's.
> 
> insert into table1(select * from table2);
> 
> is not working, how can i append the data to table 1 with auto incremented
> or nextval.
> 

INSERT INTO table1 (name) SELECT name FROM table2;


-- 
Guillaumehttp://www.postgresql.frhttp://dalibo.com


Re: insert into help

От
venkat
Дата:
HI,
  

 Please go through 

Thanks and Regards,

Venkat

On Wed, Sep 22, 2010 at 1:02 PM, Nicholas I <nicholas.domnic.i@gmail.com> wrote:
Hi,

 i have two tables,
---------------------------------------------------------------
table1

id type serial, name varchar;

--------------------------------------------------------------
table 2

name varchar;

---------------------------------------------------------------

i want to insert the values of table 2 into table 1, with automatic id's.

insert into table1(select * from table2);

is not working, how can i append the data to table 1 with auto incremented or nextval.

-Nicholas I

Re: insert into help

От
James Kitambara
Дата:

Hello Guillaume Lelarge !

I suggest you try the following question:

RE-CREATE YOUR TABLES AS FOLLOW:



CREATE SEQUENCE table1_id_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 10000000
  START 1
  CACHE 1;

CREATE TABLE TABLE1 (
    ID INTEGER NOT NULL DEFAULT nextval('table1_id_seq'::regclass)
      , NAME VARCHAR(200) NOT NULL
);



CREATE TABLE TABLE2 (
        NAME VARCHAR(200) NOT NULL
);



------------------------INSERTING THE DATA------------------------------

INSERT INTO TABLE1 (NAME) SELECT NAME FROM TABLE2;

Note:  The ID in Table1 will be generated automaticale because of
       DEFAULT nextval('table1_id_seq'::regclass)


 

James Kitambara
Computer System Analyst and Programmer
Bank of Tanzania,
P.O. Box 2939,
Mobile : +255 71 3307632,
Dar es Salaam,
Tanzania.
 


--- On Wed, 22/9/10, Guillaume Lelarge <guillaume@lelarge.info> wrote:

From: Guillaume Lelarge <guillaume@lelarge.info>
Subject: Re: [SQL] insert into help
To: "Nicholas I" <nicholas.domnic.i@gmail.com>
Cc: pgsql-sql@postgresql.org
Date: Wednesday, 22 September, 2010, 8:35

Le 22/09/2010 09:32, Nicholas I a écrit :
> Hi,
>
>  i have two tables,
> ---------------------------------------------------------------
> *table1
>
> id type serial, name varchar;*
> *--------------------------------------------------------------
> table 2
>
> name varchar;*
> ---------------------------------------------------------------
>
> i want to insert the values of table 2 into table 1, with automatic id's.
>
> insert into table1(select * from table2);
>
> is not working, how can i append the data to table 1 with auto incremented
> or nextval.
>

INSERT INTO table1 (name) SELECT name FROM table2;


--
Guillaume
http://www.postgresql.fr
http://dalibo.com

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql