我需要使一个房地产网站的数据库结构,在那里用户可以创建许多类型的属性,并与许多相关的属性功能。
主要类别将是: 1.房屋(子类型:公寓、房屋、阁楼) 2.商业(子类型:旅馆、建筑物、写字楼、工厂) 3.地形(子类型:城市、农业、工业、体育)
以上所有功能都可以定义许多功能,例如公寓:采光、燃气、房间数量、浴室、楼层编号、阳台等,并且这些功能因属性类型的不同而不同。
目前,我有一个名为property的主表,其中包含地址和价格等基本信息,以及三个子表property_house、property_commercial和property_terrain,它们的字段数量与一个属性可以具有的字段数量相同。
这个结构还好吗?我需要做的所有属性类型的创建和修改成一个形式,可能与3-4个步骤,并将根据不同的属性类型。如果我只有一个像property这样的主表和另一个property_features来存储property_id、feature_name和feature_value,会不会更简单?什么对性能和维护是最好的?你们这些人会投什么票?
谢谢!:)
发布于 2009-06-01 08:01:25
我对你提到的两种方式都有经验。(我是iRealty http://www.irealtysoft.com/版本3和版本4的联合开发人员,它们有两种不同的存储方法)。在处理这两种方法几年之后,我建议为所有属性创建一个表。这种模式被称为单表继承( Martin Fowler的http://martinfowler.com/eaaCatalog/singleTableInheritance.html)。
我认为这种方法只有两个缺点:
同时,使用这种数据库结构,所有的CRUD例程都非常简单和直接。您将节省大量构建查询/ORM层的时间。有了这种结构,您就可以自由地在WHERE子句中创建索引、利用算术和其他数据库函数,并避免代价高昂的连接。
磁盘空间便宜,开发时间昂贵。
| property_id | feature_name | feature_value |允许在更改字段和属性类型时保持相同的数据库结构,这在有复杂的升级/更新例程时非常有用。如果您要构建单个(生产)实例应用程序,那么升级应该不是问题。然而,这种方法使得CRUD模型变得复杂,因此更昂贵,也更容易出现bug。(更多代码-更多错误。)
发布于 2009-06-01 07:47:05
那么,这三个主要类别是不是一成不变的呢?有没有可能在未来出现第四个?我可能会这样说:
CREATE TABLE property (
id int not null auto_increment,
name varchar(250) not null,
property_type int not null,
property_subtype int not null,
primary key(id)
);
CREATE TABLE property_type (
id int not null auto_increment,
name varchar(250) not null,
primary key(id)
);
CREATE TABLE property_subtype (
id int not null auto_increment,
type int not null,
name varchar(250) not null,
primary key(id)
);
CREATE TABLE property_feature (
id int not null auto_increment,
property int not null,
feature int not null,
value varchar(250) not null,
primary key(id)
);
CREATE TABLE property_feature (
id int not null auto_increment,
feature int not null,
value varchar(250) not null,
primary key(id)
);我认为,从长远来看,这将是最有效的,如果时机成熟,这将是最灵活的。
使用这种结构,您可以像这样添加数据:
mysql> INSERT INTO property_type (name) VALUES ('House'),('Commercial'),('Terrains');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> INSERT INTO property_subtype (type, name) VALUES (1, 'Apartment'),(1, 'House'), (1,'Loft');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> INSERT INTO subtype_feature (subtype, name) VALUES (1, 'Light'),(1, 'Floor #');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> INSERT INTO property (name, property_type, property_subtype) VALUES ('Som
e Apartment', 1, 1);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO property_feature (feature, value) VALUES (1, 'Yes'),(2, '5th');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> INSERT INTO property_feature (property, feature, value) VALUES (1, 1, 'Yes'),(1, 2, '5th');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0然后,您可以非常容易地获取特定属性的所有功能:
mysql> SELECT s.name, f.value FROM property_feature f INNER JOIN subtype_feature
s ON f.feature = s.id WHERE f.property = 1;
+---------+-------+
| name | value |
+---------+-------+
| Light | Yes |
| Floor # | 5th |
+---------+-------+
2 rows in set (0.00 sec)https://stackoverflow.com/questions/933872
复制相似问题