I am trying to develop a home automation/monitoring system. This involves a bunch of sensors distributed around the house/garden/garage. I'd like to store the values for the sensor readings. I am trying to make the database design flexible and general so that I don't have to change it around once it is set up. However, I seem to have ended up with two possible options and I can't quite see which is the most optimal. The tables that are common to both are:
tbl_region = id, region_name - (e.g 1, first)
tbl_room = id, region_id, room_name - (e.g 1, 1, lounge)
tbl_sensor = id, sensor_name - e.g (1, dht11)
tbl_measurement = id, measurement_name - e.g (1, temperature)
Since each sensor can have many measurement types (e.g temp, humidty, pressure) and a measurement can be done by many sensors (e.g dht11, bme280) there needs to be a junction table to define the many to many relationship
tbl_sensor_measurement = id, sensortype_id, measurementtype_id - (e.g 1, 1, 1) for a dht11 sensor's temperature measurement (it does other things too)
I can potentially have multiples of the same sensor in the same room so there is a many to many relationship between rooms and sensors. Here is where my two ideas diverge:
- I could have a junction table between tbl_room and tbl_sensor_measurement:
tbl_room_sensor_measurement = id, room_id, sensor_measurement_id
this would give me mutliple rows for a sensor in a room (one for each type of measurement) and allow the same type of sensor to be housed in that room. This would mean that my final data table would only have four columns
tbl_value = id, room_sensor_measurement_id, value, timestamp
- I could have a junction table between tbl_room and tbl_sensor
tbl_room_sensor = id, room_id, sensor_id
this would give me one row for a sensor in a room and allow the same type of sensor to be housed in that room. This would mean that my final data table would now have five columns
tbl_value = id, room_sensor_id, measurement_id, value, timestamp
I think that the model 2 is going to be easier to retrieve data later on (I can pick a sensor in a room and get all of the measurements easily. I am new to rdb design and i'm not sure which to go for (or if there is an entirely different way altogether) and would appreciate any pointers.
Thank you for your help,
Martyn