tags:

views:

16

answers:

1
id | element
----------
 1 | Milk
 2 | Bread
 3 | Butter

Milk and Butter have "connection". And Bread and Butter have "connection". These are even connection, not parent-child.

How can i display them?

id1 | id2
----------
 1  |  3
 2  |  3

yes, but

id1 | id2
----------
 3  |  1
 3  |  2

also right, there are no first and second.

How can this be done?

+3  A: 

To demonstrate, I'm going to invent the scenario that you're talking about recipes and ingredients. Perhaps you're not. Either way, you want to make a 'set' which contains any number of the 'items' in a many-to-many relationship.

Make a table called a "RECIPE" or similar, which looks like this:

  • RECIPE_ID
  • NAME

And another one called RECIPE_INGREDIENTS

  • RECIPE_ID
  • INGREDIENT_ID

Then for your example of bread and butter, in RECIPE:

ID | Name
-----------------
1  | Sandwich

And in ingredients

RECIPE_ID | INGREDIENT_ID
1         | 2
1         | 3

For something made up of Bread and Milk

ID | Name
-----------------
2  | Milky Bread

And

RECIPE_ID | INGREDIENT_ID
2         | 1
2         | 2
Michael Shimmins
so i should create 'dumb' table with connections? equivalent to RECIPE table?
Qiao
+1 This is First Normal Form, avoiding repeating groups.
Bill Karwin
@Qiao - in my example the dumb table is actually the one named "RECIPIE_INGREDIENTS". That is what creates the connection that tells you which ingredients are connected to each other for each connection. Since Butter has two connections, one to bread and one to milk, I felt it appropriate to add an identifier to each of these distinct connections - in this instance I used the metaphor of a recipe to identify them. I created the RECIPE table, rather than habing a third column in the connections table, so that (as Bill Karwin pointed out) it was in first normal form.
Michael Shimmins