Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Database Foundations Reading Data from Databases with SQL Retrieving a Result Set

Insert values in junction table for many to many relationships (MySQL)?

I'm stuck and confused.

I have three tables in a many to many relationship...

PROJECTS [projects_id] [name]

UNDERGROUND [underground_id] [underground_name]

PROJECTS_UNDERGROUND [projects_id_fk] [underground_id_fk]

ā€”

Both Projects and Underground have data pre-populated, but I'm confused as to how to connect the data via the junction

The junction table has both foreign keys acting as a composite key to avoid duplicates, but what is the process / query to link the data?.

I know it's an INSERT statement into the PROJECTS_UNDERGROUND table ā€” but ultimately, I need the project_id (from PROJECTS table) to be associated with the underground_name (from the UNDERGROUND table).

Sorry if this is confusing? But I greatly appreciate your help offered.

Moses Gangipogu
Moses Gangipogu
3,772 Points

hi philip can you please rephrase the question or get your data dumped in here http://sqlfiddle.com/ and give us link to it

1 Answer

Alexander Schott
Alexander Schott
8,318 Points

You have to manually insert the values to make the connection. You say both other tables are populated. Check these tables out and make the connection by INSERT INTO PROJECTS_UNDERGROUND VALUE (2,3); for example. These connects the second project with the third underground. I dont understand what you really mean. Do you think you can write a fancy INSERT Statement to automatically make the connection? That cant work because only you know which record from project refers over the junction table to an underground and vice versa.