代码之家  ›  专栏  ›  技术社区  ›  chchrist

数据库设计问题

  •  1
  • chchrist  · 技术社区  · 15 年前

    我有餐桌产品和尺寸。每种产品可以有多种尺寸。如何设计数据库以避免重复输入同一产品?

    提前谢谢

    4 回复  |  直到 15 年前
        1
  •  2
  •   Justin Niessner    15 年前

    处理多个关系的典型方法是使用名为product\u size的映射表,该表包含每个表的主键。

    create table Product (
        id uniqueidentifier not null,
        name varchar(255),
        primary key (id))
    
    create table Size (
        id int,
        name varchar(255),
        primary key (id))
    
    create table Product_Size (
        productId uniqueidentifier,
        sizeId int,
        primary key (productId, sizeId),
        foreign key (productId) references Product(id),
        foreign key (sizeId) references Size(id))
    
        2
  •  1
  •   Chris Diver    15 年前

    这取决于,每个产品可以有多个尺寸,但不同的产品可以有相同的尺寸吗?

    如果他们不能这样做,那么您需要一个包含产品主键的productSize表。

    ProductSize (SizeID, ProductID, Size)

    如果他们可以,那么你就有一个多对多的关系,你可以通过有三个表来打破这种关系:product,size和product size,其中product包含products,size包含sizes,product size通过持有product和size的主键将每个产品映射到可用的大小。

    Product (ProductID, ProductName)
    Size (SizeID, SizeName)
    ProductSize (ProductID, SizeID)
    
        3
  •  1
  •   Pranay Rana    15 年前
    product Table
    
     1. product id 
     2. product name
     ......
    
    Product Size
    
     1. Id
     2. ProductId( Foreign key form product table)
     3. Size 
    
        4
  •  0
  •   KM.    15 年前

    尝试如下操作:

    Products
    ----------
    ProductID    PK, auto increment int
    ProductName
    ....
    
    Sizes
    ------- 
    SizeID      PK, auto increment int
    SizeInfo
    ....
    
    ProductSizes
    --------------
    ProductID   PK, FK to Products.ProductID
    SizeID      PK, FK to Sizes.SizeID