This question is related to another question I asked. In my other question I ask peoples opinions about 3 different ways I could construct a database. The cleanest way I can think of doing it without (practically) repeating tables and strange notions such as "super tables" is option 2:
Location [Table]
- Id
- Name
- HasLogger
- LoggerRFID
- LoggerUpperLimit
- LoggerLowerLimit
Sensor [Table]
- Id [PK]
- LocationId [FK]
- UpperLimit
- LowerLimit
SensorReading [Table]
- Id [PK]
- SensorId [FK]
- Value
LoggerReading [Table]
- LocationId [FK]
- Value
Alert [Table]
- Id [PK]
AlertCorrectiveAction [Table]
- AlertId [FK]
- CorrectiveActionId [FK]
- ByUserId [FK]
AlertAcknowledgement [Table]
- AlertId [FK]
- ByUserId [FK]
SensorAlertReading [Table]
- AlertId [FK]
- SensorReadingId [FK]
LoggerAlertReading [Table]
- AlertId [FK]
- LoggerReadingId [FK]
Now the problem with this option is that it allows readings from multiple sensors and multiple locations to be "linked" to a single alert.
To expand on why this is a problem, I will explain how the system works:
A location can contain many "live sensors", but only 1 logger. For this reason I put the logger attributes into the location table (it was effictively a 1 to 1 relationship). A logger collects readings until it is later collected, live sensors communicate readings immediately via a network and they have extra attributes like network slaves which have network address attributes.. so fairly different to loggers (I tried treating loggers as sensors at one point, didn't work out well).
When a sensor or logger goes out of range (indicated by the reading) the system generates an alert. The alert is for that sensor only and is considered active until a reading for that sensor (or logger) indicates that it is back in range. Until that time, readings that take the sensor further out of range are "linked" to that same alert.
So as you can see, a single alert should really only have readings for the same sensor linked to it, however my design above allows different reading from different sensors and loggers to be associated with the same alert - should I be bothered that I haven't constrained that somehow? The other problem is that it allows alerts to exist without having any readings.
Hence my question; just how far should one go with constraints or bending a design to fit those constraints? I like the design above because it is simple - alerts can have sensor readings and logger readings, so it's a simple relation to link them.
I can't help thinking I'm missing a trick too - is there just a much better way to do this design? I've gone round in circles with it for ages now and there always seems to be a compromise (unless I repeat all the alert tables for the different reading types).
Thanks.