简体   繁体   English

在MySQL的单个表或多个表中保存不同的属性类型

[英]saving different attributes types in single table or multiple tables in mysql

we are planning to built a e-commerce site with PHP. 我们计划使用PHP构建一个电子商务网站。

There are different types of products like mobiles, tshirts, watches, books. 有不同类型的产品,例如手机,T恤,手表,书籍。

  • Mobile have attributes like "ram , model_name, os" , 移动设备具有“ ram,model_name,os”之类的属性,
  • T-shirts have attributes like "color, size" T恤具有“颜色,尺寸”之类的属性

Questions are the following: 问题如下:

  1. Is it better to save all attributes in a single table called Products (even if there will be/ it will create a lot of " NULL" values)? 将所有属性保存在称为Products的单个表中是否更好(即使会有/将创建很多“ NULL”值)?

  2. Save different types of products in different tables like mobile , tshirts , etc ... 将不同类型的产品保存在不同的表中,例如手机T恤等。

in the second case more tables will be created (see the 2 images below). 在第二种情况下,将创建更多表(请参见下面的2张图像)。

在此处输入图片说明

or 要么

在此处输入图片说明

Update 更新资料

as suggested in answers & comments, i am using EAV model and draw following design, please inform me if you found any mistakes.... 如答案和评论中所建议,我正在使用EAV模型并绘制以下设计,如果发现任何错误,请通知我。

在此处输入图片说明

Create product table, 创建产品表,

create table property 创建表属性

Fields: 领域:

Id    
Name    
...

Create table category 创建表类别

Fields: 领域:

Id     
Name    
...

Create tables category_product and category_property... 创建表category_product和category_property ...

Create table property_product_value 创建表property_product_value

Fields: 领域:

Id     
Property_id
product_id    
Value    
...

Written from mobile, if you have more questions let me know. 用手机写的,如果您还有其他问题,请告诉我。

The problem you describe is pretty common - it's generally known as "storing polymorphic data in a relational schema". 您描述的问题非常普遍-通常称为“在关系模式中存储多态数据”。 It occurs on Stack Overflow quite regularly . 它发生在堆栈溢出相当 有规律

You ask "what's better" - that, of course depends on the way you intend to use the data. 您问“有什么更好的”-当然,这取决于您打算使用数据的方式。

If you don't know in advance what attributes you're going to be storing - for instance, if your ecommerce site is likely to introduce more product types in the future - a purely relational model is unlikely to work, because you have to make schema changes every time you introduce a new type of product. 如果您事先不知道要存储什么属性,例如,如果您的电子商务站点将来可能会引入更多产品类型,则纯关系模型不太可能起作用,因为您必须每次您引入一种新型产品时,架构都会更改。

If you don't know in advance what sort of queries you need to support, again, the relational model may be a problem. 同样,如果您事先不知道需要支持哪种查询,则关系模型可能是个问题。 For instance, if you have a filtering mechanism that allows users to search for t-shirts in colour blue, size small, from brand "xyz", you may need to dynamically create a SQL query. 例如,如果您有一个过滤机制,允许用户从“ xyz”品牌中搜索尺寸较小的蓝色T恤,则可能需要动态创建SQL查询。 This is not particularly easy. 这不是特别容易。

The EAV model gets round the first problem, but not the second. EAV模型解决了第一个问题,但没有解决第二个问题。 In fact, it can become really hard to run even simple queries against an EAV datamodel. 实际上,对EAV数据模型运行甚至简单的查询也可能变得非常困难。

I'd consider a solution where you store the "known upfront" attributes in SQL tables (SKU, price, is_sellable, description etc.), along with the relationships (vendor, category, warehouse etc.). 我会考虑一个解决方案,其中在SQL表(SKU,价格,is_sellable,描述等)中存储“已知的前期”属性以及关系(供应商,类别,仓库等)。 The other, variable data can then live in JSON documents within the database. 然后,其他可变数据可以存在于数据库内的JSON文档中。 I'd pay particular attention to the MySQL full text search indexing - you may be able to use this instead of "pure" SQL to run many common queries. 我要特别注意MySQL 全文搜索索引 -您也许可以使用它代替“纯” SQL来运行许多常见查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM