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 ca
Why not have:
Alert [Table]
- Id [PK]
- SensorReadingId [FK]
- LoggerReadingId [FK]
And then you fill either the SensorReadingId or the LoggerReadingId. I suppose your structure is a simplified one, but often a table with nothing else then one PK is redundent.
should I be bothered that I haven't constrained that somehow?
Yes.
You have made two basic mistakes.
Sticking Id
iot keys on everything that moves.
That has hindered your ability to model the data, as data (not as rows that have no meaning, but with an artificially enforced uniqueness), and expose Identifers; and Dependdencies (eg. a Sensor is Dependent o a Location). You are modelling spreadsheets, with pre-set Row_Ids, containing data. You need to Normalise the data, as data.
This has resulted in the problem you have identified , but there are other problems as well.
If you model the data, the Identifiers will be clear, and the Index and FK constraints will prevent this. What data is independent; what data belongs (is dependent on) what other data; what data does what to other data, and the basis of those actions.
Then (the major issues having been addressed) you are left with only minor constraints to address minor areas.
Otherwise you are stuck with adding constraints all over the place to try and get what you want, but never quite getting there. You know you need them, so you are looking for them.
Wrong place. We need to back up to (1).
I have answered your other question, and included a ▶Sensor Data Model◀. That does not address the deficiencies you identify here. However, I just saw this question, I will update the DM tomorrow and include these tables and columns.
▶Link to IDEF1X Notation◀ for anyone who is unfamiliar with the Standard for modelling Relational databases.
Questions
It looks like you need a reference table for Sensors, the shelf item, to hold UpperLimit and LowerLimit; rather than repeating it for every Location. Or are they set, localised, for each Location.
Think about the Logger being SensorNo zero.
Why don't the Sensors have an RFID ?
At each Location
, is the Logger
optional, is it 1::0-1 ?,