Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database: when to split into separate tables?

Say if I have two different types of sensors: one monitors analog voltage (such as on a temperature sensor) and one measures whether something is on or off (switch sensor).

I can't decide whether to have one table:

[Sensor]
Id : PK
UpperLimit : FLOAT
UpperLimitAlertDelay : INT
LowerLimit : FLOAT
LowerLimitAlertDelay : INT
IsAnalog : BOOL

[SensorReading]
Id : PK
SensorId : FK
AnalogValue : FLOAT
IsOn : BOOL

OR separate it all out into separate tables:

[AnalogSensor]
Id : PK
UpperLimit : FLOAT
UpperLimitAlertDelay : INT
LowerLimit : FLOAT
LowerLimitAlertDelay : INT

[AnalogSensorReadings]
Id : PK
AnalogSensorId : FK
Value : FLOAT

[SwitchSensor]
Id : PK
OnTooLongAlertDelay : INT

[SwitchSensorReadings]
Id : PK
SwitchSensorId : FK
IsOn : BOOL

At the moment I have it as one table and I use the "UpperLimitAlertDelay" as the "OnTooLongAlertDelay" when not using it as the analog sensor.

In the code I differentiate by the boolean flag on the Sensor table and create the appropriate object (i.e. AnalogSensor or SwitchSensor) but I'm wondering if it'd be neater / more proper at the database level to separate it out.

What rule of thumb would you use for this kind of decision? They are different entities on one level, but on another level you could say they are both just sensors.

This is often where I can never decide what direction to take when creating a database. Maybe whenever I use a bool to differentiate what fields mean / should be used, it should really be a separate table?

General thoughts on this topic or this sepcific problem appreciated.

Thanks!

EDIT: Some further information.

The switch sensors monitor things like whether a door is open, a fridge compressor is running, whether an appliance is turned on, etc.

Graphs and reports can be generated on any sensor so they are used in the same way; it's just the data will either be on/off or an analog value depending on the type.

So basically they are generally treated the same.

In the readings table, it is always one row for ONE reading off of ONE sensor.

So far the opinions seem to be quiet subjective - I guess there are just pros and cons to both ways.

Does the information above change anybody's opinion?

Thanks! Mark.

like image 336
Mark Avatar asked Oct 05 '10 13:10

Mark


People also ask

Why would we want to split data into separate tables?

In many cases, it may be best to split information into multiple related tables, so that there is less redundant data and fewer places to update.

When should you split a database?

If a database is shared with several people over a network, you should consider splitting it. Splitting a database reorganizes it into two files — a back-end database that contains the database's tables, and a front-end database that contains all the other database objects such as queries, forms, and reports.

Why would you need to join two tables?

It is used to create a combination of two different sets without have mutual columns. As an example, if we need to create a combination of all departments with all employees.

Is the process of breaking tables into multiple tables in a database?

The answer is to split our data up across multiple different tables, and create relationships between them. The process of splitting up data in this way to remove duplication and improve data integrity is known as normalization.


1 Answers

Is this the same application/database as your other question ?

In which case, the answer has been supplied in that Data Model.

If it isn't the same app/db, or if this question has not been adequately answered, pls post or comment. Eg. Based on previous info, I modelled it so that the SensorType table differentiates Sensor (analogue or boolean) ... but we could:

  • differentiate it at the Sensor level,

  • or make Reading into subtypes: ReadingAnalog and ReadingSwitch. That could make it a little easier for the programs that produce graphs, etc.

like image 88
PerformanceDBA Avatar answered Sep 30 '22 15:09

PerformanceDBA