我想要创建一个动态数据库
在此基础上需要步骤或流程。
如何通过存储查询或任何其他方式创建数据库。
我想用编程的方式。
1)健康食谱分类表
1)BreakFast
2)Lunch
3)Dinner
4)chicken & turkey
5)Dessert
...........2)早餐桌..。
- orange and vanilla protein oatmeal
-chili-chocolote protein oatmeal
.....3)橙子和香草蛋白燕麦片桌
-Ingredients
-directions谢谢你提前..。
发布于 2014-11-08 14:39:04
我知道你想使用SQLite。运行SQLite客户端(Sqliteman或类似程序),创建一个新的数据库,并以脚本的形式运行以下代码:
create table category (
category_id integer not null primary key,
name varchar(80) not null
);
create table meal (
meal_id integer not null primary key,
name varchar(80) not null,
directions text
);
create table meal_category (
meal_category_id integer primary key,
meal_id integer not null references meal,
category_id integer not null references category
);然后,您可以插入如下数据:
insert into category (category_id, name) values (1, 'Breakfast');
insert into category (category_id, name) values (2, 'Lunch');
insert into meal (meal_id, name) values (1, 'Orange and vanilla protein oatmeal');
insert into meal (meal_id, name) values (2, 'Chili-chocolote protein oatmeal');
insert into meal_category (meal_category_id, meal_id, category_id) values (1, 1, 1); -- meal 1 is a breakfast
insert into meal_category (meal_category_id, meal_id, category_id) values (2, 2, 1); -- meal 2 is a breakfast然后像这样查询:
select m.name || ' is ' || c.name from meal m
join meal_category mc on m.meal_id = mc.meal_id
join category c on mc.category_id = c.category_id;这是最简单的设计。您可能需要添加其他字段和一些索引--请查看有关SQL的教程。无论如何,上面的内容将为您提供一个工作的SQLite数据库。
您可能需要表“配料”,这将保存任何可用于食谱(鸡蛋,面粉,水等)的数据和"meal_ingredient“,将告诉是否应在一餐中的配料。食谱的文本可以保存在meal.recipe字段中。
请注意,有不同的方法来设计数据库,通常您应该提供一个详细的系统规范,它将使用数据库进行良好的设计。
最好是考虑一下数据库将用于什么,想从数据库中获取什么类型的数据,然后在SQL上读取,然后自己做一些实验。例如,如果你想寻找任何一种使用面粉的食物,最好把配料放在一个单独的桌子上,与食物相连--就像一个类别和一顿饭联系在一起一样,它被称为“多到多的关系”。但是,如果您不关心这样的功能,配方和配料列表都可以放在meal.recipe字段中。数据库的设计应该反映你的需求和你想要有一个模型的现实部分。
https://stackoverflow.com/questions/26817844
复制相似问题