简体   繁体   中英

Concatenating two tables with same column number and name

I have two tables:

TABLE_A

head1 head2
foo   case1
bar   case2

and TABLE_B

head1 head2
hux   case1
pix   case2
ilf   case1

What I want to do is to create this table:

head1 head2
foo case1
bar case2
hux case1
pix case2
ilf case3

Namely just combine them, without removing any redundancy.

What's the right command to do it? I tried this but it didn't give the the desired result.

CREATE TABLE TABLE_AB(head1 text, head2 text)
(SELECT * FROM TABLE_A)
UNION ALL
(SELECT * FROM TABLE_B)

You may change the columns names to the desired ones.

CREATE TABLE TAB1 (ID number, NAME VARCHAR2(10));
CREATE TABLE TAB2 (ID number, NAME VARCHAR2(10));

INSERT INTO TAB1 VALUES (1, 'AAA');
INSERT INTO TAB1 VALUES (2, 'BBB');
INSERT INTO TAB1 VALUES (3, 'CCC');

INSERT INTO TAB2 VALUES (1, 'EEE');
INSERT INTO TAB2 VALUES (4, 'FFF');

CREATE TABLE TAB AS ((SELECT * FROM TAB1) UNION ALL (SELECT * FROM TAB2));

You can omit the parenthesis around the union parts. Also, for create table ... select , you don't have the specify the column definitons.

create table Table3
select  *
from    Table1
union all
select  *
from    Table2

Example at SQL Fiddle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM