473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Inherited tables and column references

In the following example:

create table parent (
id integer unique not null,
name varchar(24)
);

create table child (
first_name varchar(256),
last_name varchar(256)
) inherits (parent);

create table relations (
id integer references child (id)
);

Creating the relations table fails with:

ERROR: there is no unique constraint maching given keys for
referenced table "child"

If I change the last table to:

create table relations (
id integer references parent (id)
);

it works.

This essentially means that references must point to the real table
where the referenced column is, and cannot point to that same column in
a table that inherits the real table.

is this by design, is it a bug, is it just not implemented yet, or am I
doing this wrong?
/s.
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #1
2 2816
Sounds right to me -- inherited table "child" doesn't really have a key of
it's own in your schema -- it's parent has the key... the child table is
basically the parent table plus some extra columns...

To do what it looks like you want to do -- you would need separate primary
keys on both parent and child but it doesn't seem exactly "normalized" to
me... this makes more sense...

To see what inheritance is really all about do this:

CREATE TABLE person (

id integer not null primary key,
first_name varchar(40) not null,
last_name varchar(50) not null

);

CREATE TABLE child (

child boolean default TRUE

) INHERITS (person);

INSERT INTO PERSON (id, first_name, last_name) VALUES (1, 'George',
'Smith');
INSERT INTO PERSON (id, first_name, last_name) VALUES (2, 'Mary', 'Smith');
INSERT INTO child (id, first_name, last_name) VALUES (3, 'Baby', 'Smith');
SET SQL_INHERITANCE TO ON;

Then run these 4 queries one at a time...

SELECT * FROM person;
SELECT * FROM ONLY person;
SELECT * FROM child;
SELECT * FROM ONLY child;

--
Greg Patnude / The Digital Demention
2916 East Upper Hayden Lake Road
Hayden Lake, ID 83835
(208) 762-0762

"Scott Goodwin" <sc***@scottg.net> wrote in message
news:A9**********************************@scottg.n et...
In the following example:

create table parent (
id integer unique not null,
name varchar(24)
);

create table child (
first_name varchar(256),
last_name varchar(256)
) inherits (parent);

create table relations (
id integer references child (id)
);

Creating the relations table fails with:

ERROR: there is no unique constraint maching given keys for
referenced table "child"

If I change the last table to:

create table relations (
id integer references parent (id)
);

it works.

This essentially means that references must point to the real table
where the referenced column is, and cannot point to that same column in
a table that inherits the real table.

is this by design, is it a bug, is it just not implemented yet, or am I
doing this wrong?
/s.
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 22 '05 #2

On Thu, 19 Feb 2004, Scott Goodwin wrote:
In the following example:

create table parent (
id integer unique not null,
name varchar(24)
);

create table child (
first_name varchar(256),
last_name varchar(256)
) inherits (parent);

create table relations (
id integer references child (id)
);

Creating the relations table fails with:

ERROR: there is no unique constraint maching given keys for
referenced table "child"

If I change the last table to:

create table relations (
id integer references parent (id)
);

it works.

This essentially means that references must point to the real table
where the referenced column is, and cannot point to that same column in
a table that inherits the real table.

is this by design, is it a bug, is it just not implemented yet, or am I
doing this wrong?


There are two separate things here that are gotchas
The first is that unique constraints don't inherit, and foreign keys must
refer to a unique constraint. You can make a unique constraint on child
and then you should be allowed to reference to it. However, that
constraint will not prevent a value already in parent from also being used
in child.
The second is that references don't inherit either, so if you reference
parent for example, rows in child will not be considered for purposes of
satifying the constraint.

These are both deficiencies in inheritance and the constraints in
question. They're likely to get fixed eventually, but AFAIK noone's
really stepped up to do alot of the work.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
6
by: Andreas | last post by:
Hello list, what about uniqueness of inherited primary keys ? eg you have : create table objects ( id int4, date_created timestamp(0), primary key (id)
2
by: Ben | last post by:
Right now I have 1 table. The first part is the first and last name along with address etc. There is about 10-15 fields here. The second part consists of times, penalties and if they enter this...
35
by: MuZZy | last post by:
Hi All, I got a issue here and hope someone can help me: Let's consider this code: // =================== CODE START ================================= using System; using System.Data; ...
18
by: Ken Kazinski | last post by:
Does anyone know of a good example for creating a access database and then tables within that database. All the examples I have found so far use a SQL database. Thanks, Ken
0
by: Eva Hovelsrød | last post by:
Hi, I just realized that foreign keys to an inherited table don't work when the record was inserted into a child table. That is postgresql-7.3.2-3. Does anyone know whether anyone is working on...
2
by: Benjamin Smith | last post by:
I have two tables like following: create table attendancereport ( id serial unique not null, staff_id integer not null references staff(id), schoolyear varchar not null references...
0
by: Gary Townsend | last post by:
I am using Postgres 7.4.6 i have 3 tables i want to join 3 tables and return a list of route_id to which a specified user_id does NOT belong. Table "public.vts_users" Column | Type...
15
by: Peter | last post by:
I have the following web page and I am trying to have the Field lables NOT to wrap. It looks fine in a designer but when I run the program in a browser the lables that have a space wrap. How do I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.