产品/变体/捆绑/购物车的数据库结构

问题描述 投票:0回答:1

我遇到了一个场景,我需要在 MariaDB 上设计 SQL 数据库结构,但我不是数据库架构师,我想第一次尝试就做好。

所以,我有一个产品表,为了可读性,它有 2 个字段,id 和 name。我不知道如何制作这个图形,所以我将使用 json 来定义它:

productColumns = {
    id: "INTEGER",
    name: "STRING",
}

然后我有变体表:

variantsColumns = {
    id: "INTEGER",
    name: "STRING",
    product_id: "INTEGER",
}

现在是目标。我需要添加 BundlesCarts。捆绑包必须包含具有指定变体的多个产品。购物车必须包含具有指定变体的多种产品以及捆绑产品。现在我需要能够为购物车和捆绑包作为一个整体添加折扣,还需要为购物车或捆绑包中的特定产品添加折扣。

我的想法:

bundlesColumns: {
    id: "INTEGER",
    name: "STRING",
    discount_type: "PERCENTAGE|ABSOLUTE"
    discount: "INTEGER"
}

bundleItems: {
    id: "INTEGER",
    bundle_id: "INTEGER",
    discount_type: "PERCENTAGE|ABSOLUTE",
    discount: "INTEGER",
    product_id: "INTEGER",
    variant_id: "INTEGER",
    quantity: "INTEGER",
}

carts: {
    id: "INTEGER",
    discount_type: "PERCENTAGE|ABSOLUTE"
    discount: "INTEGER"
}

cartItems: {
    id: "INTEGER",
    cart_id: "INTEGER",
    discount_type: "PERCENTAGE|ABSOLUTE",
    discount: "INTEGER",
    product_id: "INTEGER",
    variant_id: "INTEGER",
    bundle_id: "INTEGER",
    quantity: "INTEGER"
}

我之前设计过一些数据库,但这种结构感觉不太好,看起来这对未来的开发来说会是一个痛苦,想听听你对此的看法。有更好的方法吗?

sql database mariadb
1个回答
0
投票

您可以创建以下表和索引

-- Products Table
CREATE TABLE products (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL
);

-- Index on the name column for quicker search by product name
CREATE INDEX idx_product_name ON products(name);

-- Variants Table
CREATE TABLE variants (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    product_id INT NOT NULL,
    FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
);

-- Index on product_id to optimize joins between products and variants
CREATE INDEX idx_variant_product_id ON variants(product_id);

-- Product-Variant Relationships Table (Optional, if different variants have different prices or other unique attributes)
CREATE TABLE product_variants (
    product_id INT NOT NULL,
    variant_id INT NOT NULL,
    price DECIMAL(10, 2),  -- Optional field
    PRIMARY KEY (product_id, variant_id),
    FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
    FOREIGN KEY (variant_id) REFERENCES variants(id) ON DELETE CASCADE
);

-- Index on price for filtering based on variant price
CREATE INDEX idx_product_variant_price ON product_variants(price);

-- Bundles Table
CREATE TABLE bundles (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL
);

-- Bundle Items Table
CREATE TABLE bundle_items (
    id INT PRIMARY KEY AUTO_INCREMENT,
    bundle_id INT NOT NULL,
    product_id INT NOT NULL,
    variant_id INT NOT NULL,
    quantity INT NOT NULL DEFAULT 1,
    FOREIGN KEY (bundle_id) REFERENCES bundles(id) ON DELETE CASCADE,
    FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
    FOREIGN KEY (variant_id) REFERENCES variants(id) ON DELETE CASCADE
);

-- Indexes for bundle_items to speed up joins and searches on bundle_id, product_id, and variant_id
CREATE INDEX idx_bundle_items_bundle_id ON bundle_items(bundle_id);
CREATE INDEX idx_bundle_items_product_id ON bundle_items(product_id);
CREATE INDEX idx_bundle_items_variant_id ON bundle_items(variant_id);

-- Carts Table
CREATE TABLE carts (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT,  -- Optional: If associated with a specific user
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Index on user_id for quick retrieval of carts associated with a specific user
CREATE INDEX idx_cart_user_id ON carts(user_id);

-- Cart Items Table
CREATE TABLE cart_items (
    id INT PRIMARY KEY AUTO_INCREMENT,
    cart_id INT NOT NULL,
    product_id INT,
    variant_id INT,
    bundle_id INT,  -- If item is from a bundle
    quantity INT NOT NULL DEFAULT 1,
    FOREIGN KEY (cart_id) REFERENCES carts(id) ON DELETE CASCADE,
    FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE,
    FOREIGN KEY (variant_id) REFERENCES variants(id) ON DELETE CASCADE,
    FOREIGN KEY (bundle_id) REFERENCES bundles(id) ON DELETE SET NULL
);

-- Indexes for cart_items to speed up joins and searches on cart_id, product_id, variant_id, and bundle_id
CREATE INDEX idx_cart_items_cart_id ON cart_items(cart_id);
CREATE INDEX idx_cart_items_product_id ON cart_items(product_id);
CREATE INDEX idx_cart_items_variant_id ON cart_items(variant_id);
CREATE INDEX idx_cart_items_bundle_id ON cart_items(bundle_id);

-- Discounts Table
CREATE TABLE discounts (
    id INT PRIMARY KEY AUTO_INCREMENT,
    discount_type ENUM('PERCENTAGE', 'ABSOLUTE') NOT NULL,
    discount_value DECIMAL(10, 2) NOT NULL,
    applicable_type ENUM('BUNDLE', 'CART', 'PRODUCT') NOT NULL,
    applicable_id INT NOT NULL,
    precedence INT DEFAULT 1,
    FOREIGN KEY (applicable_id) REFERENCES products(id) ON DELETE CASCADE
);

-- Indexes on applicable_type and applicable_id for faster filtering by type and associated entity
CREATE INDEX idx_discounts_applicable_type ON discounts(applicable_type);
CREATE INDEX idx_discounts_applicable_id ON discounts(applicable_id);
CREATE INDEX idx_discounts_precedence ON discounts(precedence);
© www.soinside.com 2019 - 2024. All rights reserved.