这是我第一次尝试将更复杂的对象存储到数据库中。我需要一些关于数据库设计的帮助。
我要存储并从数据库中重新生成的配方对象
{
"id": 2345,
"name": "cake",
"description": "yummy cake",
"categorys": [
17,
26
],
"persons": 4,
"author": 26,
"language": "de",
"unit": "en",
"variantOf": 34,
"specialTools": [
34,
44,
10
],
"img": "32598734.jpg",
"estTime": 2777,
"steps": {
"1": {
"title": "mix",
"description": "mix all together",
"img": "45854.jpg",
"timer": null,
"ingredients": [
{
"name": "Butter",
"color": "#227799",
"amount": 150,
"unit": "g"
},
{
"name": "egg",
"color": "#aaff22",
"amount": 3,
"unit": "pc"
},
{
"name": "sugar",
"color": "#22ffff",
"amount": 50,
"unit": "g"
}
]
},
"2": {
"title": "bake",
"description": "put it in the oven",
"img": null,
"timer": 2400,
"ingredients": [
{
"name": "butter",
"color": "#227799",
"amount": null,
"unit": null
},
{
"name": "sugar",
"color": "#22ffff",
"amount": null,
"unit": null
},
{
"name": "egg",
"color": "#aaff22",
"amount": null,
"unit": null
}
]
}
}
}
最复杂的部分是steps对象。每个配方可以有不同数量的步骤,每个步骤分配不同的成分。
这是我做
的一个数据库设计
recipe_id, step_id是外键。我想要不同表格中的所有内容,因为食谱应该可以按成分、类别...
用于生成最重要表的SQL 代码
-------------------------------------------------- -----
-- 表`dev_Recipe`.`recipe`
-------------------------------------------------- -----
如果不存在 `dev_Recipe`.`recipe`,则创建表(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(255) NULL ,
`描述` TEXT NULL ,
`author_id` INT UNSIGNED NOT NULL ,
主键 (`id`) ,
索引 `author_id_idx` (`author_id` ASC) ,
约束`author_id`
外键(`author_id`)
参考 `dev_Recipe`.`users` (`id`)
删除无操作
更新无动作)
引擎 = InnoDB;
-------------------------------------------------- -----
-- 表`dev_Recipe`.`step`
-------------------------------------------------- -----
如果不存在 `dev_Recipe`.`step`,则创建表(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`recipe_id` INT UNSIGNED NOT NULL ,
`step_number` INT UNSIGNED NOT NULL ,
`描述` TEXT NULL ,
`timer` INT UNSIGNED NULL ,
`image` VARCHAR(100) NULL ,
主键 (`id`) ,
索引 `recipe_id_idx` (`recipe_id` ASC) ,
约束`step_recipe_id`
外键(`recipe_id`)
参考 `dev_Recipe`.`recipe` (`id`)
删除无操作
更新无动作)
引擎 = InnoDB;
-------------------------------------------------- -----
-- 表`dev_Recipe`.`ingredient`
-------------------------------------------------- -----
如果不存在 `dev_Recipe`.`ingredient`,则创建表(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`颜色` INT NOT NULL ,
`img` VARCHAR(45) NULL ,
主键 (`id`) )
引擎 = InnoDB;
-------------------------------------------------- -----
-- 表`dev_Recipe`.`step_ingredients`
-------------------------------------------------- -----
如果不存在 `dev_Recipe`.`step_ingredients`,则创建表(
`recipe_id` INT UNSIGNED NOT NULL ,
`ingredient_id` INT UNSIGNED NOT NULL ,
`step_id` INT UNSIGNED NOT NULL ,
`数量` INT NULL ,
`unit` VARCHAR(25) NULL ,
索引 `recipe_id_idx` (`recipe_id` ASC) ,
索引`ingredient_id_idx` (`ingredient_id` ASC) ,
索引 `step_id_idx` (`step_id` ASC) ,
主键(`recipe_id`,`step_id`),
约束`step_ing_recipe_id`
外键(`recipe_id`)
参考 `dev_Recipe`.`recipe` (`id`)
删除无操作
更新无动作,
约束`ingredient_step_ing_id`
外键(`ingredient_id`)
参考 `dev_Recipe`.`ingredient` (`id`)
删除无操作
更新无动作,
约束`step_ing_id`
外键(`step_id`)
参考 `dev_Recipe`.`step` (`id`)
删除无操作
更新无动作)
引擎 = InnoDB;
由于我以前从未做过连接表,我不知道这是否是解决我的问题的正确方法。这是一个合理的设计以及如何优化它?
我做了另一个设计,在哪里recipes加入step和step。ingredients我认为第一个布局更容易查询,因为我只能通过ingredients_id recipe_id查看来搜索step_ingredients,但我不确定。有什么想法吗?
