【问题标题】:what to figure out before creating a DB design?在创建数据库设计之前要弄清楚什么?
【发布时间】:2010-11-27 03:48:25
【问题描述】:

我是 Web 开发的新手,我的经理分配给我购物车项目,我在编程方面没问题。但是,我不知道我应该如何开始任何项目的数据库设计,我对任何项目的 BD 设计应该采取什么方法,我应该如何思考这个方向以及在创建数据库设计之前我应该​​弄清楚什么,如何我应该考虑实体和它们之间的关系...请详细说明一下?

【问题讨论】:

    标签: database-design


    【解决方案1】:

    这个问题太笼统了。但我建议您查看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.
    

    另外,请务必让在设计数据库方面有更多经验的人对其进行审核

    【讨论】:

      【解决方案2】:

      首先,找出您要表示的所有实体,例如客户、购物车、产品等。

      然后找出你想要的每个属性。例如,客户将拥有 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          |
         +--------------------+        +-----------------+
      

      这允许四个相当基本的实体,您可以添加更多,这取决于您需要什么。您可能想要处理批量购买、忠诚度计划和其他事情的特别优惠。购物车内容中的状态为 initialpaidforpayment confirmedshippedreceived 等等,但您可能希望在购物车结账后使用单独的 orders 表过程 - 我尽可能简单。

      它还允许每个客户有多个购物车,当它不存在时我觉得这个功能很烦人(换句话说,请实施这个,没有什么比必须清除您已经设置的购物车来做另一个更烦人的了您希望首先处理的交易)。

      【讨论】:

      • 先生,非常感谢您的帮助,我很清楚我应该如何进行。先生,pka 和 pkb 是什么?
      • 先生,我不清楚 CartContents 和 Cart 中的 CartID。它们都是自动增量字段还是与身份密钥的字段分开?那么身份密钥也应该在那里?
      • CartID 只是购物车的唯一标识符。 如何生成它是一个实现细节。最有可能的是,它是 Cart 表的自动增量,但您随后会使用该 specific 值来填充 CartContents 表。
      【解决方案3】:

      答案很大程度上取决于您的特定需求。作为起点,何不下载一些开源购物车应用程序(如Dashcommerceaspdotnetstorefront),并分析它们的数据库设计?它们甚至可能满足您的需求,而您不必重新发明轮子。

      【讨论】:

      • SteveCav 有一个很好的建议。如果您将使用 SQL Server,请查看 AdventureWorks 数据库的数据库设计。虽然它没有名为“Cart”的表,但有一个订单标题和详细信息表。
      【解决方案4】:

      如果您不熟悉数据库设计,为什么要开发购物车?去获取现成的解决方案或找有专业知识的人来帮助你。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-15
        • 2014-07-22
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 2019-04-19
        • 1970-01-01
        相关资源
        最近更新 更多