如何改进字段名称

发布于 2024-12-09 01:53:52 字数 2455 浏览 2 评论 0原文

我正在尝试改进字段名称,使其含义更加明显。字段名称可以吗?或者可以改变什么以使其更加明显?

mysql> select * from order_items;
+----+----------+--------+----------+------------+-------+
| id | order_id | name   | quantity | item_price | total |
+----+----------+--------+----------+------------+-------+
|  1 |        5 | Item 1 |        2 |       3.00 |  6.00 |
|  2 |        5 | Item 2 |        1 |       2.00 |  2.00 |
+----+----------+--------+----------+------------+-------+

mysql> select * from orders;
+----+---------+---------+-----------------+---------------+----------------+----------+---------------+------------+----------------+------------+----------------------+--------------------+--------------------+----------------------+
| id | shop_id | user_id | shipping_method | shipping_fees | payment_method | card_fee | commision_fee | item_total | customer_total | shop_total | shop_total_remaining | our_commission_net | our_commission_vat | our_commission_gross |
+----+---------+---------+-----------------+---------------+----------------+----------+---------------+------------+----------------+------------+----------------------+--------------------+--------------------+----------------------+
|  5 |      29 |       9 | delivery        |          1.00 | card           |     0.50 |            13 |       8.00 |           9.50 |       9.00 |                 7.83 |               1.17 |               0.23 |                 1.40 |
+----+---------+---------+-----------------+---------------+----------------+----------+---------------+------------+----------------+------------+----------------------+--------------------+--------------------+----------------------+

字段名称说明:

  • item_total order_items.order_id = 5 的总费用

  • customer_total 客户支付的总费用 (item_total + card_fee + shipping_fee)

  • shop_total 商店的总订单 (item_total + shipping_fees)

  • shop_total_remaining 需要偿还给商店的剩余总额(shop_total - our_commission_net)

  • our_commission_net 我将收取的佣金 (commission_fee * shop_total / 100)

  • our_commission_gross 佣金含增值税 (our_commission_net + our_commission_vat)

我是否添加了不必要的字段?

我用PHP来计算成本。

I am trying to improve the fields name to be more obvious what it means.. is the fields name ok? or what can be changed to make it more obvious?

mysql> select * from order_items;
+----+----------+--------+----------+------------+-------+
| id | order_id | name   | quantity | item_price | total |
+----+----------+--------+----------+------------+-------+
|  1 |        5 | Item 1 |        2 |       3.00 |  6.00 |
|  2 |        5 | Item 2 |        1 |       2.00 |  2.00 |
+----+----------+--------+----------+------------+-------+

mysql> select * from orders;
+----+---------+---------+-----------------+---------------+----------------+----------+---------------+------------+----------------+------------+----------------------+--------------------+--------------------+----------------------+
| id | shop_id | user_id | shipping_method | shipping_fees | payment_method | card_fee | commision_fee | item_total | customer_total | shop_total | shop_total_remaining | our_commission_net | our_commission_vat | our_commission_gross |
+----+---------+---------+-----------------+---------------+----------------+----------+---------------+------------+----------------+------------+----------------------+--------------------+--------------------+----------------------+
|  5 |      29 |       9 | delivery        |          1.00 | card           |     0.50 |            13 |       8.00 |           9.50 |       9.00 |                 7.83 |               1.17 |               0.23 |                 1.40 |
+----+---------+---------+-----------------+---------------+----------------+----------+---------------+------------+----------------+------------+----------------------+--------------------+--------------------+----------------------+

Fields name description:

  • item_total Total cost from order_items.order_id = 5

  • customer_total Total cost for customer to pay (item_total + card_fee + shipping_fee)

  • shop_total Total order for the shop (item_total + shipping_fees)

  • shop_total_remaining Total remaining to pay back to shop (shop_total - our_commission_net)

  • our_commission_net Commission I will make (commission_fee * shop_total / 100)

  • our_commission_gross Commission inc VAT (our_commission_net + our_commission_vat)

Did I add unnecessary fields?

I used PHP to calculate the cost.

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

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

发布评论

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

评论(1

爱的那么颓废 2024-12-16 01:53:52

我可以看到您实际上添加了许多不必要的字段。可以这样想:如果您可以解释如何通过对其他字段应用任何操作来计算一个字段,那么它就是一个计算字段。

这些字段不应该是简约设计的一部分,因为它们可以通过对其他字段进行操作来获得。让我们看一下表中最复杂的示例。

如果您想获取示例中某个订单的所有商品的总计,您只需选择 item_total 字段即可。但如果它不存在会发生什么?你还能得到那个号码吗?是的。方法如下:

select sum(oi.total) from order_items oi
inner join order o on (oi.order_id = o.id)
where (o.id = 5)

现在,我们已经删除了一个字段,但是我们可以删除 order_items.total 字段并仍然得到这个结果吗?是的。因为它也是一个计算字段。方法如下:

select sum(oi.quantity * oi.item_price) from order_items oi
inner join order o on (oi.order_id = o.id)
where (o.id = 5)

应用类似的模式,您可以删除您提到的所有字段。然后你就会得到一个最小的设计。

值得一提的是,计算字段比查询值更复杂,因此在 CPU 和 HD 使用方面更昂贵。计算字段的优点是可以避免数据冗余并节省一些空间:)

I can see you've actually added many fields that where not necessary. Think of it this way: If you can explain how to calculate a field by applying any operations to other fields, then it is a calculated field.

Those fields shouldn't be part of a minimalistic design as they can be obtained by operating over other fields. Let's go for the most complex example in your tables.

If you wanted to get the total of all the items for an order in your example you would just select the item_total field. But what happens if it wasn't there? Could you still get that number? Yes. This is how:

select sum(oi.total) from order_items oi
inner join order o on (oi.order_id = o.id)
where (o.id = 5)

Now, we've got rid of one field, but can we remove the order_items.total field and still get this result? Yes. Because it is also a calculated field. This is how:

select sum(oi.quantity * oi.item_price) from order_items oi
inner join order o on (oi.order_id = o.id)
where (o.id = 5)

Applying a similar pattern you can get rid of all the fields you've mentioned. And then you'll have a minimal design.

One thing that worths mentioning is that calculating fields is more complex than just querying the value so they are more expensive in terms of CPU and HD usage. The advantage of calculated fields is that you avoid data redundancy and save a bit of space too :)

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