Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Building a view with union

Posted on 2004-09-01
4
Medium Priority
?
1,335 Views
Last Modified: 2007-12-19
Hi everybody:

I need to build a view in Oracle 8i or 9i. This view must make a UNION from to tables.

Example:

I have this:

table: employee
name: varchar
age: number

table: app_user
name: varchar
age: number

I need this:

create view myuser as
  select name, age from employee
     union
  select name, age from app_user

Thanks in advanced
0
Comment
Question by:vcarballo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 12

Expert Comment

by:geotiger
ID: 11960210

I do not know why you are asking the question while you are providing the anwser at the same time. Have you tried yours? It should work:

CREATE OR REPLACE VIEW your_user_vw AS
SELECT name, age FROM employee
UNION
SELECT name,age FROM app_user;

GT

0
 

Expert Comment

by:sfardin
ID: 11960976
I agree with geotiger.  you have already answered your query.

Are you getting any error while creating view?
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 1000 total points
ID: 11961424
Note that if you don't mind duplicates in the view (i.e. if there are possibly the same rows in both the "employee" and "app_user" tables, and these both occurring in the view), then use "UNION ALL" rather than "UNION" - UNION is a strict union, so any row in the resulting view will only appear once - to do this the UNION performs a full sort and filters any duplicate rows. UNION ALL is all the rows from the first SELECT and all the rows from the second SELECT, any duplicated rows will remain duplicates, but there is no sort etc... (basically, UNION with large tables is very unadvised, if at all possible to avoid... its very similar to using DISTINCT without needing it...

Cheers,
C.
0
 
LVL 1

Expert Comment

by:StevenTaylor
ID: 11961441
One thing to be aware of is that the UNION option will cause any identical entries in both tables to be only displayed once (which is probably what you want).  However, these are really big tables, remember that there will be a unique sort possibly causing a large performance overhead.

UNION ALL will simply join the results from both tables without the unique sort but this is only useful if each table stores different entries.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question