josephbupe Posted October 13, 2014 Share Posted October 13, 2014 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 Link to comment https://forums.phpfreaks.com/topic/291604-mysql-tables-relationship-with-a-foreign-key-on-parent-table/ Share on other sites More sharing options...
Barand Posted October 13, 2014 Share Posted October 13, 2014 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 https://forums.phpfreaks.com/topic/291604-mysql-tables-relationship-with-a-foreign-key-on-parent-table/#findComment-1493447 Share on other sites More sharing options...
josephbupe Posted October 13, 2014 Author Share Posted October 13, 2014 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 https://forums.phpfreaks.com/topic/291604-mysql-tables-relationship-with-a-foreign-key-on-parent-table/#findComment-1493450 Share on other sites More sharing options...
Barand Posted October 13, 2014 Share Posted October 13, 2014 The one you already have should suffice Link to comment https://forums.phpfreaks.com/topic/291604-mysql-tables-relationship-with-a-foreign-key-on-parent-table/#findComment-1493451 Share on other sites More sharing options...
josephbupe Posted October 13, 2014 Author Share Posted October 13, 2014 Ok, Barand. I will stick to your suggestion. Again thank you. Joseph Link to comment https://forums.phpfreaks.com/topic/291604-mysql-tables-relationship-with-a-foreign-key-on-parent-table/#findComment-1493455 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.