tags:

views:

41

answers:

3

I'm having trouble wrapping my head around how to set this up. I have two tables:

  • Projects
  • Sections

1 project can belong to many sections.
1 section can have many projects.

  1. How would I go about setting this up, would this be a many to many relationship or one to many?
  2. Would it best if I created a lookup table?
+1  A: 

This is a simple 1-to-many relationship . The 2 requirements you stated above amount to the same thing. No lookup table is required. In your sections table you simply have a fk back to the projects table

ennuikiller
1-to-many would be if only projects could have many sections - not both ways.
OMG Ponies
The two requirements do not in fact "amount to the same thing". I read it the first time and thought they did, but:- 1 project can *belong to* many sections (where the project refers to the section), and- 1 section can have many projects *belonging to it* (where the section refers to the project).Hence, what you have is a many-to-many relationship, so a joining table is in order, like OMG Ponies suggests.
Lucas Richter
A: 

Create a relationship table... let's call it ProjectSections, with fields ProjectId and SectionId. You create the relationship between a project and a section in ProjectSections by creating a record with the ProjectId and the SectionId.

ProjectSections then links the Projects and Sections together.

Lee Harold
+2  A: 

would this be a many to many relationship or one to many?

It's regarded as many-to-many, because many projects can refer to many sections.

You'll need to implement a table that sits between projects and sections, using foreign keys from both to be the primary key. Example:

PROJECTS_SECTIONS_XREF table

  • PROJECT_ID, pk, fk
  • SECTION_ID, pk, fk

The naming convention is up to you, but I recommend something informational by using the names of both tables involved. Corrollary, cross references (xref), lookup, etc. There's been two questions regarding the naming convention in the past week or so - all the names are synonyms, choose whatever you like.

OMG Ponies