【发布时间】:2010-11-27 03:48:25
【问题描述】:
我是 Web 开发的新手,我的经理分配给我购物车项目,我在编程方面没问题。但是,我不知道我应该如何开始任何项目的数据库设计,我对任何项目的 BD 设计应该采取什么方法,我应该如何思考这个方向以及在创建数据库设计之前我应该弄清楚什么,如何我应该考虑实体和它们之间的关系...请详细说明一下?
【问题讨论】:
标签: database-design
我是 Web 开发的新手,我的经理分配给我购物车项目,我在编程方面没问题。但是,我不知道我应该如何开始任何项目的数据库设计,我对任何项目的 BD 设计应该采取什么方法,我应该如何思考这个方向以及在创建数据库设计之前我应该弄清楚什么,如何我应该考虑实体和它们之间的关系...请详细说明一下?
【问题讨论】:
标签: database-design
这个问题太笼统了。但我建议您查看this out - Database Design,尤其是“设计过程”部分
Determine the purpose of your database - This helps prepare you for the remaining steps.
Find and organize the information required - Gather all of the types of
information you might want to record in the database, such as product name
and order number.
Divide the information into tables - Divide your information items into major
entities or subjects, such as Products or Orders.
Each subject then becomes a table.
Turn information items into columns - Decide what information you want to
store in each table. Each item becomes a field, and is displayed as
a column in the table. For example, an Employees table might
include fields such as Last Name and Hire Date.
Specify primary keys - Choose each table’s primary key. The primary key
is a column that is used to uniquely identify each row.
An example might be Product ID or Order ID.
Set up the table relationships - Look at each table and decide
how the data in one table is related to the data in other tables.
Add fields to tables or create new tables to clarify the
relationships, as necessary.
Refine your design - Analyze your design for errors.
Create the tables and add a few records of sample data.
See if you can get the results you want from your tables.
Make adjustments to the design, as needed.
Apply the normalization rules - Apply the data normalization rules
to see if your tables are structured correctly.
Make adjustments to the tables.
另外,请务必让在设计数据库方面有更多经验的人对其进行审核
【讨论】:
首先,找出您要表示的所有实体,例如客户、购物车、产品等。
然后找出你想要的每个属性。例如,客户将拥有 ID、姓名、地址、送货地址等。购物车将包含一个客户 ID 和多个产品。不要太担心会在这里遗漏细节,因为它们可以稍后添加。
为您的实体分配属性几乎肯定会将这些实体之间的关系置于您的脑海中。这是下一步,找出所有实体之间的关系。
一旦您(认为自己)拥有了所有这些,请运行一些用例(新客户、客户将商品添加到购物车、结帐/付款等)。这可能会出现您没有想到的事情,您可以调整您的实体/关系图以适应。 记录这些用例并根据需要添加到它们中,在最终确定架构时它们将是无价的。
记住,无论如何,从第三种范式数据库模式开始。恢复到较小的性能形式是可以的(前提是您了解并缓解其中涉及的问题),但这是很多稍后才会出现的东西,只有如果你有真正的表现3NF 的问题。
第三范式意味着表中的每个非键列都依赖于键,整个键,而只有键。
举例来说,这里有一个基本架构可以帮助您:
+--------------------+ +-----------------+
| Products | | Customers |
+--------------------+ +-----------------+
+->| ProductId (pk) | | CustomerID (pk) |<-+
| | Description | | Name | |
| | StockLevel | | Address | |
| | Cost | | ShippingAddress | |
| +--------------------+ +-----------------+ |
| |
| +--------------------+ +-----------------+ |
| | CartContents | | Cart | |
| +--------------------+ +-----------------+ |
| | CartID (pka,fk) |------->| CartID (pk) | |
+--| ProductID (pkb,fk) | | CustomerID (fk) |--+
| Quantity | | Status |
+--------------------+ +-----------------+
这允许四个相当基本的实体,您可以添加更多,这取决于您需要什么。您可能想要处理批量购买、忠诚度计划和其他事情的特别优惠。购物车内容中的状态为 initial、paidfor、payment confirmed、shipped、received 等等,但您可能希望在购物车结账后使用单独的 orders 表过程 - 我尽可能简单。
它还允许每个客户有多个购物车,当它不存在时我觉得这个功能很烦人(换句话说,请实施这个,没有什么比必须清除您已经设置的购物车来做另一个更烦人的了您希望首先处理的交易)。
【讨论】:
答案很大程度上取决于您的特定需求。作为起点,何不下载一些开源购物车应用程序(如Dashcommerce 或aspdotnetstorefront),并分析它们的数据库设计?它们甚至可能满足您的需求,而您不必重新发明轮子。
【讨论】:
如果您不熟悉数据库设计,为什么要开发购物车?去获取现成的解决方案或找有专业知识的人来帮助你。
【讨论】: