473,756 Members | 5,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shared Foreign Keys From Two Tables

I'm looking for a better way to make use of foreign keys. Here is a
sample setup:

-- TESTING Foreign Keys

create table mod (
mod_id int not null primary key,
name varchar(32) not null default ''
);

insert into mod(mod_id, name) values (1, 'module one');
insert into mod(mod_id, name) values (2, 'module two');

create table groupie (
groupie_id int not null primary key,
name varchar(32) not null default ''
);

insert into groupie(groupie _id, name) values (1, 'groupie one');
insert into groupie(groupie _id, name) values (2, 'groupie two');

create table groupie_mod (
groupie_mod_id int not null primary key,
groupie_id int not null references groupie on delete restrict,
mod_id int not null references mod on delete restrict,
UNIQUE (groupie_id, mod_id)
);

insert into groupie_mod(gro upie_mod_id, groupie_id, mod_id) values (1,
1, 1);
insert into groupie_mod(gro upie_mod_id, groupie_id, mod_id) values (2,
1, 2);
insert into groupie_mod(gro upie_mod_id, groupie_id, mod_id) values (3,
2, 2);

create table mod_pref (
mod_pref_id int not null primary key,
mod_id int not null REFERENCES mod(mod_id),
pref_key varchar(32) NOT NULL,
pref_value varchar(255) NOT NULL DEFAULT '',
UNIQUE (mod_id, pref_key)
);

insert into mod_pref(mod_pr ef_id, mod_id, pref_key, pref_value) values
(1, 1, 'key1', 'value1');
insert into mod_pref(mod_pr ef_id, mod_id, pref_key, pref_value) values
(2, 1, 'key2', 'value2');
insert into mod_pref(mod_pr ef_id, mod_id, pref_key, pref_value) values
(3, 2, 'key1', 'value1');

create table groupie_mod_pre f (
groupie_id int not null,
mod_id int not null,
pref_key varchar(32) NOT NULL,
pref_value varchar(255) NOT NULL DEFAULT '',
FOREIGN KEY (groupie_id, mod_id) REFERENCES groupie_mod (groupie_id,
mod_id),
FOREIGN KEY (mod_id, pref_key) REFERENCES mod_pref (mod_id, pref_key)
);

insert into groupie_mod_pre f(groupie_id, mod_id, pref_key, pref_value)
values (1, 1, 'key1', 'value1');

-- should fail because 'key3' doesn't exist!
insert into groupie_mod_pre f(groupie_id, mod_id, pref_key, pref_value)
values (1, 1, 'key3', 'value3');

The above setup works. In table groupie_mod_pre f, I needed mod_id to be
a shared common foreign key in two other tables. Consequently in used
two table constraints in groupie_mod_pre f. However, I thought that was a
waste of storage space having to repeat groupie_id and mod_id from
groupie_mod. Is there anyway to keep the functionality like above, while
using groupie_mod_id from groupie_mod in groupie_mod_pre f in place of
groupie_id and mod_id and still constrain mod_id to mod_pref table?

Regards,
Thomas
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #1
1 2092
On Fri, Jul 30, 2004 at 12:01:44 -0500,
"Thomas T. Thai" <to*@minnesota. com> wrote:

The above setup works. In table groupie_mod_pre f, I needed mod_id to be
a shared common foreign key in two other tables. Consequently in used
two table constraints in groupie_mod_pre f. However, I thought that was a
waste of storage space having to repeat groupie_id and mod_id from
groupie_mod. Is there anyway to keep the functionality like above, while
using groupie_mod_id from groupie_mod in groupie_mod_pre f in place of
groupie_id and mod_id and still constrain mod_id to mod_pref table?


Not easily. (It should be possible to do with triggers.) What you could do
is use a compound primary key in groupie_mod and mod_pref get rid of the
(presumably) surrogate primary keys.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #2

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

Similar topics

10
42421
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed to be optional, it's very definition is it's a necessary link to the parent table and part of the definition. If it's optional it shouldn't be part of the definition of a table and should be in a linking table instead. Comments?
31
3380
by: Robert Brown | last post by:
Let's say I have a type hierarchy: (just an example) the general entity customer: CREATE TABLE customer(customer_id int, customer_name varchar(250), customer_type int) three specific customer subtypes: 1 - business, 2 - home, 3 - university
1
3004
by: Vinodh Kumar P | last post by:
I understand the number of foreign keys allowed is restricted by the DBMS I use. In a general relational schema design perspective how many foreign keys a table shall have? If I have large number of foreign keys what anamolies it will lead to? Is this crucial to identify all the foriegn key relationships for a table? Vinodh
6
2811
by: Brendan Jurd | last post by:
Hi all, I read on the manual page for Inheritance that: "A limitation of the inheritance feature is that indexes (including unique constraints) and foreign key constraints only apply to single tables, not to their inheritance children. Thus, in the above example, specifying that another table's column REFERENCES cities(name) would allow the other table to contain city names but not capital names. This deficiency will probably be fixed...
5
3338
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example I'm adding a customer. The Customer fields are mostly foreign keys that refer to primary keys in other tables, left join instead of junction tables at this point. So, when I want to add a customer record, I also need to add records to the other...
9
2399
by: Jax | last post by:
I'm making my first ever database for my program. I understand the concept of one to many relationships but fail to see the advantage of using a secondary key over a primary one. I have a lot of tables and link them together by one key the CaseKey. What is the disadvantages of this method? Why should I include more detailed item keys (e.g an arraylist of customer credit items would be stored in the CustomerCreditItem table surely it's...
2
2522
by: Ian Davies | last post by:
I have created a database with about 17 tables. I have been creating foreign keys some of which have worked but when creating others I get the message below ************************* 1005 (ER_CANT_CREATE_TABLE) Cannot create table. If the error message refers to errno 150, table creation failed because a foreign key constraint was not correctly formed.
1
3259
by: rbarber | last post by:
I have to synchronize 2 databases hourly but am having difficulty maintaining foreign key relations. These tables use auto-increment columns as primary keys, with child records in other tables related with foreign keys. I can't change the way the local software uses primary or foreign keys as it is hardcoded in the local app. (microsoft retail management system)..(however the web-remote app is easily customized). I am using CDB synchronizer to...
1
11009
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line 1 Could not drop object 'Client' because it is referenced by a FOREIGN KEY constraint.
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.