views:

229

answers:

6

hi, i have 2 tables in an oracle database, which has the same column-names and types. for example:

Table1: id, name, comment Table2: id, name, comment

How can i show all data from both table in one view?

A: 

Why two identical tables? Whatever happened to "Don't Repeat Yourself"? Sorry, sounds like a bad design smell to me.

Whatever difference inspired you to create two tables, I'll bet it really could be another attribute to distinguish two groups in one table.

duffymo
it's an old application, that i don't want to change. i fetch deleted items from an table with an trigger and put it into my new table. after this i do some statistic things with the current items and the "deleted" ones.
ddejmek
Your choice, of course, but we're talking about a single ALTER to add a column to a table, plus the attendant trigger and other system changes.
duffymo
Yet another SO'er obsessed with cleaning up the world instead of simply answering the question. Why assume *HE* made the tables? Why assume he's *ALLOWED* to do DDL?
Just something to consider, that's all. I think it is a potential answer to the question. What's wrong with at least proposing it? I'm not obsessed with anything, just offering an opinion.
duffymo
+2  A: 

If you want 4 separate columns, simply use aliases, like you would any other select.

create or replace view vw_my_view as
  select t1.id t1_id
        ,t1.comment t1_comment
        ,t2.id t2_id
        ,t2.comment t2_comment
  from table1 t1
    inner join table2 t2 on join condition
  where filter conditions

EDIT Of course, your tables will relate to each other in some way, otherwise there is no way for a single row of your view to mean anything. You will therefore have a join condition to join the two tables, such as t1.id = t2.id

If you want them in two columns, use Union

create or replace view vw_my_view as
  select id
        ,comment
  from table1
  union all                -- use ALL unless you want to lose rows
  select id
        ,comment
  from table2;
darasd
You realize that in the first answer you'll get t1 x t2 total rows in the output. You'll get A LOT more rows than you bargained for.
A: 

SELECT * FROM TABLE1 UNION SELECT * FROM TABLE2

(or UNION ALL if you want duplicates)

People don't usually WANT duplicates, but they do want every row duplicated or not. In cases like these the two tables are almost always mutually exclusive thus UNION and UNION ALL will give identical answers but one will have a MUCH higher cost. Always choose the cheap choice by default and stray.
A: 

I agree with what duffymo says, but if there is a good reason for it then a UNION will do it for you. e.g.

SELECT id, name, comment FROM Table1
UNION
SELECT id, name, comment FROM Table2
DanSingerman
UNION is almost never what people want and it forces an unnecessary sort.
A: 
select * from table1
union
select * from table2;
Chry Cheng
UNION is almost never what people want and it forces an unnecessary sort.
A: 

thanks, works great!!!!

ddejmek