2014-10-28 73 views
0

我正在开展一个项目,并试图找到一种方法将我的定价计划表与客户相关联。在mysql和php中实现定价计划最简单的方法是什么?

id name description  price days 
1 Free the free plan 0.00 0 
2 Silver the silver plan 49.99 365 
3 Gold the gold plan 99.99 365 

我的第一个想法是将plan id作为外键添加到customers表中。但我也需要知道何时到期日(基于购买日期和所选计划的日期)。

回答

0

当试图做这样的事情时,最好把表格从对方抽象出来。

user_pricing_plans - the table name 
pricing_plans_id - the id for the record you want to associate with a user 
user_id - the id of the user 
date_expiration - the date the plan will expire 
date_purchased - the date the plan was purchased 

下面的方法可以让你修改这个表,如果需要添加额外的信息。您也可以在未来使用另一个表格遵循类似的方法。

所有这一切的关键是将您的担忧(数据)分隔为单独的容器。

编辑:为了提示我在谈论为什么分离表是个好主意,我在user_pricing_plans表中添加了date_purchased作为字段。

a dba我知道曾经说过“mysql不是开发者的地方 - 他们试图创建表来处理他们的代码,表格是为了表示数据,你的代码应该被编写来处理数据和代表他们的模式 - 不是相反的方式“

+0

有没有一种方法实现,没有第三个表? – 2014-10-28 02:37:57

+0

有,但您需要将定价plan_id和到期日期字段添加到用户表中。我建议不要这样做,因为它只会邀请你进一步污染用户的表格模式并提供额外的信息 - 这样你就可以访问你之后的任何信息。最终,你的桌子会变得太大。 sql中表的关键是保持它们轻量级,正确索引和规范化。 – 2014-10-28 02:40:21

+0

这是一个很好的链接,你应该在某个时候阅读:http://searchbusinessintelligence.techtarget.in/tutorial/Database-normalization-in-MySQL-Four-quick-and-easy-steps – 2014-10-28 02:42:00

相关问题