简体   繁体   English

餐厅食品可选折扣的数据库设计

[英]Database design for optional discount on food items of a restaurant

I have to design a database for a restaurant chain.我必须为一家连锁餐厅设计一个数据库。
The situation is as follows - The customer gets either Item A free OR Item B free (not both), when he purchases Item C.情况如下 - 客户购买物品 C 时,要么免费获得物品 A,要么免费获得物品 B(不是两者都免费)。

Following are the two tables in my DB.以下是我的数据库中的两个表。
在此处输入图片说明


For the above example, I will insert an entry for Item C in the HappyHour_Menu_Trans that will generate a new HappyHourMenuTransId.对于上面的示例,我将在 HappyHour_Menu_Trans 中为项目 C 插入一个条目,该条目将生成一个新的 HappyHourMenuTransId。 Using this HappyHourMenuTransId, I will insert two entries in the HappyHour_Menu_Subsequent Table.使用这个 HappyHourMenuTransId,我将在 HappyHour_Menu_Subsequent 表中插入两个条目。

Now what about the optional part?那么可选部分呢?
I can think of adding a column 'Condition' in which I will put value 'OR' for both entries.我可以考虑添加一个“条件”列,我将在其中为两个条目输入值“或”。 This OR will tell me that I can choose only one option.这个 OR 会告诉我我只能选择一个选项。
If I put value 'AND', it will tell me that I will get discounts on both the items.如果我输入值“AND”,它会告诉我这两个项目都将获得折扣。

This solution is fine for me till now, but my senior wants me to avoid using string.到目前为止,这个解决方案对我来说很好,但我的前辈希望我避免使用字符串。 I can use a checkbox (that's not the problem!).我可以使用复选框(这不是问题!)。

The Tricky parts starts from here -棘手的部分从这里开始——

Lets suppose the discount is like this - Buy Item A, and you will get either both (Item B + Item C) free, OR only (Item D + Item E) free.假设折扣是这样的 - 购买物品 A,您将同时获得(物品 B + 物品 C)免费,或仅(物品 D + 物品 E)免费。

Now according to my current system I will insert four entries;现在根据我当前的系统,我将插入四个条目; but how will I know the exact logic from the tables(which two items make a pair?).但是我如何从表格中知道确切的逻辑(哪两个项目是一对?)。

I need clarification on the following statement you made.我需要澄清您所做的以下声明。

"Buy Item A, and you will get either both (Item B + Item C) free, OR only (Item D + Item E) free." “购买物品 A,您将同时获得(物品 B + 物品 C)免费,或仅(物品 D + 物品 E)免费。”

Is it only Item D or Item E?是只有项目 D 还是项目 E?

Initially I was thinking this could be solved with some type of referential integrity.最初我认为这可以通过某种类型的参照完整性来解决。

A discount table has the following entries.折扣表具有以下条目。

(Purchase Item, Free Item)
(A, B)
(A, C)

Create a foreign key relationship between the [HappyHour_Menu_Subsequent] table and these two columns.在 [HappyHour_Menu_Subsequent] 表和这两列之间创建外键关系。 However, this assumes a 1-to-1 relationship.但是,这假设是 1 对 1 的关系。 If you start bundling items, 1-to-1 or 1-to-x, this solution will fail.如果您开始捆绑项目,1 对 1 或 1 对 x,此解决方案将失败。

Given the fact that the business logic might change, there are two solutions that come to mind.考虑到业务逻辑可能会改变这一事实,我想到了两种解决方案。

1 - I would use an INSERT or UPDATE trigger to enforce your business logic if the application does direct inserts and updates to the tables. 1 - 如果应用程序直接插入和更新表,我将使用 INSERT 或 UPDATE 触发器来强制执行您的业务逻辑。

CREATE TRIGGER [TRG_ENFORCE_MY_HAPPY_HOUR_DEALS] 
    ON [HappyHour_Menu_Subsequent]
FOR INSERT, UPDATE
AS
BEGIN

-- put your logic here
IF (Violation) 
    RAISERROR();

END

The Violation is just a place holder.违规只是一个占位符。 You will have to look at the inserted and deleted tables to retrieve the purchased item and free item.您将不得不查看插入和删除的表以检索购买的项目和免费项目。 Make sure the combination meets your business rules.确保组合符合您的业务规则。

Have the front end handle the error if the logic is violated.如果违反逻辑,让前端处理错误。

2 - If you use stored procedures to perform INSERTS or UPDATES, add the business logic in that code. 2 - 如果您使用存储过程来执行 INSERTS 或 UPDATES,请在该代码中添加业务逻辑。

In my opinion, create another table called Group_Item, this table will hold the item as a pair, for your example we will have two rows, one row contains Item (B + C) and one row contains Item ( D + E).在我看来,创建另一个名为 Group_Item 的表,该表将项目作为一对保存,对于您的示例,我们将有两行,一行包含 Item (B + C),一行包含 Item (D + E)。 If implement like this, you can enhance the item length or reduce in pair, for example (B + C + D) or (C + F).如果这样实现,你可以成对增加或减少项目长度,例如(B + C + D)或(C + F)。

In case if not a pair just a single item like you said above:如果不是一对,就像你上面说的那样,只有一个项目:

The customer gets either Item A free OR Item B free客户获得免费的物品 A 或免费的物品 B

We will have two rows, one row contains Item A only and another row contains Item B only我们将有两行,一行仅包含项目 A,另一行仅包含项目 B

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

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