2009-06-01 45 views
2

我需要爲房地產網站製作數據庫結構,用戶可以在其中創建多種類型的屬性以及與屬性相關的許多功能。在這種情況下使用的mysql表結構

的主要類別是: 1,房屋(亞型公寓,別墅,閣樓) 2.商業(亞型:賓館,大廈,寫字樓,工廠) 3.地形(亞型:市區,阿格里科拉,工業,對於運動)

上述所有功能都可以定義許多功能,例如公寓:燈光,燃氣,房間數量,浴室數量,樓層數量,陽臺等等,這些功能是不同的,另一個。

目前,我有property_terrain含有如地址和價格的基本信息命名property一個主表,以及三個子表property_houseproperty_commercial,並與許多領域擁有財產可以有。

這個結構好嗎?我需要將所有屬性類型創建和修改成一種形式,可能需要3-4步,並且會因屬性類型而異。如果我只有一個主表(如property)和另一個property_features(其中存儲property_id,feature_name和feature_value),會更容易嗎?性能和維護最適合什麼?你會投什麼?

謝謝! :)

回答

0

那麼,這三個主要類別設置在石頭?未來有沒有第四個出現的可能性?我可能會像這樣的東西去:

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) 
2

我有兩個經驗你提到的方式。 (我是iRealty http://www.irealtysoft.com/版本3的合作開發者,版本4有兩種不同的存儲方法)。經過幾年處理這兩種方式,我建議爲所有屬性創建一張表。這種模式稱爲單表繼承(Martin Fowler的http://martinfowler.com/eaaCatalog/singleTableInheritance.html)。

我看到這個方法只有兩個缺點:

  1. 字段名稱應該所有的財產類型
  2. 了很多紀錄將有NULL的範圍內是唯一的大約有自己的列會浪費磁盤空間有點

與此數據庫結構同時,所有的CRUD例程都非常簡單明瞭。您將節省大量時間構建查詢/ ORM層。通過這種結構,您可以自由地創建索引並在WHERE子句中使用算術和其他數據庫函數,並避免代價高昂的JOIN。

磁盤空間很便宜,開發時間很昂貴。

The | property_id | feature_name | feature_value |允許在更改字段和屬性類型時保持相同的數據庫結構,這在您擁有複雜的升級/更新例程時很有用。如果您要構建單個(生產)實例應用程序,升級不應該成爲問題。然而,這種方法使得CRUD模型變得複雜,因此更加昂貴並且容易出錯。 (更多代碼---更多bug)