Database 如何在没有圆的情况下建立多对多关系模型

Database 如何在没有圆的情况下建立多对多关系模型,database,relationship,geometry,Database,Relationship,Geometry,注意:因为我原来的问题不清楚,所以我写的是全新的 我的数据库中有两个表,外加一个连接/联接表: 食谱: 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 VARCH

注意:因为我原来的问题不清楚,所以我写的是全新的
我的数据库中有两个表,外加一个连接/联接表:
食谱:

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)
)
成分:

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)
)
IngCreditsRecipes:

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 Ingredient{
        private $id;
        private $name;
        private $recipes; //In which recipes this ingredient is used
}
class Recipe{
        private $id;
        private $name;
        private $ingredients; //Ingredients used in this Recipe
}
这是我的食谱课:

class Ingredient{
        private $id;
        private $name;
        private $recipes; //In which recipes this ingredient is used
}
class Recipe{
        private $id;
        private $name;
        private $ingredients; //Ingredients used in this Recipe
}
现在,当我想填充这两个列表时,我有以下问题: Recipe类有许多配料,而配料类有许多配方。每个类包含另一个类,我希望这个小图片可以说明这种情况

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

为多对多关系编写模型类的首选方法是什么?

这通常通过连接或映射表来完成,以保存两者之间的关系,例如:

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)
);

米奇说的。这通常被称为“联接表”。不过,为了明确其用途,我将其命名为。我知道联接表的概念,但那只是数据库站点,我想知道如何在代码中表示这种关系,或者我应该创建一个类RecipeCredit,然后创建一个RecipeCredit对象列表?也许你可以告诉我们你使用的是什么编程语言或ORM?我使用的PHP没有任何ORM或框架