Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to copy all data from one table to another table, plus an extra column?

Tags:

sql

oracle

I have two tables that are very slightly different. Table A has 4 columns, and Table B has only 3. I want to copy all the data from Table B into Table A, but I also want to populate the extra column with the value 1 for every row.

This would work if not for the extra column:

insert into TABLEA (COL1, COL2, COL3) select COL1, COL2, COL3 from TABLEB;

Unfortunately, the extra column in Table A is not nullable, so I can't just run an update afterwards.

Thanks for any help!

like image 456
Nick Brunt Avatar asked Dec 06 '22 11:12

Nick Brunt


2 Answers

Specify the column and use a constant for the value (note you can mix constants and column references in a select clause). In this case we're specifying every row will get the constant 1 for column COL4.

insert into TABLEA (COL1, COL2, COL3, COL4)
select COL1, COL2, COL3, 1
from TABLEB;
like image 103
lc. Avatar answered Dec 29 '22 00:12

lc.


insert into TABLEA (COL1, COL2, COL3, extra) 
select COL1, COL2, COL3, 1 
from TABLEB;
like image 42
John Woo Avatar answered Dec 29 '22 00:12

John Woo