General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 16th, 2003, 05:52 AM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
Question PostgreSQL Inheritance Constraints

Hey guys,

I'm playing around with inherited tables in Postgres, where my parent table defines the id as a primary key sequence (i.e. "serial" or "int"). The subclass tables inherit the id and other columns from the parent table.

Now I want a new table (outside of this class structure) that REFERENCES the id of one of my subclass tables. Is that possible or does it have to reference the parent table directly? (If that's the case, then is there a way to add a CONSTRAINT using tableoid that would check which subclass table the id number is in?)

Also on a related note, I was able to use ALTER TABLE to redefine a column of one of my subclass tables which was inherited. When I check the parent table, the column definition remains unchanged. Does this mean that inherited tables exhibit polymorphism?

Cool, but mind-boggling from a data constraint point of view...

My hair is too short to tear out right now.

Anyone?

Reply With Quote
  #2  
Old July 26th, 2003, 05:53 PM
avit avit is offline
Not Yet Perfect
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Squamish, BC
Posts: 111 avit User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via ICQ to avit
After doing some research, this is apparently a known long-standing issue with inheritance in Postgres. Nobody calls it a "bug" outright, though. Just a limitation. It makes me wonder how others have tackled problems of a similar nature.

I've since devised a different way, but if you guys have any other suggestions for how you've handled this sort of thing in PostgreSQL, let me know.

To grossly simplify, here's what I was trying to do.

Code:
CREATE TABLE products (
  id     serial      NOT NULL PRIMARY KEY,
  name   varchar(64) NOT NULL,
  price  numeric(6,2)
);

CREATE TABLE pants (
  waist    smallint,
  length   smallint,
  colour   varchar(12)
) inherits (products);

CREATE TABLE computers (
  cpu    varchar(12),
  mhz    smallint,
) inherits (products);

INSERT INTO pants (name,price,waist,length,colour)
  VALUES ('Brand-X Cargo Pants', 49.95, 32, 34, 'khaki');

INSERT INTO computers (name,price,cpu,mhz)
  VALUES ('Flower Power iMac', $666.66, 'Motorola 750', 500);

SELECT id,name FROM products;

--> 1  Brand-X Cargo Pants
--> 2  Flower Power iMac

INSERT INTO computers (id, name,price,cpu,mhz)
  VALUES (1, 'Mac Plus', $5.00, 'Motorola 68000', 8);

SELECT id,name FROM products;

--> 1  Brand-X Cargo Pants
--> 1  Mac Plus
--> 2  Flower Power iMac


Huh? But products.id is a primary key! The "Mac Plus" screwed it up by inserting duplicate values!

No, I'm not actually making a database for a 24hr Pants-and-Computers store, but I'm using this department store analogy to show how different records will need different sets of attributes, yet I would still need to select from all of them as "products" and maintain referential integrity.

Ideas and discussion welcome...

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > PostgreSQL Inheritance Constraints


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway