pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Classes, Inheritance, and Children
2+ messages / 2 participants
[nested] [flat]

* Classes, Inheritance, and Children
@ 2000-07-27 07:06  Thomas Swan <tswan@olemiss.edu>
  0 siblings, 1 reply; 2+ messages in thread

From: Thomas Swan @ 2000-07-27 07:06 UTC (permalink / raw)
  To: pgsql-sql


I think I may have asked this before... If I did I'm sorry, but maybe this 
attempt, assuming a prior one, may be a little more clear.

create table foo (id int8);
create table bar1 (name text) inherits (foo);
create table bar2 (data text) inherits (foo);
create table hybrid ( ) inherits (bar1, bar2);

INSERT INTO foo VALUES (1);
INSERT INTO bar1 VALUES (2,'myname');
INSERT INTO bar2 VALUES (3,'mydata');
INSERT INTO hybrid VALUES (4,'morename','moredata');


I want to do a SELECT * FROM foo*; but I only get the 'id' column as in :

id
---
  1
  2
  3
  4


What would be the query to get the following table or a magical way to 
expand children?

I had originally hoped that SELECT * FROM foo* would yield the following, 
but it's not so.

id | name       | data
---+------------+-------------
  1 | null       | null
  2 | 'myname'   | null
  3 | null       | 'mydata'
  4 | 'morename' | 'moredata'
    |            |

I tried SELECT id, name AS NULL, data AS NULL FROM foo*; but that didn't do 
anything but make 2 null columns...

Any help would be ... helpful...

Thanks,
Thomas

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Classes, Inheritance, and Children
@ 2000-07-27 09:01  Oliver Elphick <olly@lfix.co.uk>
  parent: Thomas Swan <tswan@olemiss.edu>
  0 siblings, 0 replies; 2+ messages in thread

From: Oliver Elphick @ 2000-07-27 09:01 UTC (permalink / raw)
  To: Thomas Swan <tswan@olemiss.edu>; +Cc: pgsql-sql

Thomas Swan wrote:
  >I think I may have asked this before... If I did I'm sorry, but maybe this 
  >attempt, assuming a prior one, may be a little more clear.
  >
  >create table foo (id int8);
  >create table bar1 (name text) inherits (foo);
  >create table bar2 (data text) inherits (foo);
  >create table hybrid ( ) inherits (bar1, bar2);
  >
  >INSERT INTO foo VALUES (1);
  >INSERT INTO bar1 VALUES (2,'myname');
  >INSERT INTO bar2 VALUES (3,'mydata');
  >INSERT INTO hybrid VALUES (4,'morename','moredata');
  >
  >
  >I want to do a SELECT * FROM foo*; but I only get the 'id' column as in :
  >
  >id
  >---
  >  1
  >  2
  >  3
  >  4

This is correct in object-oriented theory.  foo only knows about its own
features; it does not know about additional features of its descendants,
nor should it.

  >What would be the query to get the following table or a magical way to 
  >expand children?
  >
  >I had originally hoped that SELECT * FROM foo* would yield the following, 
  >but it's not so.
  >
  >id | name       | data
  >---+------------+-------------
  >  1 | null       | null
  >  2 | 'myname'   | null
  >  3 | null       | 'mydata'
  >  4 | 'morename' | 'moredata'
 
You need to use a UNION of the four tables, with nulls
supplied where necessary:

select * from hybrid                   -- specify first to establish the
                                       -- column types
union select id, null, null from foo
union select id, name, null from bar1
union select id, null, data from bar2;

Unfortunately, you can't make this a view, because views of unions are
not yet supported.

-- 
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
PGP: 1024R/32B8FAA1: 97 EA 1D 47 72 3F 28 47  6B 7E 39 CC 56 E4 C1 47
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "But the wisdom that is from above is first pure, then 
      peaceable, gentle, and easy to be intreated, full of 
      mercy and good fruits, without partiality, and without
      hypocrisy."     James 3:17 





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2000-07-27 09:01 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-07-27 07:06 Classes, Inheritance, and Children Thomas Swan <tswan@olemiss.edu>
2000-07-27 09:01 ` Oliver Elphick <olly@lfix.co.uk>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox