Хорошо, эти три основные категории установлены в камне? Есть ли вероятность появления четвертого в будущем? Я бы, наверное, пошел с чем-то вроде этого:
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)