简体   繁体   English

我应该如何构建数据库?

[英]How should I structure my database?

I'm a complete beginner to SQL. 我是SQL的完整入门者。 I currently have "coders block" and need help pushing forward. 我目前有“编码器阻止程序”,需要帮助来推动。


I am unsure how I should structure my database. 我不确定如何构造数据库。 I've included an image to hopefully give you a better picture of what I am trying to achieve. 我提供了一张图片,希望可以使您更好地了解我要实现的目标。 Please take a look at it 请看一下

在此处输入图片说明


There will be multiple shops which contain multiple items. 将有多个商店,其中包含多个项目。 Each item will have a buying price and a selling price. 每个项目都有一个购买价格和一个销售价格。 An item may be used by multiple shops, and may have a different buying/selling price from other shops. 一个项目可能被多个商店使用,并且可能具有与其他商店不同的购买/销售价格。 My goal in creating this database is to be able to query an item and determine which shop(s) are buying or selling it at the lowest price. 我创建此数据库的目标是能够查询商品并确定哪些商店以最低价格购买或出售该商品。


EDIT: 编辑:

My idea is to create three tables: Items, Stores, and ItemsStores. 我的想法是创建三个表:Items,Stores和ItemsStores。

Stores will have columns ID, Name 商店将具有列ID,名称

Items will have columns ID, Name 项目将具有列ID,名称

ItemsStores will have columns Store, Item, Buy Price, Sell Price. ItemsStore将具有“ 商店”,“项目”,“购买价格”,“出售价格”列。

I feel creating a column for just an ID and a name may be redundant. 我觉得仅为ID和名称创建一列可能是多余的。 Am I wrong? 我错了吗? Is there a better way to do this? 有一个更好的方法吗?


I've read a few articles and even watched videos on the basics of structuring databases, but for some reason it's just not clicking with me. 我已经阅读了几篇文章 ,甚至还观看有关构建数据库基础知识的视频 ,但是由于某种原因,它只是没有点击而已。 Any help on what my tables / columns should look like would be greatly appreciated. 任何有关我的表/列的外观的帮助将不胜感激。

When you start designing a database, you need to think of the question by highlighting the objects that needs a table. 在开始设计数据库时,需要通过突出显示需要表的对象来思考问题。 The more the merrier (surprise surprise). 越多越好(惊喜)。 So this is how I would write down the problem: 所以这就是我写下问题的方式:
There will be multiple shops which contain multiple items . 将有多个商店包含多个项目 Each item will have a buying price and a selling price . 每个项目都有一个购买价格和一个销售价格 An item may be used by multiple shops, and may have a different buying/selling price from other shops. 一个项目可能被多个商店使用,并且可能具有与其他商店不同的购买/销售价格。 My goal in creating this database is to be able to query an item and determine which shop(s) are buying or selling it at the lowest price. 我创建此数据库的目标是能够查询商品并确定哪些商店以最低价格购买或出售该商品。

multiple is highlighted because that means a many to many relationship 突出显示倍数,因为这意味着多对多关系

obviously you need to decide (that's the tricky part) where you want the price to be: is price an attribute of item? 显然,您需要确定(这是棘手的部分)您希望价格位于哪里:价格是商品的属性吗? or of an item in a shop? 或商店里的物品?

Once your head stops spinning and you figure that one out you'll be in a much better position. 一旦您的头停止旋转,您就会发现自己会处于一个更好的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM