mysql购物车_MySQL购物车结构

本文介绍了如何使用MySQL设计一个购物车系统,包括四个表格:ItemTypes、TypeProperties、Items和ItemProperties,分别用于存储商品类型、属性、具体商品及其属性值。通过这些表格,可以有效地管理和组织购物车中的各种商品信息。
摘要由CSDN通过智能技术生成

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

-- 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' );

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值