Mysql数据库设计一个复杂的购物目录部分

Mysql数据库设计一个复杂的购物目录部分,mysql,database-design,schema,shopping,Mysql,Database Design,Schema,Shopping,我想做一个声明系统。但我搞砸了数据库设计部分 该系统将有无限的类别和他们的项目,所以每个类别将有奇怪的属性 例如,汽车有型号、行业、制造日期、颜色、齿轮类型。还有更多 另一个例子是房地产有一个门数,园丁,Duplex三联式,有多少个浴室,多少个厨房 另一个可以是电子的,它有百万像素,千兆字节,保修,分辨率,尺寸,装运 因此,我如何收集这些数据可能有些对象具有类似的属性,例如大多数项目必须有制造商日期,那么我如何使用这些数据?模式是如何实现的 请注意,这是一个非常模糊的问题,所以我尽力回答:) 注

我想做一个声明系统。但我搞砸了数据库设计部分

该系统将有无限的类别和他们的项目,所以每个类别将有奇怪的属性

例如,汽车有型号、行业、制造日期、颜色、齿轮类型。还有更多

另一个例子是房地产有一个门数,园丁,Duplex三联式,有多少个浴室,多少个厨房

另一个可以是电子的,它有百万像素,千兆字节,保修,分辨率,尺寸,装运

因此,我如何收集这些数据可能有些对象具有类似的属性,例如大多数项目必须有制造商日期,那么我如何使用这些数据?模式是如何实现的


请注意,这是一个非常模糊的问题,所以我尽力回答:)

注意:有很多更好的方法可以做到这一点,我可能会问你为什么要这样做,但下面的方法应该可以完成你所寻找的

以下是三张表格:

CREATE TABLE `object` (
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`category` INTEGER NOT NULL ,
`name` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`id`)
);

CREATE TABLE `properties` (
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`objectid` INTEGER NOT NULL ,
`property_key` VARCHAR(100) NOT NULL ,
`property_value` MEDIUMTEXT NOT NULL ,
PRIMARY KEY (`id`)
);

CREATE TABLE `category` (
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`id`)
);
现在,让我们向其中添加一些假装数据:

INSERT INTO category VALUES ("","CAR");
INSERT INTO category VALUES ("","REALESTATE");
INSERT INTO category VALUES ("","ELECTRONIC");
INSERT INTO object VALUES ("",1,"98 CAMERO");
INSERT INTO object VALUES ("",1,"06 PRIUS");
INSERT INTO object VALUES ("",2,"A House Someplace");
INSERT INTO object VALUES ("",3,"iPod Touch");
INSERT INTO object VALUES ("",3,"iPhone");
INSERT INTO object VALUES ("",3,"Zune");
INSERT INTO properties VALUES ("",1,"color","red");
INSERT INTO properties VALUES ("",1,"doors","4");
INSERT INTO properties VALUES ("",1,"engine","v6");
INSERT INTO properties VALUES ("",2,"engine","electric");
INSERT INTO properties VALUES ("",2,"doors","4");
INSERT INTO properties VALUES ("",2,"color","blue");
INSERT INTO properties VALUES ("",6,"video-playback","true");
INSERT INTO properties VALUES ("",4,"video-playback","true");
INSERT INTO properties VALUES ("",5,"video-playback","true");
INSERT INTO properties VALUES ("",6,"manufacturer","microsoft");
INSERT INTO properties VALUES ("",5,"manufacturer","apple");
INSERT INTO properties VALUES ("",4,"manufacturer","apple");
现在,我们的数据应该是这样的

mysql> select * from object;
+----+----------+-------------------+
| id | category | name              |
+----+----------+-------------------+
|  1 |        1 | 98 CAMERO         | 
|  2 |        1 | 06 PRIUS          | 
|  3 |        2 | A House Someplace | 
|  4 |        3 | iPod Touch        | 
|  5 |        3 | iPhone            | 
|  6 |        3 | Zune              | 
+----+----------+-------------------+
6 rows in set (0.00 sec)

mysql> select * from category;
+----+-------------+
| id | name        |
+----+-------------+
|  1 | CAR         | 
|  2 | REALESTATE  | 
|  3 | ELECTRONICS | 
+----+-------------+
3 rows in set (0.00 sec)

mysql> select * from properties;
+----+----------+----------------+----------------+
| id | objectid | property_key   | property_value |
+----+----------+----------------+----------------+
|  1 |        1 | color          | red            | 
|  2 |        1 | doors          | 4              | 
|  3 |        1 | engine         | v6             | 
|  4 |        2 | engine         | electric       | 
|  5 |        2 | doors          | 4              | 
|  6 |        2 | color          | blue           | 
|  7 |        6 | video-playback | true           | 
|  8 |        4 | video-playback | true           | 
|  9 |        5 | video-playback | true           | 
| 10 |        6 | manufacturer   | microsoft      | 
| 11 |        5 | manufacturer   | apple          | 
| 12 |        4 | manufacturer   | apple          | 
+----+----------+----------------+----------------+
12 rows in set (0.00 sec)
您可以在这些表中添加无限数量的类别、无限数量的属性和无限数量的对象

现在,让我们将它们结合在一起:

mysql> SELECT * FROM object 
    ->   LEFT JOIN category ON object.category = category.id
    ->   LEFT JOIN properties ON properties.objectid = object.id;
+----+----------+-------------------+------+-------------+------+----------+----------------+----------------+
| id | category | name              | id   | name        | id   | objectid | property_key   | property_value |
+----+----------+-------------------+------+-------------+------+----------+----------------+----------------+
|  1 |        1 | 98 CAMERO         |    1 | CAR         |    1 |        1 | color          | red            | 
|  1 |        1 | 98 CAMERO         |    1 | CAR         |    2 |        1 | doors          | 4              | 
|  1 |        1 | 98 CAMERO         |    1 | CAR         |    3 |        1 | engine         | v6             | 
|  2 |        1 | 06 PRIUS          |    1 | CAR         |    4 |        2 | engine         | electric       | 
|  2 |        1 | 06 PRIUS          |    1 | CAR         |    5 |        2 | doors          | 4              | 
|  2 |        1 | 06 PRIUS          |    1 | CAR         |    6 |        2 | color          | blue           | 
|  3 |        2 | A House Someplace |    2 | REALESTATE  | NULL |     NULL | NULL           | NULL           | 
|  4 |        3 | iPod Touch        |    3 | ELECTRONICS |    8 |        4 | video-playback | true           | 
|  4 |        3 | iPod Touch        |    3 | ELECTRONICS |   12 |        4 | manufacturer   | apple          | 
|  5 |        3 | iPhone            |    3 | ELECTRONICS |    9 |        5 | video-playback | true           | 
|  5 |        3 | iPhone            |    3 | ELECTRONICS |   11 |        5 | manufacturer   | apple          | 
|  6 |        3 | Zune              |    3 | ELECTRONICS |    7 |        6 | video-playback | true           | 
|  6 |        3 | Zune              |    3 | ELECTRONICS |   10 |        6 | manufacturer   | microsoft      | 
+----+----------+-------------------+------+-------------+------+----------+----------------+----------------+
13 rows in set (0.00 sec)

我希望这就是您所寻找的,它是一个无限可扩展的解决方案,尽管如果数据库因繁重的工作负载而负担过重,它可能会因其设计而减速。

是否有明确的类别列表,或者有人决定随时添加具有任意属性的类别吗?感谢您的回复,我忘记了提及一些信息我必须在表单上使用这些值,表单必须有复选框输入框选择框等。因此,我必须如何将值与类别的表单元素分组以显示其表单?