如何为没有圆圈的多对多关系建模
How to model many to many relationships without circles
注意:因为我原来的问题没看清楚所以我重新写!
我的数据库中有两个 table,外加一个 junction/join table:
食谱:
CREATE TABLE Recipes(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
)
成分:
CREATE TABLE Ingredients(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
)
成分食谱:
CREATE TABLE IngredientsRecipes(
id INT(11) NOT NULL AUTO_INCREMENT,
recipeId INT(11) NOT NULL,
ingredientId INT(11) NOT NULL,
PRIMARY KEY (id)
)
我在 php 代码中的成分 class 如下所示:
class Ingredient{
private $id;
private $name;
private $recipes; //In which recipes this ingredient is used
}
这是我的食谱class:
class Recipe{
private $id;
private $name;
private $ingredients; //Ingredients used in this Recipe
}
现在,当我想要填充这两个列表时,我遇到了以下问题:
食谱 class 有很多配料,配料 class 有很多食谱。每个 class 包含包含另一个,我希望这张小图可以说明情况。
Recipe | Ingredients | Recipes using |
|used in Recipe | this Ingredient |
----------------+---------------+-----------------+
|--Noodles------|Spaghetti
|
Spaghetti-------|--Sauce--------|--Spaghetti
|
|--Cheese-------|--Spaghetti
|
|--Mac n Cheese
|--Macaroni-----|Mac n Cheese
|
Mac n Cheese----|--Cheese-------|--Spaghetti
|
|--Mac n Cheese
编写多对多关系模型 class 的首选方法是什么?
这通常是通过连接或映射 table 来保持两者之间的关系,例如:
CREATE TABLE recipe (
recipe_id NUMERIC PRIMARY KEY
recipe_name VARCHAR (100)
-- etc...
);
CREATE TABLE ingredient (
ingredient_id NUMERIC PRIMARY KEY
ingredient_name VARCHAR (10),
-- etc...
);
CREATE TABLE recipe_ingredient (
recipe_id NUMERIC REFERENCES recipe (recipe_id),
ingredient_id NUMERIC REFERENCES ingredient (ingredient_id),
PRIMARY KEY (recipe_id, ingredient_id)
);
注意:因为我原来的问题没看清楚所以我重新写!
我的数据库中有两个 table,外加一个 junction/join table:
食谱:
CREATE TABLE Recipes(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
)
成分:
CREATE TABLE Ingredients(
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
)
成分食谱:
CREATE TABLE IngredientsRecipes(
id INT(11) NOT NULL AUTO_INCREMENT,
recipeId INT(11) NOT NULL,
ingredientId INT(11) NOT NULL,
PRIMARY KEY (id)
)
我在 php 代码中的成分 class 如下所示:
class Ingredient{
private $id;
private $name;
private $recipes; //In which recipes this ingredient is used
}
这是我的食谱class:
class Recipe{
private $id;
private $name;
private $ingredients; //Ingredients used in this Recipe
}
现在,当我想要填充这两个列表时,我遇到了以下问题: 食谱 class 有很多配料,配料 class 有很多食谱。每个 class 包含包含另一个,我希望这张小图可以说明情况。
Recipe | Ingredients | Recipes using |
|used in Recipe | this Ingredient |
----------------+---------------+-----------------+
|--Noodles------|Spaghetti
|
Spaghetti-------|--Sauce--------|--Spaghetti
|
|--Cheese-------|--Spaghetti
|
|--Mac n Cheese
|--Macaroni-----|Mac n Cheese
|
Mac n Cheese----|--Cheese-------|--Spaghetti
|
|--Mac n Cheese
编写多对多关系模型 class 的首选方法是什么?
这通常是通过连接或映射 table 来保持两者之间的关系,例如:
CREATE TABLE recipe (
recipe_id NUMERIC PRIMARY KEY
recipe_name VARCHAR (100)
-- etc...
);
CREATE TABLE ingredient (
ingredient_id NUMERIC PRIMARY KEY
ingredient_name VARCHAR (10),
-- etc...
);
CREATE TABLE recipe_ingredient (
recipe_id NUMERIC REFERENCES recipe (recipe_id),
ingredient_id NUMERIC REFERENCES ingredient (ingredient_id),
PRIMARY KEY (recipe_id, ingredient_id)
);