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

How to do a Join Using Foreign Keys in Stored in a Database Column?

I have a database column that stores a comma delimited list of foreign
keys. Would someone show me how to do a join using the values from a
list stored within a record?

For example, a record in tbl_cds.genre_id might have a value of "2,
5, 6" corresponding to genre_ids 2 , 5 and 6. I want to join
tbl_cds.genre_id to tbl_genre.genre_id using the values in that data
field.

It seems I need a loop like this:
SELECT * FROM tbl_cds
WHERE
Begin Loop
tbl_cds.genre_id[i] = tbl_genre.genre_id
End Loop.

Would someone give me the correct syntax?
Is there an alternative method that would create less overhead?

Sorry for such a novice post.

Apr 17 '07 #1
2 2961
On 17 Apr 2007 14:55:36 -0700, Chris wrote:
>I have a database column that stores a comma delimited list of foreign
keys. Would someone show me how to do a join using the values from a
list stored within a record?

For example, a record in tbl_cds.genre_id might have a value of "2,
5, 6" corresponding to genre_ids 2 , 5 and 6. I want to join
tbl_cds.genre_id to tbl_genre.genre_id using the values in that data
field.

It seems I need a loop like this:
SELECT * FROM tbl_cds
WHERE
Begin Loop
tbl_cds.genre_id[i] = tbl_genre.genre_id
End Loop.

Would someone give me the correct syntax?
Is there an alternative method that would create less overhead?

Sorry for such a novice post.
Hi Chris,

You can't. And that's because the database design volates one of the
basic principles of databases - you store a single value in a column.

If a CD can belong to three genres, you'll have to add a table like
this:

CREATE TABLE CDGenres
(CD_ID int NOT NULL,
Genre_ID int NOT NULL,
PRIMARY KEY (CD_ID, Genre_ID),
FOREIGN KEY (CD_ID) REFERENCES tbl_cds(CD_ID),
FOREIGN KEY (Genre_ID) REFERENCES Genres(Genre_ID)
);

If you don't have a master table of all genres, then you can leave out
the last foreign key constraint.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Apr 17 '07 #2
Chris writes:
I have a database column that stores a comma delimited list of foreign
keys. Would someone show me how to do a join using the values from a
list stored within a record?

For example, a record in tbl_cds.genre_id might have a value of "2,
5, 6" corresponding to genre_ids 2 , 5 and 6. I want to join
tbl_cds.genre_id to tbl_genre.genre_id using the values in that data
field.

It seems I need a loop like this:
SELECT * FROM tbl_cds
WHERE
Begin Loop
tbl_cds.genre_id[i] = tbl_genre.genre_id
End Loop.

Would someone give me the correct syntax?
Is there an alternative method that would create less overhead?
As Hugo said, you are in desperate need of a redesign.

On SQL 2005 you can nevertheless do:

SELECT *
FROM tbl_cds c
OUTER APPLY list_to_table(c.genre_id) l
JOIN tbl_genre g ON l.num = g.genre_id

where list_to_table is a table-valued function that unpacks the
comma-separated list into a table. See
http://www.sommarskog.se/arrays-in-sql.html for examples of such functions.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 18 '07 #3

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

Similar topics

1
by: Paul Bramscher | last post by:
Here's one for pathological SQL programmers. I've got a table of things called elements. They're components, sort of like amino acids, which come together to form complex web pages -- as nodes...
10
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...
3
by: heroe | last post by:
Hello *, i write you with an urgent problem. I would like to create forign keys to datetime field, but it doesn't seem to work. I get Can't create table '.\sampleDB\student_module.frm' (errno:...
2
by: Ev | last post by:
I have a database table in SQL Server that has a self join. In C# I have a DataTable with a self-join. I have defined a foreign key constraint on the DataTable for the self join. The...
5
by: Rico | last post by:
Hello, I have a relationship that I'd like to modify using DAO. Right now the Cascading updates are off, but I'd like to add them and I'm not sure the best way to do that. Any ideas? ...
6
by: PW | last post by:
I've created an ASP application which uses an Access database. I've created an outer join query, but for some reason the "Property_Def" column is not aligning with the "ESPN" column. They should...
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...
2
by: gubbachchi | last post by:
Hi, How to use foreign key with join queries. I have used the query SELECT first_name,last_name,user_email from Info,Registry WHERE user_id='1'; where user_id = foreign key...
6
by: insirawali | last post by:
Hi all, I have this problem, i need to know is there a way i cn use the data adapter's update method in this scenario. i have 3 tables as below create table table1{ id1 int identity(1,1)...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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...

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.