webdevRefinery Forum: Can anyone advise me on many to many relationships for my ERM? - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

User is offline deucalion0 

  • Group: Members
  • Posts: 183
  • Joined: 05-September 10
  • LocationDundee, Scotland

Posted 29 December 2011 - 11:03 AM (#1)

Can anyone advise me on many to many relationships for my ERM?


I have created my ERM, I was using the conolly and Begg 9 steps, and have realised that at step 7 I need to create a junction table between my many to many tables. I am unsure of why exactly and how to do this correctly, can anyone provide some help with this? Here is my ERM diagram:

my ERM

I appreciate any of your expertise advise on this as all information I could find seemed to be lacking in a full explanation.

Many thanks.
0


User is offline Starblaster100 

  • Group: Members
  • Posts: 211
  • Joined: 08-March 10
  • LocationEngland
  • Expertise:HTML,CSS,PHP,Javascript,SQL

Posted 29 December 2011 - 11:22 AM (#2)

If I'm understanding you correctly, you're having trouble between the Customers and Addresses Table?

I would add the foreign key Customer ID into the Addresses Table (rather than have the Address ID as a foreign key in the Customer table) and then have the Foreign Key AddressID in the Orders Table.

Order Table
OrderID (PK)
CustomerID (FK)
AddressID (FK)
etc.

Customers
As you have but no AddressID (FK)

Address
As you have +
CustomerID (FK)


If I've miunderstood the problem, please let me know, and we can try again :)
0


User is offline Kyek 

  • Founder of wdR
  • Group: Administrators
  • Posts: 5081
  • Joined: 20-February 10
  • LocationPhiladelphia, PA, USA
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 29 December 2011 - 11:28 AM (#3)

Edit: I got starblasted D:

It sounds complicated, but it's actually very easy :). Without spending more time reading your schema, I'm not sure which tables are many-to-many -- but imagine for a moment that products can be placed in more than one category. So Categories can have many Products, and Products can have many Categories. Many to many. Your junction table looks like this:

+----+------------+-------------+
| id | product_id | category_id |
+----+------------+-------------+
| 1  | 479        | 2           |
| 2  | 479        | 4           |
| 3  | 512        | 2           |
| .. | ...        | ...         |
+----+------------+-------------+


The 'id' field is optional -- you could also do this by making your primary key a dual-column key. That's all personal preference :). Either way you do it, make sure you have INDIVIDUAL indexes on your product_id and category_id fields.

And that's it! You just put a row in there for every relationship. In the example above, product #479 belongs to category 2 and 4. Category 2 contains two different products. All you need to do to get a list of all the products in a certain category is JOIN this table to the products table, and select any rows that match the category ID you need. Do the opposite to find the categories that belong to a certain product :)

Good luck!
0


User is offline deucalion0 

  • Group: Members
  • Posts: 183
  • Joined: 05-September 10
  • LocationDundee, Scotland

Posted 29 December 2011 - 11:30 AM (#4)

Hey Starblaster100, thanks for your reply. I think there has been a small misunderstanding. :)

I have five many to many relationships in my ERM diagram. according to Connelly and Begg I must have another table in between any tables that have a many to many relationship. Apparently you create a new table with a name and give it the primary key of both tables and also teh foreign key of each as well. So I must then have to do this five times increasing the size of my ERM from 7 tables to 12.

I was hoping fro clarification on this, and how to do it and why.

Does that make sense?

Thanks!
0


User is offline Starblaster100 

  • Group: Members
  • Posts: 211
  • Joined: 08-March 10
  • LocationEngland
  • Expertise:HTML,CSS,PHP,Javascript,SQL

Posted 29 December 2011 - 11:33 AM (#5)

I see, I think I jumped the gun a bit there! (I was thinking more along the lines of: Addresses should really be attached to an Order - if I have multiple addresses, how would you know which to post to?!)
See Kyek's post above, it explains how to use table mappings very nicely :)
0


User is offline deucalion0 

  • Group: Members
  • Posts: 183
  • Joined: 05-September 10
  • LocationDundee, Scotland

Posted 29 December 2011 - 11:44 AM (#6)

Ah Kyek, I never saw your post. OK, I think I understand your logic.What I was thinking about was; if I have a new middle table what exactly is the relationship with the two table with the new one, is it a 1 to many relationship now? I was always getting confused, because I split products up into its own table, this will contain every single product, but these products can be dividing into categories, and these into a range. So a MENS BLUE PULLOVER is a product of category type MENS and the range is within the PULLOVER range, if this makes sense? It has been a long process doing all this trust me! :)

Thanks!
0


User is offline Starblaster100 

  • Group: Members
  • Posts: 211
  • Joined: 08-March 10
  • LocationEngland
  • Expertise:HTML,CSS,PHP,Javascript,SQL

Posted 29 December 2011 - 12:24 PM (#7)

Expanding on the example given above - Say we have 3 tables now:

The 2 existings ones:
Product
Category

And our new Mapping Table:
ProductCategoryMap

Product to ProductCategoryMap = 1 to many relationship.
Category to ProductCategoryMap = 1 to many relationship.



So initially, your ERM shows :
Product = *..* = Category

We now have
Product = 1..* = ProductCategoryMap = *..1 = Category


Essentially we have turned a many to many relationship into a 1 to many to 1. This intermediate table allows us to combine products and categories into as many different pairing as we want.


You'd do a similar thing for the Range table to the Cateogry table.

Category = 1..* = CategoryRangeMap = *..1 = Range.

This again will allow you to combine as many categories and ranges into as many pairings as you want.
0


User is offline deucalion0 

  • Group: Members
  • Posts: 183
  • Joined: 05-September 10
  • LocationDundee, Scotland

Posted 29 December 2011 - 02:42 PM (#8)

starblaster that's an excellent breakdown, thanks. I just have one last query, do I use the primary key from product and category and merge them as a composite primary key in the new table? Also how does this then work with creating a foreign key? If I could just figure out the key part then I am done I think!

Thanks!
0


User is offline Starblaster100 

  • Group: Members
  • Posts: 211
  • Joined: 08-March 10
  • LocationEngland
  • Expertise:HTML,CSS,PHP,Javascript,SQL

Posted 29 December 2011 - 07:25 PM (#9)

2 parts to your question, lets break it down:

View Postdeucalion0, on 29 December 2011 - 02:42 PM, said:

starblaster that's an excellent breakdown, thanks. I just have one last query, do I use the primary key from product and category and merge them as a composite primary key in the new table? Also how does this then work with creating a foreign key? If I could just figure out the key part then I am done I think!

Thanks!


Regardless of whether we choose to create an identity column (I'm in favour of doing so), we still need to at least create a primary key, so ...

We will probably want to create a composite primary key on these mapping tables. Why? It will give us a single column with a unique constraint on the 2 columns we are referencing, and will be indexed. Regardless, there are 2 approaches.

Take Kyek's example:

+----+------------+-------------+
| id | product_id | category_id |
+----+------------+-------------+
| 1  | 1          | 2           |
| 2  | 1          | 4           |
| 3  | 2          | 2           |
| .. | ...        | ...         |
+----+------------+-------------+


Approach 1:

Problem 1: We need a primary key
Solution 1: Our identity column, "id", acts as the primary key.

Problem 2: Product 1 is in Category 2 and 4. Once we know this, there is no need to ever insert Product 1 into Category 2 or 4 again. We want to stop inserting a certain product into a category it is already defined in it.
Solution 2: Create a unique constraint on these 2 columns.

OR Approach 2:

Solution 1 and 2: Create a Primary Composite Key on product_id and category_id. This will give us that unique constraint, as well as act as the identity column and primary key for the table. (In some databases, composite prmary keys = unique constraint)

The difference?

Approach 1 has a separate identity column (primary key, and indexed), with a unique constraint on the other 2 columns.

Approach 2 has no identity column, with the unique constraint acting on both columns (indexed, both primary composite)

I hope I explained that clearly?!

View Postdeucalion0, on 29 December 2011 - 02:42 PM, said:

starblaster that's an excellent breakdown, thanks. I just have one last query, do I use the primary key from product and category and merge them as a composite primary key in the new table? Also how does this then work with creating a foreign key? If I could just figure out the key part then I am done I think!

Thanks!


Create foreign keys as usual, referencing the correct columns in the other tables. This doesn't change whether we are using composite primary keys or not.


Hope that helps!


Edited for clarity and spelling

This post has been edited by Starblaster100: 29 December 2011 - 07:36 PM

0


User is online Daniel15 

  • dan.cx
  • Group: Moderators
  • Posts: 3435
  • Joined: 17-April 10
  • LocationMelbourne, Australia
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 29 December 2011 - 08:19 PM (#10)

The table in between the two tables involved in a many-to-many relationship is a weak entity called a junction table. To name this table, I normally combine the two table names (eg. CategoryProduct, CustomerAddress)

Also be careful of singular vs plural - Your ERD has some singular (eg. Category and Range) and some plural (eg. Products and Customers). This will often confuse you if you create the database and start writing queries for it :)

Quote

do I use the primary key from product and category and merge them as a composite primary key in the new table?

Yep - This is what I'd do :)
Daniel15! :D
Posted Image

Repeat after me: jQuery is not JavaScript. It is not the answer to every JavaScript-related question. When you have to write some JavaScript, do not instantly react with "Oh, I'll do that with jQuery!"

Spoiler
0


User is offline Scipio 

  • Group: Members
  • Posts: 27
  • Joined: 23-April 10
  • Expertise:HTML,CSS

Posted 01 January 2012 - 05:22 AM (#11)

Maybe you should use a database modelling software like mysql workbench, it's quite handy at solving issues like this.
0


User is online Daniel15 

  • dan.cx
  • Group: Moderators
  • Posts: 3435
  • Joined: 17-April 10
  • LocationMelbourne, Australia
  • Expertise:HTML,CSS,PHP,Java,Javascript,Node.js,SQL

Posted 01 January 2012 - 05:27 PM (#12)

You need to understand how things are done before using modelling software :)
Daniel15! :D
Posted Image

Repeat after me: jQuery is not JavaScript. It is not the answer to every JavaScript-related question. When you have to write some JavaScript, do not instantly react with "Oh, I'll do that with jQuery!"

Spoiler
0


User is offline deucalion0 

  • Group: Members
  • Posts: 183
  • Joined: 05-September 10
  • LocationDundee, Scotland

Posted 02 January 2012 - 06:47 AM (#13)

Thanks a lot guys, many tips there, a great help to me! I always get confused when my learning materials are disorganised! I am gonna have to rethink my whole ERM now I think, I am even thinking that Products category and range are not a many to many relationship now. Ah well the joys of learning!

Thanks again guys for all your input, its appreciated.
0


User is offline Brewed 

  • Group: Members
  • Posts: 1
  • Joined: 03-January 12

Posted 03 January 2012 - 12:58 PM (#14)

This has been quite helpful. A stab in the dark but I'm going to guess this is for www.viga.co.uk... And looking at the link location you're currently a student at Abertay and this might even be your coursework for Database and Internet Application Design.

I'm doing the same thing, I googled Connolly & Begg 9 step process and found this :lol:
0


User is offline deucalion0 

  • Group: Members
  • Posts: 183
  • Joined: 05-September 10
  • LocationDundee, Scotland

Posted 10 January 2012 - 06:38 PM (#15)

View PostBrewed, on 03 January 2012 - 12:58 PM, said:

This has been quite helpful. A stab in the dark but I'm going to guess this is for www.viga.co.uk... And looking at the link location you're currently a student at Abertay and this might even be your coursework for Database and Internet Application Design.

I'm doing the same thing, I googled Connolly & Begg 9 step process and found this :lol:



Haha yes you are right on all accounts, I am at Abertay. This forum is the best you will find for help with anything as long as you try and figure things out yourself first.

This coursework is gonna be huge....
0


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services