Anda cuba memodelkan varian produk dan percaya EAV (Entity-Attribute-Value) mungkin diperlukan. Berikut ialah reka bentuk anda:
PRODUCT -> PRODUCT_VARIANTS +---------------+ +---------------+ | #product_id | | #product_id | | product_name | | #variant_id | +---------------+ | sku_id | | +---------------+ | | +--------^--------+ +--------^--------+ | PRODUCT_OPTIONS |-----< VARIANT_VALUES | +-----------------+ +-----------------+ | #product_id | | #product_id | | #option_id | | #variant_id | +--------v--------+ | #option_id | | | value_id | +-----------------+ +--------v--------+ | OPTIONS | | +---------------+ | | #option_id | | | option_name | | +---------------+ | | | +-------^-------+ | | OPTION_VALUES |---------------+ +---------------+ | #option_id | | #value_id | | value_name | +---------------+
Anda ingin tahu sama ada reka bentuk anda kukuh, sama ada reka bentuk itu akan mempunyai masalah pertanyaan dan skalabiliti, dan jika ia dinormalisasi.
Matlamat memodelkan varian produk adalah untuk mewakili produk yang boleh mempunyai variasi yang berbeza, seperti saiz, warna atau bahan. Setiap variasi biasanya dikaitkan dengan SKU (Unit Penyimpan Stok) yang unik.
Berikut ialah reka bentuk alternatif yang tidak menggunakan EAV:
+---------------+ +---------------+ | PRODUCTS |-----< PRODUCT_SKUS | +---------------+ +---------------+ | #product_id | | #product_id | | product_name | | #sku_id | +---------------+ | sku | | | price | | +---------------+ | | +-------^-------+ +------^------+ | OPTIONS |------< SKU_VALUES | +---------------+ +-------------+ | #product_id | | #product_id | | #option_id | | #sku_id | | option_name | | #option_id | +---------------+ | value_id | | +------v------+ +-------^-------+ | | OPTION_VALUES |-------------+ +---------------+ | #product_id | | #option_id | | #value_id | | value_name | +---------------+
Utama Kunci (PK), Kunci Unik (UK) dan Kunci Asing (FK):
PRODUK
PILIHAN
OPTION_VALUES
PRODUCT_SKUS
SKU_VALUES
Menggunakan data berikut daripada hamparan yang disediakan:
+-----------+----------+---------+----------+----------+ | ProductId | Product | Size | Color | Class | +-----------+----------+---------+----------+----------+ | 1 | Widget 1 | Small | White | null | | 1 | Widget 1 | Small | Black | null | | 1 | Widget 1 | Large | White | null | | 1 | Widget 1 | Large | Black | null | | 2 | Widget 2 | Small | null | null | | 2 | Widget 2 | Medium | null | null | | 3 | Widget 3 | Medium | null | Amateur | | 3 | Widget 3 | Large | null | Amateur | | 3 | Widget 3 | Medium | null | Professional | | 3 | Widget 3 | Large | null | Professional | +-----------+----------+---------+----------+----------+
The data boleh dimasukkan ke dalam jadual seperti berikut:
-- PRODUCTS +-----------+----------+ | ProductId | Product | +-----------+----------+ | 1 | Widget 1 | | 2 | Widget 2 | | 3 | Widget 3 | +-----------+----------+ -- OPTIONS +-----------+---------+----------+ | ProductId | OptionId | OptionName | +-----------+---------+----------+ | 1 | 1 | Size | | 1 | 2 | Color | | 2 | 1 | Size | | 3 | 1 | Class | | 3 | 2 | Size | +-----------+---------+----------+ -- OPTION_VALUES +-----------+---------+---------+----------+ | ProductId | OptionId | ValueId | ValueName | +-----------+---------+---------+----------+ | 1 | 1 | 1 | Small | | 1 | 1 | 2 | Large | | 1 | 2 | 1 | White | | 1 | 2 | 2 | Black | | 2 | 1 | 1 | Small | | 2 | 1 | 2 | Medium | | 3 | 1 | 1 | Amateur | | 3 | 1 | 2 | Professional | | 3 | 2 | 1 | Medium | | 3 | 2 | 2 | Large | +-----------+---------+---------+----------+ -- PRODUCT_SKUS +-----------+----------+-----+--------+ | ProductId | SkuId | Sku | Price | +-----------+----------+-----+--------+ | 1 | 1 | W1SSCW | 10.00 | | 1 | 2 | W1SSCB | 10.00 | | 1 | 3 | W1SLCW | 12.00 | | 1 | 4 | W1SLCB | 15.00 | | 2 | 1 | W2SS | 100.00 | | 2 | 2 | W2SM | 100.00 | | 3 | 1 | W3CASM | 50.00 | | 3 | 2 | W3CASL | 50.00 | | 3 | 3 | W3CPSM | 150.00 | | 3 | 4 | W3CPSL | 160.00 | +-----------+----------+-----+--------+ -- SKU_VALUES +-----------+----------+---------+---------+ | ProductId | SkuId | OptionId | ValueId | +-----------+----------+---------+---------+ | 1 | 1 | 1 | 1 | -- W1SSCW: Size; Small | 1 | 1 | 2 | 1 | -- W1SSCW: Color; White | 1 | 2 | 1 | 1 | -- W1SSCB: Size; Small
Atas ialah kandungan terperinci Adakah reka bentuk pangkalan data varian produk berasaskan EAV saya berbunyi, berskala dan dinormalisasi, atau adakah reka bentuk hubungan alternatif menawarkan pertanyaan dan prestasi yang lebih baik?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!