473,434 Members | 1,432 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,434 software developers and data experts.

Composite Foreign Key

I have been looking and looking and cannot come up with a clear, solid solution.

I have a table and that table uses 3 columns to create a composite PK. I need to use this PK as a foreign key in another table. That means I need to add these three columns to the child table which...quite frankly..i think is kind of a pain. I cannot believe there is not a simpler method. If anyone has any ideas or alternative solutions, pleaes give me a hollar. Thanks a lot.
May 16 '07 #1
5 4192
frozenmist
179 Expert 100+
Hi,
I dont know how good this is but why dont you try using an identity column.
For each combination of the three keys the identity column will have a value.
Pust this ID column value in the child table.
Just a thought.
Hope I could give something meaningful
Cheers
May 16 '07 #2
chandu031
78 Expert
I have been looking and looking and cannot come up with a clear, solid solution.

I have a table and that table uses 3 columns to create a composite PK. I need to use this PK as a foreign key in another table. That means I need to add these three columns to the child table which...quite frankly..i think is kind of a pain. I cannot believe there is not a simpler method. If anyone has any ideas or alternative solutions, pleaes give me a hollar. Thanks a lot.
Hi,

You can have a surrogate key on the parent table which as frozen pointed out would be an identity column. This surrogate key can then be used as a foreign key in the child table.
May 16 '07 #3
Right..thanks..I see what you two are saying. However..wouldn't this allow for the three columns to have redundant data and not be unique? I kind of needs this composite PK be unique. Any ideas?
May 16 '07 #4
pradeep kaltari
102 Expert 100+
Right..thanks..I see what you two are saying. However..wouldn't this allow for the three columns to have redundant data and not be unique? I kind of needs this composite PK be unique. Any ideas?
Hi,
You can still have a unique constraint on the combination of those three columns and the identity column can be the primary key.
Expand|Select|Wrap|Line Numbers
  1. ALTER TABLE <table_name> ADD CONSTRAINT unique_col UNIQUE(col1,col2,col2)
  2.  
Regards,
Pradeep
May 16 '07 #5
yes yes yes! that is perfect. Thanks for opening up my eyes everyone. Really, thanks a lot!
May 16 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

31
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...
5
by: John | last post by:
Specifically for joint tables... tblStudents tblClasses tblClasses_Students Is it be good programming to use a composite primary key in tblClasses_Students (where the key is ClassID and...
2
by: bobby_b_ | last post by:
I have a table where fields 1 and 2 make up the primary key. Because of this, I have a unique composite index on fields 1 and 2 (as required by DB2). Now my question is: Fields 1 and 2 are also...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
6
by: mac | last post by:
Summary: 1. I want to define a column in anMS Access table to be lookups on other tables. 2. The table that is the data source (e.g the "parent" table) has a composite primary key. 3. When...
3
by: bvdrsganesh1981 | last post by:
Hi Can We Create a Foreign Key Relation in Table1 with a Table2 Table 2 having Composite Primary key with columns T2Col1,T2Col2,T2Col3, T2Col4 where first 3 columns are composite primary...
1
NickZA
by: NickZA | last post by:
Hi Rather than posting in an old thread I thought I'd start this one, but basically what I need to do is outlined here: http://www.thescripts.com/forum/thread647242-composite+foreign.html I am...
4
by: Wolfgang Keller | last post by:
Hello, so far it seems to me as if the only ORM module for Python which supports composite primary/foreign keys was SQLAlchemy. Which looks a little bit "overbloated" for my needs: I "just" need...
1
by: Madhususdhan | last post by:
Hellow, I am new to MS SQL Server and I have been designing a database for my company. I am using Microsoft Visio for desigin and in my database design, I am planning to use composite primary key for...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
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
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...
0
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.