![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Apologies in advance if this isn't the right group for this question! Forewarning: I'm not a DBA, I have limited experience working with MySQL on a personal, small-scale level. I'm trying to design a functional database for some inventory tracking. *Here is the structure I'm currently working with. *All IDs would be surrogate, auto-generated keys...I've omitted fields that are useless for this scenario, such as serial number. Device DeviceID (PK) Chassis ChassisID (PK) RackID (FK) Rack RackID (PK) Office OfficeID (PK) The idea is that we can find out, through the DB, exactly where a Device is. *It could be in an Office. *It could be in a Rack. *It could also be inside a Chassis that is inside a Rack (a chassis is - always- inside a Rack, hence the FK). Every halfway sane scenario I can come up with involves either a FK that points to multiple tables (which I gather is a big no-no), or having a nullable series of FKs, possibly in a separate location table: Location LocationID RackID ChassisID OfficeID |
|
So a device in a rack would have a LocationID FK, and that LocationID would have a RackID, but null ChassisID and OfficeID. Both of these seem like sub-optimal solutions. |
|
Ideas? *Am I missing an obvious solution? *Do I need to go back from square one and re-work this? *Any guidance would be greatly appreciated. |
#3
| ||||||
| ||||||
|
|
No DeviceID? |
|
You are designing a tracking DB and do not identify exactly what you are tracking?! |
|
Questions: If the "device" is in the "chassis", is it now part of the chassis? |
|
Are these "locations" really different? |
|
If the goal is to track devices, do you really need to know that a chassis is in a rack? *If device A is in chassis B is in Rack C then isn't A just in C? Apparently a chassis is not a location, so this problem is not well defined. |
|
Another probably poor design is: Table LOCATION type enum device, office, chassis, rack item_id number LOC_ID *PK Sample data Type * * * item_id * *loc_id device * * *5 * * * * * 1 office * * * 5 * * * * * *1 office * * * 6 * * * * * 3 chassis * 7 * * * * * 4 chassis * 8 * * * * * 5 rack * * * *1 * * * * * 5 device * * 3 * * * * * 5 rack * * * *2 * * * * * 6 device * * 4 * * * * * 6 device * * 9 * * * * * 9 |
#4
| |||
| |||
|
|
I think a simpler stating of this problem (and what I can't wrap my head around) is, what is the optimal way to store location information for an item that can be in more than one type of location. *I don't think the Office table is even relevant right now...whatever solution for the Chassis/Rack table problem would encompass it. *More than one location shouldn't present a problem (i.e. items in different addresses), it is the location type that is throwing me off. *Every solution I can think of involves either having a field pointing at multiple tables, or having one non-null value amongst other null values to indicate location type. Thanks for taking the time to ponder my predicament. |
#5
| |||
| |||
|
|
Apologies in advance if this isn't the right group for this question! Forewarning: I'm not a DBA, I have limited experience working with MySQL on a personal, small-scale level. I'm trying to design a functional database for some inventory tracking. Here is the structure I'm currently working with. All IDs would be surrogate, auto-generated keys...I've omitted fields that are useless for this scenario, such as serial number. Device DeviceID (PK) Chassis ChassisID (PK) RackID (FK) Rack RackID (PK) Office OfficeID (PK) The idea is that we can find out, through the DB, exactly where a Device is. It could be in an Office. It could be in a Rack. It could also be inside a Chassis that is inside a Rack (a chassis is - always- inside a Rack, hence the FK). Every halfway sane scenario I can come up with involves either a FK that points to multiple tables (which I gather is a big no-no), or having a nullable series of FKs, possibly in a separate location table: Location LocationID RackID ChassisID OfficeID So a device in a rack would have a LocationID FK, and that LocationID would have a RackID, but null ChassisID and OfficeID. |
|
Both of these seem like sub-optimal solutions. Ideas? Am I missing an obvious solution? Do I need to go back from square one and re-work this? Any guidance would be greatly appreciated. |
#6
| |||
| |||
|
|
On 03/19/2010 04:09 PM, JayCee wrote: Apologies in advance if this isn't the right group for this question! Forewarning: I'm not a DBA, I have limited experience working with MySQL on a personal, small-scale level. I'm trying to design a functional database for some inventory tracking. *Here is the structure I'm currently working with. *All IDs would be surrogate, auto-generated keys...I've omitted fields that are useless for this scenario, such as serial number. Device DeviceID (PK) Chassis ChassisID (PK) RackID (FK) Rack RackID (PK) Office OfficeID (PK) The idea is that we can find out, through the DB, exactly where a Device is. *It could be in an Office. *It could be in a Rack. *It could also be inside a Chassis that is inside a Rack (a chassis is - always- inside a Rack, hence the FK). Every halfway sane scenario I can come up with involves either a FK that points to multiple tables (which I gather is a big no-no), or having a nullable series of FKs, possibly in a separate location table: Location LocationID RackID ChassisID OfficeID So a device in a rack would have a LocationID FK, and that LocationID would have a RackID, but null ChassisID and OfficeID. And in this case you should also have a check constraint which avoids illegal combinations. Both of these seem like sub-optimal solutions. Ideas? *Am I missing an obvious solution? *Do I need to go back from square one and re-work this? *Any guidance would be greatly appreciated. One solution you apparently did not yet consider is inheritance. *Your containers could inherit an abstract base class solely for the purpose of containing Devices. *You could model that with an additional table which carries a PK column and possibly also a type discriminator. *All other container tables and the Device table then had FK's to the base class table Drawback is that you now make all your tables depend on the base class table which could impose a bottleneck - performance wise. Another solution would be to have a single pseudo FK column and a type discriminator which denotes the target table but I don't like that because it is not "type safe" - the FK type needs to be a superset of all FK's used which might work in your case because they are all surrogate keys but the DB has zero chance of checking integrity efficiently (you could implement it with triggers but I'd rather not go down that road - it's too fragile and error prone). All in all your suggestion with the various FKs augmented with a constraint avoiding illegal combinations of FK's seems the best compromise to me. *The DB can automatically check integrity and your FK's are "type safe" meaning you use exact the proper FK types for each container table. *The cost is of course a larger table - large in terms of columns not necessarily storage. *Btw, you can also put this in a separate table which only defines relationship between Device and its various containers. Kind regards * * * * robert -- remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/ |
#7
| |||
| |||
|
|
Thanks Ed, Robert. *I'm working right now with the tables using the LocationID as a FK in the separate locations and it seems to be doing what I want! |
![]() |
| Thread Tools | |
| Display Modes | |
| |