views:

80

answers:

2

Hi. Consider this very small contrived subset of my schema:

SensorType1
ID : PK

SensorType2
ID : PK

Reading
Timestamp
Value
SensorType1_ID FK -> SensorType1
SensorType2_ID FK -> SensorType2

Some readings are for SensorType1, some are for SensorType2. I would probably add a constraint to ensure exclusively one of those FK's is always pointing somewhere.

I've read a lot in the past about NULL FK's being very bad design, but I've been wrestling with my schema for days (see previous posts) and no matter which way I twist and turn it, I either end up with a NULL-able FK somewhere, or I have to duplicate my reading table (and it's dependants) for every sensor type I have (3).

The above just seems to solve the problem nicely, but it leaves a not-so-nice taste in my mouth, for some reason. It is the ONE place in my entire schema where I allow NULL fields.

I thought a bit of peer review would help me accept it before I move on.

Thanks!

+2  A: 

What is wrong with doing it like:

Sensor
  ID: PK
  ... common sensor fields ...

SensorType1
  ID: FK(Sensor)
  ... specifics ...

SensorType2
  ID: FK(Sensor)
  ... specifics ...

Reading
  ID: PK
  Sensor: FK(Sensor)
  Timestamp: DateTime
  Value: whatever
Mike DeSimone
What stops a row in SensorType1 and SensorType2 both accidently pointing to the same Sensor? The database would not catch that integrity error.
Mark
Seriously, if there's a way to stop that happening then I'll use it. It's what I originally had, but it was too easy for the database to get in a mess without something to catch the data integrity error.
Mark
@marksim: you may want to add some constraints and it will work perfectly. The way to do is: add two fields to Sensor (isType1, isType2), they must be exclusive (a check constraint will do it) and you need constraints like if isType_ is not null then sensortype_ of the same id must exist.
Chris
The nest way I know to stop the integrity error is to have a trigger on the two sensortype tables that will not allow inserting if the sensor ID is in the other table.
HLGEM
You can ensure the sensor can only be of one type like this:http://consultingblogs.emc.com/davidportas/archive/2007/01/08/Distributed-Keys-and-Disjoint-Subtypes.aspx
dportas
A: 

First PK's as just "ID" mean they have to change names constantly throughout the model. It makes following the RI difficult. I know some people like that. I hate it because it prevents an automated approach to finding columns.

I do things like this

SELECT * FROM ALL_TAB_COLUMNS WHERE Column_Name = :1;

If you need to "role play" have the same FK twice in a table then

LIKE '%' || :1 should work.

But you're changing col names even when not forced to. ID becomes Location_ID and then becomes LoggingLocation_ID for no technical reason

I'm assuming this isn't a physical model. If it is, why are you vertically partitioning LiveMonitoringLocation and HandProbingLocation? Is it just to avoid a nullable column? If so you're utility function is all messed up. Nullable columns are fine... adding a new table to avoid a nullable column is like driving from NYC to Cleveland to Boston in order to avoid any red lights.

Ess Oh Hader