创建一个简单的(但不适合我)数据库

发布于 2024-10-19 04:22:54 字数 1204 浏览 2 评论 0原文

我正在尝试建立一个数据库来跟踪小型 craigslist 销售业务中发生的情况。

基本上这些人的操作方式就是这样。

  1. 假设有人以每本 10 美元的价格购买 3 本书。

  2. 他们将其发布在 CL 上

  3. 他们以 20 美元的价格出售。

  4. 每售出一本书,购买者可获得 10 美元的返还,外加 1/3 的利润。

问题是他们每次几乎都买同一本书,但他们购买的价格却发生了变化。有时,他们以 12 美元或 15 美元的价格购买这本书。如果市场对他们有利的话,他们会购买其他书籍。

我想要做的是建立和库存清单,通过书籍的名称和购买价格来跟踪书籍。

因此,如果我们有 10 本“绿鸡蛋和火腿”书,每本售价 10 美元,而我们有 5 本“绿鸡蛋和火腿”书,每本售价 15 美元。库存清单应反映两个单独的项目。这样投资者的投资就能得到适当的报酬。

我在以这种方式设计数据库时遇到困难。我有以下表格:

产品

  • ID PK
  • 名称
  • 说明

库存

  • ID
  • 购买日期
  • Product_ID PK
  • 购买价格 PK
  • 数量

采购

  • ID PK
  • Product_ID
  • Product_Quanity
  • Purchase_Price

到目前为止,我的推理表明我应该在产品表中跟踪他们正在销售的商品类型。还要跟踪他们购买商品的时间以及在给定时间购买该商品的数量;因此就有了采购表。然后我想建立一个库存清单,但每条记录都需要根据商品及其成本来唯一。所以我决定使用购买价格和产品 ID 的复合键来做到这一点。

如果字段不是主键,是否可以将采购表中的购买价格和产品 ID 关联到我的库存表?到目前为止,我的方法适合我想要实现的目标吗?对于我想做的事情有更好的方法吗?

数据库还有其他元素我没有提到,但如果有必要的话会提及

感谢所有可以帮助我的人。

I am trying to build a db to track the going-ons within a small craigslist selling operation.

Basically how the guys are operating is like so.

  1. some one buys lets say 3 books at a price of $10 each.

  2. They post it on CL

  3. And they sell it for $20.

  4. The purchaser gets his $10 back for each book sold, plus 1/3 of the profits.

The problem is they are pretty much buying same book each time, but the price they are buying it at changes. Sometimes, they buy the book for $12, at other $15. They will buy other books, if the market is good on them.

Want I'm wanting to do is build and inventory list that tracks the books by the its name, and by the price they were purchased at.

So if we have 10 "Green Eggs and Ham" books, that cost of $10 a piece, and we have 5 "Green Egg and Ham" Books that cost $15. the inventory list should reflect two separate items. This is so that the investor gets paid appropriately for his investment.

I am having trouble dessigning the db in this fashion. I have the following tables:

Product

  • ID PK
  • Name
  • Description

Inventory

  • ID
  • Purchase Date
  • Product_ID PK
  • Purchase_Price PK
  • Quantity

Purchasing

  • ID PK
  • Product_ID
  • Product_Quanity
  • Purchase_Price

My reasoning so far suggest that i should keep track of the types items they are selling, in a products table. Also keep track of when they purchase an item, and how many they purchase of that item at a given time; hence the purchasing table. Then I want to build a list of what i have in inventory, but each record needs to unique based on the item and its cost. So i decided that I would use a composite Key of purchase price, and Product Id to do that.

Is it possible to use relate the purchase price, and the Product ID from the Purchasing table to my Inventory table, if the fields aren't Primary Keys? Is my method so far appropriate for what I am trying to accomplish? Is there a better method for what I am try to do.

there are other elements to the db that I haven't mentioned, but will if they are necessary

Thanks to all who can help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昔梦 2024-10-26 04:22:54

产品

  • ID PK
  • 产品名称
  • 描述

购买

  • ID PK
  • 产品ID FK
  • 购买日期
  • 数量
  • 购买价格

>Sale

  • ID PK
  • ProductID FK
  • SaleDate
  • Quantity
  • SalePrice

Inventory

SELECT ID, ProductName, 
       (SELECT Sum(Quantity) FROM Purchase WHERE ProductID=Product.ID) -
       (SELECT Sum(Quantity) FROM Sale WHERE ProductID=Product.ID) AS NumInStock
FROM Product

以上查询的作用是从产品中返回 ID 和 ProductName表以及计算该商品的库存数量。第一个子查询 (SELECT Sum(Quantity) FROM Purchase WHERE ProductID=Product.ID) 返回已购买的所有匹配商品的总和。然后,它从该总数中减去已售出的所有匹配商品的总和(SELECT Sum(Quantity) FROM Sale WHERE ProductID=Product.ID),得出库存商品。

要获取以 100 美元购买的 iPad 数量,您可以使用如下查询:

SELECT Sum(Quantity) AS NumiPadsFor100
FROM Purchase WHERE PurchPrice = 100

要获取特定商品的利润,在查询中执行所有操作会有点麻烦,但在代码中执行会很简单。

Product

  • ID PK
  • ProductName
  • Description

Purchase

  • ID PK
  • ProductID FK
  • PurchDate
  • Quantity
  • PurchPrice

Sale

  • ID PK
  • ProductID FK
  • SaleDate
  • Quantity
  • SalePrice

Inventory

SELECT ID, ProductName, 
       (SELECT Sum(Quantity) FROM Purchase WHERE ProductID=Product.ID) -
       (SELECT Sum(Quantity) FROM Sale WHERE ProductID=Product.ID) AS NumInStock
FROM Product

What the above query is doing is returning the ID and ProductName from the Product table as well as calculating the Number of that item in stock. The first subquery (SELECT Sum(Quantity) FROM Purchase WHERE ProductID=Product.ID) returns the sum of all matching items that have been purchased. It then subtracts from that total the sum of all matching items that have been sold (SELECT Sum(Quantity) FROM Sale WHERE ProductID=Product.ID) to come up with what is in stock.

To get the number of iPads purchased at $100, you could use a query like this:

SELECT Sum(Quantity) AS NumiPadsFor100
FROM Purchase WHERE PurchPrice = 100

To get the profit on a particular item, that would be a bit more cumbersome to do all in queries, but would be straightforward to do in code.

遗心遗梦遗幸福 2024-10-26 04:22:54

那么,在产品入库之前应该先购买,对吗?只需使用 PurchasingID 作为 Inventory 表中的外键 - 这将消除在两个表中使用 buy_price 的需要。
编辑:
它将是这样的:
产品
身份PK
姓名
库存描述

身份PK
购买日期
数量
采购_ID FK

采购
身份PK
产品_ID FK
产品_数量
Purchase_Price

根据您的需求,您可能需要在采购和库存之间添加一个额外的表

So, Before the product goes to inventory it should be purchase right? Just use PurchasingID as a foreign key in Inventory table - this will eliminate the need of using purchase_price in both tables.
EDIT:
It will be like this:
Product
ID PK
Name
Description

Inventory
ID PK
Purchase Date
Quantity
Purchasing_ID FK

Purchasing
ID PK
Product_ID FK
Product_Quanity
Purchase_Price

You probably will need an extra table between purchasing and inventory bassed on your needs

爱要勇敢去追 2024-10-26 04:22:54

1 个产品表和 1 个变动表(销售和采购)。这样你的库存就会非常容易。如果流量很大(假设每年超过 50,000 次移动),您可能需要查看这项有趣的研究

1 table for Products, and 1 table for movements (sales as well as purchases). This way your inventory will be very easy. If have large volumes (let's say more than 50.000 movements a year) you may want to look at this interesting study.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文