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

Questions about Foreign Keys

first of all, these are my first steps into SQL world so please dont shoot me.

I have looked around to find answers to the following questions but had no luck so far:

When querying a table with a foreign key, will its linked data also be shown?
if not, how could i adjust that query to show its matching row of the other table?

I am trying to make a forum (just for fun and learning) and have a database like the following (really simple)

table Subforum
(PK) ID int
Title varchar(150)
Description varchar(150)

table Topics
(PK) ID int
(FK -> (table Subforum) ID) Subforum_ID int
title varchar(150)

table Posts
(PK) ID int
(FK -> (table Topics) ID) Topic_ID int
Title varchar(150)
Body varchar(MAX)
(FK -> (table Users) ID) User_ID int

table Users
(PK) ID int
Username varchar(50)
Password varchar(MAX)
i hope you can understand and give me some hints/tips on how to go on with my project. At this moment im doubting whether to use Foreign Keys and just query everything.
Im using C# ASP.NET 3.5, if you need to know anything more i will answer as soon as i can.
Jul 9 '09 #1
3 1523
ck9663
2,878 Expert 2GB
The foreign key constraint refers to maintaining the relationship of the table. So that you don't insert a row on the child table with a key not existing on the parent table. But the table is still stored and accessed separately. Depending on your need, you can either create a stored proc, function or views to have your desired rowset returned to your app.

In either case, explore the world of JOINS. It will help you linked those tables together and return it as if it's a single row.

Good luck!!!

--- CK
Jul 9 '09 #2
Its simple..as suggested by ck9663 you need to explore worl of joins.
You can say foreign keys are used to maintaine the relationships of tables.
You can use this

SELECT POSTS.*,TOPICS.*,SUBFORUM.*,USERS.* from
POSTS INNER JOIN TOPICS ON POSTS.TOPIC_ID=TOPICS.ID
INNER JOIN USERS ON POSTS.USER_ID=USERS.ID
INNER JOIN SUBFORUM ON TOPICS.SUBFORUM_ID=SUBFORUM.ID

Insted of stars (*) you can select your desired column in this query.
Use SqlServer query builder and paste this query, it will show you the complete diagram, which would be easy to understand.
HAPPY QUERY..
-Pankaj Tambe
Jul 16 '09 #3
Thanks for your reactions, using Pankajs query i was able to crop some queries together.
i just pasted the entire thing and slimmed it down till i got what i needed, i cant write joins just yet, but i am capable of manipulating it
Jul 23 '09 #4

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

Similar topics

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...
1
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...
0
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
2
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...
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...
4
by: Ted | last post by:
Understand, I have developed a number of applications using RDBMS, including MySQL, PostgreSQL and MS Access, but this is my first experience with MS SQL. I'd bet my bottom dollar that MS SQL...
1
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...
1
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...
0
by: David | last post by:
On Wed, Jun 18, 2008 at 11:16 AM, M.-A. Lemburg <mal@egenix.comwrote: Thanks for your reply. How do you maintain foreign key references with this approach? eg, you have these 4 tables: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.