1

我希望创建一个能够处理多个类别的 MySQL 商店。我已经对所有类别设施等进行了排序,但我没有得到任何地方的是这个..

每个项目可以有多个选项,例如一件 T 恤应该有“颜色”和“尺码”选项。然后,我需要从父产品创建许多变体/派生产品,指定超大蓝色 T 恤有 20 件库存(例如)。问题是,出售的不只是衣服,它可能是任何数量的东西。所以我还需要这个架构来处理无限数量的变体,例如带有“跑车”设计的“ 6mm ”“大型”生日贺卡。6mm、Large 和 'Ace' 是变量。通过这种方式,我能够确保我们没有任何库存控制问题。如果对您有用,以下是我当前的站点结构。

现有数据库架构 http://www.hallwaystudios.com/screenshots/uploads/g5B7SNKU.png

我希望你明白我的意思,并且有人能回答我的问题!非常感谢提前(当然之后)

4

1 回答 1

5

不太清楚这里的问题是什么......我可能会创建四个表:

-- a table of item types (t-shirt, birthday card, etc.)
CREATE TABLE ItemTypes (
  TypeID      INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  TypeName    VARCHAR(20) NOT NULL
);

-- a table of associated properties
CREATE TABLE TypeProperties (
  PropertyID  INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  TypeID      INT NOT NULL,
  PropName    VARCHAR(20) NOT NULL,
  INDEX(Property, TypeID),
  FOREIGN KEY(TypeID) REFERENCES ItemTypes(TypeID)
);

-- a table of specific items (XL Blue t-shirt, large bday card w/sports car, etc.)
CREATE TABLE Items (
  ItemID      INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  TypeID      INT NOT NULL,
  ItemName    VARCHAR(100) NOT NULL,
  ItemPrice   DECIMAL UNSIGNED NOT NULL,
  ItemStock   INT UNSIGNED NOT NULL,
  INDEX(ItemID, TypeID),
  FOREIGN KEY(TypeID) REFERENCES ItemTypes(TypeID)
);

-- the dictionary of property values
CREATE TABLE ItemProperties (
  ItemID      INT NOT NULL,
  TypeID      INT NOT NULL,
  PropertyID  INT NOT NULL,
  Value       VARCHAR(20) NOT NULL,
  PRIMARY KEY(ItemID, Property),
  INDEX(ItemID,     TypeID),
  INDEX(PropertyID, TypeID),
  FOREIGN KEY(            TypeID) REFERENCES ItemTypes     (            TypeID),
  FOREIGN KEY(ItemID,     TypeID) REFERENCES Items         (ItemID,     TypeID),
  FOREIGN KEY(PropertyID, TypeID) REFERENCES TypeProperties(PropertyID, TypeID)
);

这应该是相当明显的,但以防万一,示例数据看起来像:

INSERT INTO ItemTypes (TypeID, TypeName) VALUES
  (1, 'T-Shirt'      ),
  (2, 'Birthday Card'),
  (3, 'Balloon'      );

INSERT INTO TypeProperties(PropertyID, TypeID, PropName) VALUES
  (51, 1, 'Colour' ), (52, 1, 'Size'),
  (53, 2, 'Size/mm'), (54, 2, 'Size'), (55, 2, 'Design'),
  (56, 3, 'Colour' );

INSERT INTO Items (ItemID, TypeID, ItemName, ItemPrice, ItemStock) VALUES
  (101, 1, 'Extra Large Blue T-Shirt',                       10.99, 20),
  (102, 2, '6mm Large Birthday Card with Sports Car Design',  2.99, 17),
  (103, 1, 'Extra Large Black T-Shirt',                      10.99,  5),
  (104, 3, 'Pink balloon',                                    0.10, 60);

INSERT INTO ItemProperties (ItemID, TypeID, PropertyID, Value) VALUES
  (101, 1, 51, 'Blue'       ),
  (101, 1, 52, 'Extra Large'),
  (102, 2, 53, '6'          ),
  (102, 2, 54, 'Large'      ),
  (102, 2, 55, 'Sports Car' ),
  (103, 1, 51, 'Black'      ),
  (103, 1, 52, 'Extra Large'),
  (104, 3, 56, 'Pink'       );
于 2012-05-01T13:43:12.217 回答