Jump to content

MySQL tables relationship with a foreign key on parent table


josephbupe

Recommended Posts

Hi,

 

I want each new incident created into the t_incidents table by the user to be associated with the AgencyID, a foreign key in the t_users table.

 

The problem is I do not know how this relationship will work and whether I will need a junction table.

 

The two tables are:

t_Users
+----------+----------+------------+
|UserID    | AgencyID |User name   |
+----------+----------+------------+
|1         | 1        |john        |
+----------+----------+------------+
|2         | 1        |andrew      |
+----------+----------+------------+


t_Agencies
+----------+------------+
|AgencyID  |agency name |
+----------+------------+
|1         |police      |
+----------+------------+
|2         |immigration |
+----------+------------+

I will appreciate your advice.

 

Joseph

Edited by josephbupe
Link to comment
Share on other sites

Put the id in a AgencyID column in the incident table. (as in your data model http://forums.phpfreaks.com/topic/291293-database-tables-relationship/?do=findComment&comment=1492162)

 

You would only need a "junction table" if you have a many-to-many relationship. ie both these statements are true

  • An incident is handled by many agencies
  • An agency handles many incidents
Link to comment
Share on other sites

 

Put the id in a AgencyID column in the incident table. (as in your data model http://forums.phpfreaks.com/topic/291293-database-tables-relationship/?do=findComment&comment=1492162)

 

You would only need a "junction table" if you have a many-to-many relationship. ie both these statements are true

  • An incident is handled by many agencies
  • An agency handles many incidents

 

 

 

Hi Barand,

 

Ok, Thanx.

 

And If understand you well, I will create a foreign key AgencyID in the t_incidents from the t_users table (which also has a foreign key AgencyID from t_agencies).

 

Is that so? Sorry, I just want to be clear on this one, noting also that I slightly departed from my previous table relationship between incidents and agencies after including users table.

 

Joseph

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.