Hi everyone, here´s my question:
Consider that you want a model a problem in which there are N buildings, numbered sequentially, each building has N floors numbered sequentially and each floor has N rooms numbered sequentially too. So there are several ways to model this:
A)
Buildings
building_num INTEGER (PRIMARY KEY)
Floors
floor_num INTEGER (PRIMARY KEY)
building_num INTEGER (PRIMARY KEY) (FOREIGN KEY to Buildings)
Rooms
room_num INTEGER (PRIMARY KEY)
building_num INTEGER (PRIMARY KEY) (FOREIGN KEY to Buildings)
floor_num INTEGER (PRIMARY KEY) (FOREIGN KEY to Floors)
As you can see there´s a lot of redundancy in this tables, but if I conserve the model this way it will support easily changes in requirements like storing other data about floors without a lot of work. Now consider the following version:
B)
Buildings
id_building INTEGER (PRIMARY KEY)
num_building INTEGER
Floors
id_floor INTEGER (PRIMARY KEY)
id_building INTEGER (FOREIGN KEY to Buildings)
num_floor INTEGER
Rooms
id_room INTEGER (PRIMARY KEY)
id_floor (FOREIGN KEY to Floors)
num_floor
With this approach we could reference from another table a single row of Rooms table with only the id_room value which is an autonumeric value but I would have to examine each table primary, foreign keys and other attributes in order to identify the desired room what could cause query complexity and performance hit in the future if those tables become very large. Also for this design I would need 2 indexes per table (the primary key and a unique constraint for num_x).
Another way would be having a table called Rooms with all the data it requires and omitting tables Buildings and Floors like this way:
C)
Rooms
room_num INTEGER (PRIMARY KEY)
floor_num INTEGER (PRIMARY KEY)
building_num INTEGER (PRIMARY KEY)
But the real problem with this is that I if I want to reference a single row of this table I would have to conserve a composite key of 3 columns in the referencing tables which duplicates a lot of data.
And maybe another way could be the following:
D)
Rooms
id_room INTEGER (PRIMARY KEY)
room_num INTEGER (PRIMARY KEY)
floor_num INTEGER (PRIMARY KEY)
building_num INTEGER (PRIMARY KEY)
But this will need again two indexes (primary key and unique constraint) instead of only 1 (primary key).
Every table in above possible solutions is normalized to the third normal form but as you can see each approach has its advantages and disadvantages but I´m not sure what to do? which one do you consider the best? is there another way to model this situation?, is there something I haven´t consider? in simple words... WHAT DO YOU THINK??????