473,408 Members | 2,813 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,408 software developers and data experts.

Foriegn keys

I have a table, lets call it contacts that holds personal info like name phone and such, it also holds a fk to a table called workplace. the workplace table holds company info. one column in the workplace table is instructor, which holds a fk back to the contact table. Is this the way it should be done?

I am trying to connect all employees with their workplace and at the same time know who is the primary instructor at any given workplace.
May 21 '09 #1
4 1647
prabirchoudhury
162 100+
ok.. its depends on what relation you are tryng to build .. is that one to many or many to many relationship.

1. is that one to many relation ship (contacts ->workplace) that is one person may have ome then one workplace . so then take the contact pk as fk in workplace table or one workplace(role) may have more people then workplace pk as fk in contacts table

2. in many to many relationship, one contacts may have morethen one workplace or one workplace may have more then one contact. then you need tp make another connecting table that would take contacts pk and workplace pk as fk in that connecting table .. say connecting table name contacts_workplace
PK | FK | FK
cw_id | contacts pk | workplace pk


think that gonna help you ..

more online on relational database tutorial
May 22 '09 #2
Atli
5,058 Expert 4TB
Hi.

I can't see a problem with the structure you described.
Other than it limits employees to a single workplace, and only allows a single instructor per workplace.
In other words; both relationships are one-to-many (1:N) relationships.

If that's all you need, then it should work fine.

If you needed more, like say; to allow each employee to join multiple workplaces, and have multiple instructors per workplace, then you would have to switch over to many-to-many (N:M) relationships.

Adding to prabirchoudhury's explanation; this means extracting the FK columns from the actual tables and putting them into intermediary tables, where a list of relationships would be stored.
Like:
Expand|Select|Wrap|Line Numbers
  1. Employee:      Workplace:
  2. +----+------+  +----+--------+
  3. | ID | Name |  | ID | Name   |
  4. +----+------+  +----+--------+
  5. |  1 | Joe  |  |  1 | First  |
  6. |  2 | Jane |  |  2 | Second |
  7. |  3 | Jack |  |  3 | Third  |
  8. +----+------+  +----+--------+
  9.  
  10. EmployeeWorkplaces:
  11. +------------+-------------+
  12. | EmployeeID | WorkplaceID |
  13. +------------+-------------+
  14. |          1 |           1 |
  15. |          1 |           2 |
  16. |          2 |           2 |
  17. |          2 |           3 |
  18. |          3 |           1 |
  19. |          3 |           3 |
  20. +------------+-------------+
  21.  
  22. WorkplaceInstructors:
  23. +-------------+------------+
  24. | WorkplaceID | EmployeeID |
  25. +-------------+------------+
  26. |           1 |          1 |
  27. |           1 |          2 |
  28. |           2 |          3 |
  29. |           2 |          1 |
  30. |           3 |          1 |
  31. +-------------+------------+
May 22 '09 #3
prabirchoudhury
162 100+
thats good, Thanks Atli for more explain in details
May 22 '09 #4
Thanks for the explanations. I think thats what I need to do, is add the second table for the many to many relationship.
May 27 '09 #5

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

Similar topics

3
by: Girish Agarwal | last post by:
Hi All, I have a database consisting of 2 tables. patient_info ( Patient Information ).Looks like lastname VARCHAR(10) fname VARCHAR(10) ssno INT(9) PRIMARY KEY ID INT(4) AUTO_INCREMENT ...
0
by: Girish Agarwal | last post by:
--0-474210375-1058976151=:31789 Content-Type: text/plain; charset=us-ascii Content-Id: Content-Disposition: inline Note: forwarded message attached. __________________________________
1
by: Bob | last post by:
Hi, Does anyone know why forgein key constraints are not enforced when updateing multiple tables using a combination of views and rules? This doesn't seem to be a major problem as I can...
0
by: David Allison | last post by:
Foriegn Key Required - Yes - no wonder it didnt work. Now they tell me. -- Dave Allison
0
by: Dev | last post by:
Hello all, I have a database system in which i truncate a main table to update the information. I would like to setup some foriegn keys referencing back to that table but am concerned as to how...
4
by: irfi | last post by:
Hi, I am a form which has a sub form, The sub form is linked with primary key of main form as a foriegn key in sub form, very basic, In the main form, I have another field which is...
2
by: raghunana | last post by:
hello friends, i am a beginer.I was unable to add a foriegn key constraint to my NAMES table which refers my EMPLOYEE TABLE SQL> desc employee Name Null? ...
1
by: nads707 | last post by:
i hav following 2 tables n i am able to insert data into the second table having a foriegn key create table Customer_Details( Customer_ID integer primary key, Customer_First_Name varchar(75),...
1
by: srinivaspnv21 | last post by:
hi every one, plz help me out in setting primary key and foriegn key to tables in sql server 2000 and brief description on it or any article u have plz send me...... waiting for u r...
0
by: chets92 | last post by:
sir can u give me an example (simple code) of how to use primary key and foriegn key queries...ie if i delete the primary key item the result should be reflected in other forms....
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?
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
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
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.