Skip site navigation (1) Skip section navigation (2)

Re: Database Views

From: Frank Bax <fbax(at)sympatico(dot)ca>
To:
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Database Views
Date: 2008-11-06 17:45:38
Message-ID: 49132D42.30803@sympatico.ca (view raw or flat)
Thread:
Lists: pgsql-novice
gcj wrote:
> I have the following three tables:  CARS  TRUCKS  SUVS
  >
> Although the tables contain numerous columns each, they each contain the 
> following columns in common:
> 
> Manufacturer
> Identification Number
> Color
> 
> I would like to create a view that will display this common information 
> as well as a column called “Type” that will indicate what kind of record 
> it is – eg Car, Truck, or SUV.


create table car (manufacturer varchar, vin varchar, color varchar);
create table truck (manufacturer varchar, vin varchar, color varchar);
create table suv (manufacturer varchar, vin varchar, color varchar);

insert into car values('Toyota','TTT', 'Silver');
insert into truck values('Mack','QQQ', 'Black');
insert into suv values('Lexxus','ZZZ', 'Yellow');

create or replace view vehicle as
select 'car' as type,manufacturer,vin,color from car
union
select 'truck' as type,manufacturer,vin,color from truck
union
select 'suv' as type,manufacturer,vin,color from suv
;

select * from vehicle;

  type  | manufacturer | vin | color
-------+--------------+-----+--------
  car   | Toyota       | TTT | Silver
  suv   | Lexxus       | ZZZ | Yellow
  truck | Mack         | QQQ | Black

In response to

Responses

pgsql-novice by date

Next:From: Tom LaneDate: 2008-11-06 17:50:22
Subject: Re: Database Views
Previous:From: gcjDate: 2008-11-06 17:21:11
Subject: Database Views

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group